File Coverage

blib/lib/Algorithm/Dependency/Source/File.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 16 50.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 43 51 84.3


line stmt bran cond sub pod time code
1             package Algorithm::Dependency::Source::File;
2             # ABSTRACT: File source for dependency hierarchies
3              
4             #pod =pod
5             #pod
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod Algorithm::Dependency::Source::File implements a
9             #pod L where the items are stored in a flat
10             #pod file or a relatively simple format.
11             #pod
12             #pod =head2 File Format
13             #pod
14             #pod The file should be an ordinary text file, consisting of a series of lines,
15             #pod with each line completely containing the information for a single item.
16             #pod Blank lines, or lines beginning with the hash character '#' will be
17             #pod ignored as comments.
18             #pod
19             #pod For a single item line, only word characters will be used. A 'word character'
20             #pod consists of all letters and numbers, and the underscore '_' character.
21             #pod Anything that is not a word character will be assumed to be a separator.
22             #pod
23             #pod The first word will be used as the name or id of the item, and any further
24             #pod words in the line will be used as other items that this one depends on. For
25             #pod example, all of the following are legal.
26             #pod
27             #pod # A single item with no dependencies
28             #pod Foo
29             #pod
30             #pod # Another item that depends on the first one
31             #pod Bar Foo
32             #pod
33             #pod # Depending on multiple others
34             #pod Bin Foo Bar
35             #pod
36             #pod # We can use different separators
37             #pod One:Two|Three-Four+Five=Six Seven
38             #pod
39             #pod # We can also use multiple non-word characters as separators
40             #pod This&*&^*&File: is& & & :::REALLY()Neat
41             #pod
42             #pod From the examples above, it should be easy to create your own files.
43             #pod
44             #pod =head1 METHODS
45             #pod
46             #pod This documents the methods differing from the ordinary
47             #pod L methods.
48             #pod
49             #pod =cut
50              
51 7     7   3969 use 5.005;
  7         31  
52 7     7   38 use strict;
  7         16  
  7         133  
53 7     7   45 use Algorithm::Dependency::Source ();
  7         15  
  7         2718  
54              
55             our $VERSION = '1.112';
56             our @ISA = 'Algorithm::Dependency::Source';
57              
58              
59              
60              
61             #####################################################################
62             # Constructor
63              
64             #pod =pod
65             #pod
66             #pod =head2 new $filename
67             #pod
68             #pod When constructing a new Algorithm::Dependency::Source::File object, an
69             #pod argument should be provided of the name of the file to use. The constructor
70             #pod will check that the file exists, and is readable, returning C
71             #pod otherwise.
72             #pod
73             #pod =cut
74              
75             sub new {
76 9     9 1 3977 my $class = shift;
77 9 50       34 my $filename = shift or return undef;
78 9 50       255 return undef unless -r $filename;
79              
80             # Get the basic source object
81 9 50       88 my $self = $class->SUPER::new() or return undef;
82              
83             # Add our arguments
84 9         28 $self->{filename} = $filename;
85              
86 9         26 $self;
87             }
88              
89              
90              
91              
92              
93             #####################################################################
94             # Private Methods
95              
96             sub _load_item_list {
97 9     9   19 my $self = shift;
98              
99             # Load the contents of the file
100 9         40 local $/ = undef;
101 9 50       380 open( FILE, $self->{filename} ) or return undef;
102 9 50       250 defined(my $source = ) or return undef;
103 9 50       111 close( FILE ) or return undef;
104              
105             # Split, trim, clean and remove comments
106 9         200 my @content = grep { ! /^\s*(?:\#|$)/ }
  110         322  
107             split /\s*[\015\012][\s\015\012]*/, $source;
108              
109             # Parse and build the item list
110 9         31 my @Items = ();
111 9         26 foreach my $line ( @content ) {
112             # Split the line by non-word characters
113 94         289 my @sections = grep { length $_ } split /\W+/, $line;
  183         369  
114 94 50       219 return undef unless scalar @sections;
115              
116             # Create the new item
117 94 50       290 my $Item = Algorithm::Dependency::Item->new( @sections ) or return undef;
118 94         237 push @Items, $Item;
119             }
120              
121 9         57 \@Items;
122             }
123              
124             1;
125              
126             __END__