/usr/share/perl5/Algorithm/Dependency/Source.pm is in libalgorithm-dependency-perl 1.110-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | package Algorithm::Dependency::Source;
=pod
=head1 NAME
Algorithm::Dependency::Source - Implements a source of heirachy items
=head1 DESCRIPTION
The Algorithm::Dependency::Source class provides an abstract parent class for
implementing sources for the heirachy data the algorithm will use. For an
example of an implementation of this, see
L<Algorithm::Dependency::Source::File>, which is bundled with the main
L<Algorithm::Dependency> package.
=head1 METHODS
=cut
use 5.005;
use strict;
use Algorithm::Dependency ();
use Params::Util qw{_SET};
use vars qw{$VERSION};
BEGIN {
$VERSION = '1.110';
}
#####################################################################
# Constructor
=pod
=head2 new @arguments
Although you cannot directly use the C<new> constructor for
C<Algorithm::Dependency::Source>, it will work the same in all subclasses.
The constructor takes zero or more subclass specific arguments to define the
location of the source of the items, and returns a new object. Alrough it
may check that the arguments you passed are valid, the source will usually
NOT actually load the items from the source, instead defering the loading
until you need to use the items.
Returns a new object on success, or C<undef> on error.
=cut
sub new {
my $class = shift;
# This can't be created directly, it must be through
# a SUPER::new call
if ( $class eq __PACKAGE__ ) {
die "Cannot directly instantiate Algorithm::Dependency::Source."
. " You must use a subclass";
}
# Create the basic object
my $self = bless {
# Has the source been loaded
loaded => 0,
# Indexes
items_hash => undef,
items_array => undef,
}, $class;
$self;
}
=pod
=head2 load
The C<load> method is the public method used to actually load the items from
their storage location into the the source object. The method will
automatically called, as needed, in most circumstances. You would generally
only want to use C<load> manually if you think there may be some uncertainty
that the source will load correctly, and want to check it will work.
Returns true if the items are loaded successfully, or C<undef> on error.
=cut
sub load {
my $self = shift;
# If this is a reload, clean up in preperation
if ( $self->{loaded} ) {
$self->{loaded} = 0;
$self->{items_hash} = undef;
$self->{items_array} = undef;
}
# Pass through to the real loader
my $items = $self->_load_item_list;
return $items unless $items;
unless ( _SET($items, 'Algorithm::Dependency::Item') ) {
die( ref($self) . "::_load_item_list did not return an Algorithm::Dependency::Item set" );
}
# Add the items
foreach my $item ( @$items ) {
# Have we added this one already?
my $id = $item->id;
if ( $self->{items_hash}->{ $id } ) {
# Duplicate entry
return undef;
}
# Add it
push @{ $self->{items_array} }, $item;
$self->{items_hash}->{$id} = $item;
}
$self->{loaded} = 1;
}
=pod
=head2 item $name
The C<item> method fetches and returns the item object specified by the
name argument.
Returns an L<Algorithm::Dependency::Item> object on success, or C<undef> if
the named item does not exist in the source.
=cut
sub item {
my $self = shift;
my $id = (defined $_[0] and ! ref $_[0] and $_[0] ne '') ? shift : return undef;
$self->{loaded} or $self->load or return undef;
# Return the item (or undef)
$self->{items_hash}->{$id};
}
=pod
=head2 items
The C<items> method returns, as a list of objects, all of the items
contained in the source. The item objects will be returned in the same order
as that in the storage location.
Returns a list of L<Algorithm::Dependency::Item> objects on success, or
C<undef> on error.
=cut
sub items {
my $self = shift;
$self->{loaded} or $self->load or return undef;
@{ $self->{items_array} };
}
=pod
=head2 missing_dependencies
By default, we are leniant with missing dependencies if the item is neved
used. For systems where having a missing dependency can be very bad, the
C<missing_dependencies> method checks all Items to make sure their
dependencies exist.
If there are any missing dependencies, returns a reference to an array of
their ids. If there are no missing dependencies, returns 0. Returns
C<undef> on error.
=cut
sub missing_dependencies {
my $self = shift;
$self->{loaded} or $self->load or return undef;
# Merged the depends of all the items, and see if
# any are missing.
my %missing = map { $_ => 1 }
grep { ! $self->item($_) }
map { $_->depends }
$self->items;
%missing ? [ sort keys %missing ] : 0;
}
#####################################################################
# Catch unimplemented methods in subclasses
sub _load_item_list {
die "Class $_[0] failed to define the method _load_item_list";
}
1;
=pod
=head1 EXTENDING
C<Algorithm::Dependency::Source> itself is a fairly thin module, and it
is intended that you will probably need to extend it to be able to
extract item data from whatever location you have stored them.
This is usually a fairly simple two step process.
=over 4
=item Overload the C<new> method.
Assuming your subclass takes some form or argument on creation, you will
need to overload the C<new> method to accept the arguments, validate them,
and store them in the source object.
=item Define the method C<_load_item_list>.
Leaving our parent's C<load> method to take care of conflict, errors, and
whatever, the C<_load_item_list> method is used to simply create a list of
L<Algorithm::Dependency::Item> objects from wherever you store the item,
and return them as a list.
=back
Having completed these two things, your subclass should be completed. For
an example of the code, have a look at the source for the simple subclass
L<Algorithm::Dependency::Source::File>.
=head1 SUPPORT
For general comments, contact the author.
To file a bug against this module, in a way you can keep track of, see the
CPAN bug tracking system.
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Dependency>
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 SEE ALSO
L<Algorithm::Dependency>, L<Algorithm::Dependency::Source::File>
=head1 COPYRIGHT
Copyright 2003 - 2009 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|