Dependency/Source/File.pm

filter: Statementstmt 100% 32 / 32 Branchbran 50.0% 8 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 85.1% 46 / 54 CC 10 SCAR 16.3
CC
10
Cov
83%
_load_item_list
7.4
new
4.2
BEGIN
1.0
CRAP 5.1
« Dependency/Source.pm ↑ Summary Dependency.pm »
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<source|Algorithm::Dependency::Source> 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<Algorithm::Dependency::Source> methods.
48 #pod
49 #pod =cut
50  
51 7 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use Algorithm::Dependency::Source ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 7 our $VERSION = '1.112';
56 7 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<undef>
71 #pod otherwise.
72 #pod
73 #pod =cut
74  
75 9 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
76 9 my $class = shift;
77 9 ▶ my $filename = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or return undef 9 0 9
78 9 ▶ return undef unless -r $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename 0 9 9
79  
80 # Get the basic source object
81 9 ▶ my $self = $class->SUPER::new() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $class->SUPER::new or return undef 9 0 9
82  
83 # Add our arguments
84 9 $self->{filename} = $filename;
85  
86 9 $self;
87 }
88  
89  
90  
91  
92  
93 #####################################################################
94 # Private Methods
95  
96 9 ▶sub _load_item_list {
Subroutine _load_item_list: called CC=7 SCAR=20.0
97 9 my $self = shift;
98  
99 # Load the contents of the file
100 9 local $/ = undef;
101 9 ▶ open( FILE, $self->{filename} ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $self->{"filename"} or return undef 9 0 9
102 9 ▶ defined(my $source = <FILE>) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $source = readline FILE) or return undef 9 0 9
103 9 ▶ close( FILE ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
close FILE or return undef 9 0 9
104  
105 # Split, trim, clean and remove comments
106 9 my @content = grep { ! /^\s*(?:\#|$)/ }
107 split /\s*[\015\012][\s\015\012]*/, $source;
108  
109 # Parse and build the item list
110 9 my @Items = ();
111 9 foreach my $line ( @content ) {
112 # Split the line by non-word characters
113 94 my @sections = grep { length $_ } split /\W+/, $line;
114 94 ▶ return undef unless scalar @sections;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @sections 0 94 94
115  
116 # Create the new item
117 94 ▶ my $Item = Algorithm::Dependency::Item->new( @sections ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Item = "Algorithm::Dependency::Item"->new(@sections) or return undef 94 0 94
118 94 push @Items, $Item;
119 }
120  
121 9 \@Items;
122 }
123  
124 7 1;
125  
126 __END__
« Dependency/Source.pm ↑ Summary Dependency.pm »