1Algorithm::Dependency::UWseeirghCto(n3t)ributed Perl DocAulmgeonrtiatthimo:n:Dependency::Weight(3)
2
3
4
6 Algorithm::Dependency::Weight - Calculate dependency 'weights'
7
9 # Create a source from a file
10 my $Source = Algorithm::Dependency::Source->new( 'file.txt' );
11
12 # Create a Weight algorithm object
13 my $alg = Algorithm::Dependency::Weight->new( source => $Source );
14
15 # Find the weight for a single item
16 my $weight = $alg->weight('foo');
17 print "The weight of 'foo' is $weight\n";
18
19 # Or a group
20 my $hash = $alg->weight_hash('foo', 'bar', 'baz');
21 print "The weight of 'foo', 'bar', and 'bar' are $hash->{foo},"
22 . " $hash->{bar} and $hash->{baz} respectively\n";
23
24 # Or all of the items
25 my $all = $alg->weight_all;
26 print "The following is a list from heaviest to lightest:\n";
27 foreach ( sort { $all->{$b} <=> $all->{$a} } keys %$all ) {
28 print "$_: $all->{$_}\n";
29 }
30
32 In dependency systems, it can often be very useful to calculate an
33 aggregate or sum for one or all items. For example, to find the "naive
34 install weight" of a Perl distribution (where "naive" means you treat
35 each distribution equally), you would want the distribtion (1) + all
36 its dependencies (n) + all their dependencies (n2) recursively
37 downwards.
38
39 If calculated using a normal Algorithm::Dependency object, the result
40 would be (in a simple systems) equal to:
41
42 # Create your normal (non-ordered alg:dep)
43 my $dependency = Algorithm::Dependency->new( ... );
44
45 # Find the naive weight for an item
46 my $weight = scalar($dependency->schedule('itemname'));
47
48 "Algorithm::Dependency::Weight" provides a way of doing this with a
49 little more sophistication, and in a way that should work reasonable
50 well across all the Algorithm::Dependency family.
51
52 Please note that the this might be a little (or more than a little)
53 slower than it could be for the limited case of generating weights for
54 all of the items at once in a dependency system with no selected items
55 and no circular dependencies. BUT you can at least rely on this class
56 to do the job properly regardless of the particulars of the situation,
57 which is probably more important.
58
59 METHODS
60 new @params
61 The "new" constructor creates a new "Algorithm::Dependency::Weight"
62 object. It takes a number of key/value pairs as parameters (although at
63 the present time only one).
64
65 source => $Source
66 The "source" param is mostly the same as for Algorithm::Dependency.
67 The one addition is that as a source you can provide an
68 Algorithm::Dependency object, and the Algorithm::Dependency::Source
69 for that will be used.
70
71 Returns a new "Algorithm::Dependency::Weight" object, or "undef" on
72 error.
73
74 source
75 The "source" accessor returns the source used for the weight
76 calculations.
77
78 This will be either the one passed to the constructor, or the source
79 from inside the "Algorithm::Dependency" object passed as the "source"
80 param (not the object itself, its source).
81
82 weight $name
83 The "weight" method takes the name of a single item and calculates its
84 weight based on the configuration of the
85 "Algorithm::Dependency::Weight" object.
86
87 Returns the weight as a scalar (which in the naive case will be an
88 integer, but in more complex uses may be any real number), or "undef"
89 on error.
90
91 weight_merged @names
92 The "weight_merged" method takes the name of a set of items and
93 calculates an aggregated weight for the whole set.
94
95 Returns the weight as a scalar, or "undef" on error.
96
97 weight_hash @names
98 The "weight_hash" method takes a list of item names, and calculates
99 their weights.
100
101 Returns a reference to a "HASH" with the item names as keys and weights
102 as values, or "undef" on error.
103
104 weight_all
105 The "weight_all" method provides the one-shot method for getting the
106 weights of all items at once. Please note that this does not do
107 anything different or special, but is slightly faster than iterating
108 yourself.
109
110 Returns a reference to a "HASH" with the item names as keys and weights
111 as values, or "undef" on error.
112
114 - Add support for non-naive weights via either custom code or method
115 name
116
118 Bugs should be submitted via the CPAN bug tracker, located at
119
120 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Dependency>
121
122 For general comments, contact the author.
123
125 Adam Kennedy <adamk@cpan.org>
126
128 Algorithm::Dependency, Algorithm::Dependency::Source
129
131 Copyright 2003 - 2009 Adam Kennedy.
132
133 This program is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136 The full text of the license can be found in the LICENSE file included
137 with this module.
138
139
140
141perl v5.28.0 2009-04-14 Algorithm::Dependency::Weight(3)