Algorithm-Dependency
view release on metacpan or search on metacpan
lib/Algorithm/Dependency/Weight.pm view on Meta::CPAN
#pod items and no circular dependencies. BUT you can at least rely on
#pod this class to do the job properly regardless of the particulars of
#pod the situation, which is probably more important.
#pod
#pod =head2 METHODS
#pod
#pod =cut
use 5.005;
use strict;
use List::Util ();
use Algorithm::Dependency ();
use Params::Util qw{_INSTANCE _STRING};
our $VERSION = '1.112';
#####################################################################
# Constructor and Accessors
#pod =pod
#pod
#pod =head2 new @params
#pod
#pod The C<new> constructor creates a new C<Algorithm::Dependency::Weight>
#pod object. It takes a number of key/value pairs as parameters (although
#pod at the present time only one).
#pod
#pod =over 4
#pod
#pod =item source => $Source
#pod
#pod The C<source> param is mostly the same as for L<Algorithm::Dependency>.
#pod The one addition is that as a source you can provide an
#pod L<Algorithm::Dependency> object, and the L<Algorithm::Dependency::Source>
#pod for that will be used.
#pod
#pod =back
#pod
#pod Returns a new C<Algorithm::Dependency::Weight> object, or C<undef> on error.
#pod
#pod =cut
sub new {
my $class = shift;
my %args = @_;
# Get the source object, or derive it from an existing alg-dep
my $source = _INSTANCE($args{source}, 'Algorithm::Dependency')
? $args{source}->source
: _INSTANCE($args{source}, 'Algorithm::Dependency::Source')
or return undef;
# Build the alg-dep object we use
my $algdep = Algorithm::Dependency->new(
source => $source,
ignore_orphans => 1,
) or return undef;
# Create the basic object
my $self = bless {
source => $source,
algdep => $algdep,
weight => {},
}, $class;
$self;
}
#pod =pod
#pod
#pod =head2 source
#pod
#pod The C<source> accessor returns the source used for the weight calculations.
#pod
#pod This will be either the one passed to the constructor, or the source from
#pod inside the C<Algorithm::Dependency> object passed as the C<source> param
#pod (B<not> the object itself, B<its> source).
#pod
#pod =cut
sub source {
$_[0]->{source}
}
#####################################################################
# Algorithm::Dependency::Weight Methods
#pod =pod
#pod
#pod =head2 weight $name
#pod
#pod The C<weight> method takes the name of a single item and calculates its
#pod weight based on the configuration of the C<Algorithm::Dependency::Weight>
#pod object.
#pod
#pod Returns the weight as a scalar (which in the naive case will be an
#pod integer, but in more complex uses may be any real number), or C<undef>
#pod on error.
#pod
#pod =cut
sub weight {
my $self = shift;
my $id = defined(_STRING($_[0])) ? shift : return undef;
$self->{weight}->{$id} or
$self->{weight}->{$id} = $self->_weight($id);
}
sub _weight {
my $self = shift;
my $items = $self->{algdep}->schedule($_[0]) or return undef;
scalar(@$items);
}
#pod =pod
#pod
( run in 0.847 second using v1.01-cache-2.11-cpan-e93a5daba3e )