File Coverage

blib/lib/Algorithm/Dependency/Source/File.pm
Criterion Covered Total %
statement 31 31 100.0
branch 8 16 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 46 54 85.2


line stmt bran cond sub pod time code
1             package Algorithm::Dependency::Source::File;
2              
3             =pod
4            
5             =head1 NAME
6            
7             Algorithm::Dependency::Source::File - File source for dependency heirachys
8            
9             =head1 DESCRIPTION
10            
11             Algorithm::Dependency::Source::File implements a
12             L<source|Algorithm::Dependency::Source> where the items are stored in a flat
13             file or a relatively simple format.
14            
15             =head2 File Format
16            
17             The file should be an ordinary text file, consisting of a series of lines,
18             with each line completely containing the information for a single item.
19             Blank lines, or lines beginning with the hash character '#' will be
20             ignored as comments.
21            
22             For a single item line, only word characters will be used. A 'word character'
23             consists of all letters and numbers, and the underscore '_' character.
24             Anything that is not a word character will be assumed to be a seperator.
25            
26             The first word will be used as the name or id of the item, and any further
27             words in the line will be used as other items that this one depends on. For
28             example, all of the following are legal.
29            
30             # A single item with no dependencies
31             Foo
32            
33             # Another item that depends on the first one
34             Bar Foo
35            
36             # Depending on multiple others
37             Bin Foo Bar
38            
39             # We can use different seperators
40             One:Two|Three-Four+Five=Six Seven
41            
42             # We can also use multiple non-word characters as seperators
43             This&*&^*&File: is& & & :::REALLY()Neat
44            
45             From the examples above, it should be easy to create your own files.
46            
47             =head1 METHODS
48            
49             This documents the methods differing from the ordinary
50             L<Algorithm::Dependency::Source> methods.
51            
52             =cut
53              
54 7     7   93 use strict;
  7         119  
  7         789  
55 7     7   161 use base 'Algorithm::Dependency::Source';
  7         65  
  7         104  
56              
57 7     7   135 use vars qw{$VERSION};
  7         328  
  7         97  
58             BEGIN {
59 7     7   95 $VERSION = '1.102';
60             }
61              
62              
63              
64              
65              
66             #####################################################################
67             # Constructor
68              
69             =pod
70            
71             =head2 new $filename
72            
73             When constructing a new Algorithm::Dependency::Source::File object, an
74             argument should be provided of the name of the file to use. The constructor
75             will check that the file exists, and is readable, returning C<undef>
76             otherwise.
77            
78             =cut
79              
80             sub new {
81 9     9 1 92 my $class = shift;
82 9 50       109 my $filename = shift or return undef;
83 9 50       312 return undef unless -r $filename;
84              
85             # Get the basic source object
86 9 50       268 my $self = $class->SUPER::new or return undef;
87              
88             # Add our arguments
89 9         103 $self->{filename} = $filename;
90              
91 9         93 $self;
92             }
93              
94              
95              
96              
97              
98             #####################################################################
99             # Private Methods
100              
101             sub _load_item_list {
102 9     9   85 my $self = shift;
103              
104             # Load the contents of the file
105 9         106 local $/ = undef;
106 9 50       592 open( FILE, $self->{filename} ) or return undef;
107 9 50       901 defined(my $source = <FILE>) or return undef;
108 9 50       234 close( FILE ) or return undef;
109              
110             # Split, trim, clean and remove comments
111 9         311 my @content = grep { ! /^\s*(?:\#|$)/ }
  110         1216  
112             split /\s*[\015\012][\s\015\012]*/, $source;
113              
114             # Parse and build the item list
115 9         137 my @Items = ();
116 9         95 foreach my $line ( @content ) {
117             # Split the line by non-word characters
118 94         1131 my @sections = grep { length $_ } split /\W+/, $line;
  183         19909  
119 94 50       1017 return undef unless scalar @sections;
120              
121             # Create the new item
122 94 50       1369 my $Item = Algorithm::Dependency::Item->new( @sections ) or return undef;
123 94         1234 push @Items, $Item;
124             }
125              
126 9         185 \@Items;
127             }
128              
129             1;
130              
131             =pod
132            
133             =head1 SUPPORT
134            
135             To file a bug against this module, use the CPAN bug tracking system
136            
137             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Dependency>
138            
139             For other comments, contact the author.
140            
141             =head1 AUTHOR
142            
143             Adam Kennedy, L<http://ali.as/>, cpan@ali.as
144            
145             =head1 SEE ALSO
146            
147             L<Algorithm::Dependency>
148            
149             =head1 COPYRIGHT
150            
151             Copyright (c) 2003 - 2005 Adam Kennedy. All rights reserved.
152            
153             This program is free software; you can redistribute
154             it and/or modify it under the same terms as Perl itself.
155            
156             The full text of the license can be found in the
157             LICENSE file included with this module.
158            
159             =cut
160