Version.pm

filter: Statementstmt 15.3% 20 / 130 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 7 totaltotal 9.4% 25 / 266 CC 52 SCAR 51.1
CC
52
Cov
9%
new
420.0
__ANON__
132.0
locate_descendants
56.0
CRAP 165.5
↑ Summary cp_version.pl »
1 package File::Version;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9 1 our @ISA = qw(Exporter);
10 1 our @EXPORT = qw(); # nothing to export, this is OO
11 1 our $VERSION = '0.02';
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # file-private functions
15  
16 0 ▶my $recursive_find = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
17 0 my $self = shift;
18 0 my $regex = shift;
19 0 my($loc_recursive, $rec_depth, @matches);
20 0 ▶ $loc_recursive = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
21 0 my $path = shift;
22 0 ▶ return if $self->{RECURSION_DEPTH} && (scalar @$path > $rec_depth);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"RECURSION_DEPTH"} and scalar @$path > $rec_depth 0 0 0
Condition0%0/3
$self->{"RECURSION_DEPTH"} and scalar @$path > $rec_depth
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"RECURSION_DEPTH"} and scalar @$path > $rec_depth
$self->{"RECURSION_DEPTH"} scalar @$path > $rec_depth
Truth table0%0/3
A and B
A= $self->{"RECURSION_DEPTH"}B= scalar @$path > $rec_depth
ABresult
0X0
100
111
23 0 my $str_path = join('/', @$path);
24 0 ▶ substr($str_path, 0, 0) = '/' unless $str_path =~ /^\.+\/?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str_path =~ m[^\.+/?] 0 0 0
25 0 opendir(PH, $str_path);
26 0 my @files = grep(!/^\.\.?$/, readdir(PH));
27 #weed out symbolic links unless FOLLOW_SYMBOLIC is true
28 0 ▶ @files = grep( !( -l $_ ), @files) unless $self->{FOLLOW_SYMBOLIC};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FOLLOW_SYMBOLIC"} 0 0 0
29 0 closedir(PH);
30 0 for(@files) {
31 0 my @temp = @$path;
32 0 push(@temp, $_);
33 0 my $string_loc = join('/', @temp);
34 0 ▶ substr($string_loc, 0, 0) = '/' unless $string_loc =~ /^\.+\/?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string_loc =~ m[^\.+/?] 0 0 0
35 0 ▶ next unless (-r $string_loc);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $string_loc 0 0 0
36 0 ▶ if( -f $string_loc && /$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $string_loc and /$regex/) 0 0 0
Condition0%0/3
-f $string_loc and /$regex/
!ll&&!rl&&r
000
MC/DC0%0/2
-f $string_loc and /$regex/
-f $string_loc /$regex/
Truth table0%0/3
A and B
A= -f $string_locB= /$regex/
ABresult
0X0
100
111
37 0 push(@matches, $string_loc);
38 }
39 0 ▶ if( -d $string_loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $string_loc) 0 0 0
40 0 &$loc_recursive(\@temp);
41 }
42 }
43 0 };
44  
45 0 for(@ { $self->{SEARCH_PATHS} }) {
46
47 0 my @dir_parts = grep(/./, split '/');
48 0 $rec_depth = ($self->{RECURSION_DEPTH} + scalar @dir_parts);
49 0 &$loc_recursive([ @dir_parts ]);
50 }
51 0 ▶ @matches ? return \@matches : return;
Branch: 1 branch
BranchTrueFalseTotal
@matches ? : 0 0 0
52 1 };
53  
54 # constructor
55  
56 0 ▶sub new {
Subroutine new: not called CC=20 SCAR=60.4
Pod undocumented
57 0 my $invocant = shift;
58 0 ▶ my $class = ref($invocant) || $invocant;
Condition0%0/3
ref $invocant || $invocant
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table0%0/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
59 0 my $self = { SEARCH_PATHS => [ qw(.) ], # search current directory
60 RECURSION_DEPTH => 0,
61 FOLLOW_SYMBOLIC => 0, # default: don't follow symlinks
62 };
63 # validate user supplied arguments
64 0 my %args = @_;
65 0 ▶ croak( "Invalid file" ) unless( $args{FILE} && -f $args{FILE} && -r _);
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"FILE"} and -f $args{"FILE"} and -r _ 0 0 0
Condition0%0/6
$args{"FILE"} and -f $args{"FILE"} and -r _
!ll&&!rl&&r
000
$args{"FILE"} and -f $args{"FILE"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"FILE"} and -f $args{"FILE"} and -r _
$args{"FILE"} -f $args{"FILE"} -r _
Truth table0%0/4
A and B and C
A= $args{"FILE"}B= -f $args{"FILE"}C= -r _
ABCresult
0XX0
10X0
1100
1111
66 0 ▶ if($args{FILE} =~ /^(.*\/)?((?:\d+_)+?)(?:0_)*_(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'FILE'} =~ m[^(.*/)?((?:\d+_)+?)(?:0_)*_(.+)$]) { } 0 0 0
elsif ($args{'FILE'} =~ m[^(.*/)?(.+)$]) { } 0 0 0
67 0 ▶ $self->{FILE} = $3 or croak( "Invalid file.\n" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"FILE"} = $3 or croak("Invalid file.\n") 0 0 0
68 0 ▶ $self->{VERSION} = $2 || '';
Condition0%0/2
$2 || ''
l!l
00
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
69 }
70 elsif($args{FILE} =~ /^(.*\/)?(.+)$/) {
71 0 ▶ $self->{FILE} = $2 or croak( "Invalid file.\n" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"FILE"} = $2 or croak("Invalid file.\n") 0 0 0
72 0 $self->{VERSION} = '';
73 }
74 0 else { croak( "Invalid file" ) };
75 0 ▶ $self->{FILE_PATH} = $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
76 0 ▶ $self->{WHOLE} = $& or croak( "Invalid file.\n" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"WHOLE"} = $& or croak("Invalid file.\n") 0 0 0
77  
78 0 ▶ if ( $args{SEARCH_PATHS} && @{ $args{SEARCH_PATHS} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"SEARCH_PATHS"} and @{$args{"SEARCH_PATHS"};}) 0 0 0
Condition0%0/3
$args{"SEARCH_PATHS"} and @{$args{"SEARCH_PATHS"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"SEARCH_PATHS"} and @{$args{"SEARCH_PATHS"};}
$args{"SEARCH_PATHS"} @{$args{"SEARCH_PATHS"};}
Truth table0%0/3
A and B
A= $args{"SEARCH_PATHS"}B= @{$args{"SEARCH_PATHS"};}
ABresult
0X0
100
111
79 0 for(@{ $args{SEARCH_PATHS} }) {
80 0 ▶ if( -d && -r) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and -r $_) { } 0 0 0
Condition0%0/3
-d $_ and -r $_
!ll&&!rl&&r
000
MC/DC0%0/2
-d $_ and -r $_
-d $_ -r $_
Truth table0%0/3
A and B
A= -d $_B= -r $_
ABresult
0X0
100
111
81 0 push @{ $self->{SEARCH_PATHS} }, $_;
82 }
83 else {
84 0 carp ( "Directory does not exist: $_\n");
85 }
86 }
87 }
88 0 ▶ if ($args{RECURSION_DEPTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"RECURSION_DEPTH"}) 0 0 0
89 croak ( "Invalid RECURSION_DEPTH: $args{RECURSION_DEPTH}\n")
90 0 ▶ unless $args{RECURSION_DEPTH} =~ /^[+]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"RECURSION_DEPTH"} =~ /^[+]?\d+$/ 0 0 0
91 0 $self->{RECURSION_DEPTH} = $args{RECURSION_DEPTH};
92 }
93 0 ▶ $self->{FOLLOW_SYMBOLIC} = $args{FOLLOW_SYMBOLIC} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'FOLLOW_SYMBOLIC'} ? : 0 0 0
94 0 bless $self, $class;
95 0 return $self;
96 }
97  
98 # OO functions
99  
100 0 ▶sub recursion_depth {
Subroutine recursion_depth: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my $self = shift;
102 0 ▶ if(@_) { $self->{RECURSION_DEPTH} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
103 0 return $self->{RECURSION_DEPTH};
104 }
105  
106 0 ▶sub follow_symbolic {
Subroutine follow_symbolic: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $self = shift;
108 0 my %args = @_;
109 0 ▶ $self->{FOLLOW_SYMBOLIC} = $args{FOLLOW_SYMBOLIC} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'FOLLOW_SYMBOLIC'} ? : 0 0 0
110 0 return $self->{FOLLOW_SYMBOLIC};
111 }
112  
113 0 ▶sub search_paths {
Subroutine search_paths: not called CC=5 SCAR=34.0
Pod undocumented
114 0 my $self = shift;
115 0 my %args = @_;
116 0 ▶ if (@{ $args{SEARCH_PATHS} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args{'SEARCH_PATHS'};}) { } 0 0 0
117 0 for(@{ $args{SEARCH_PATHS} }) {
118 0 ▶ if( -d && -r) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and -r $_) { } 0 0 0
Condition0%0/3
-d $_ and -r $_
!ll&&!rl&&r
000
MC/DC0%0/2
-d $_ and -r $_
-d $_ -r $_
Truth table0%0/3
A and B
A= -d $_B= -r $_
ABresult
0X0
100
111
119 0 push @{ $self->{SEARCH_PATHS} }, $_;
120 }
121 else {
122 0 carp ( "SEARCH_PATHS not set: $_\n");
123 }
124 }
125 }
126 0 else { carp ( "SEARCH_PATHS not set.\n"); }
127 0 return $self->{SEARCH_PATHS};
128 }
129  
130 0 ▶sub locate_ancestors {
Subroutine locate_ancestors: not called CC=5 SCAR=34.0
Pod undocumented
131 0 my $self = shift;
132 0 my @gens;
133 0 @gens = split('_', $self->{VERSION});
134 0 my $gen_count = 0;
135 0 ▶ while(@gens) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gens) 0 0 0
136 0 my $match;
137 0 my $regex = join('_', @gens) . "_(?:0_)*_$self->{FILE}";
138 0 ▶ $regex = eval { qr/$regex/ } || croak "Invalid pattern.\n";
Condition0%0/2
eval { do { qr/$regex/ } } || croak("Invalid pattern.\n")
l!l
00
MC/DC0%0/1
eval { do { qr/$regex/ } } || croak("Invalid pattern.\n")
eval { do { qr/$regex/ } }
Truth table0%0/2
A
A= eval { do { qr/$regex/ } }
Aresult
11
00
139 0 ▶ ($match = &$recursive_find($self, $regex)) && do {
Branch: 1 branch
BranchTrueFalseTotal
$match = &$recursive_find($self, $regex) and do { foreach $_ (@$match) { ++$self->{"ANCESTORS"}{$gen_count}{$_}; } } 0 0 0
140 0 for(@$match) { $self->{ANCESTORS}{$gen_count}{$_}++; }
141 };
142 0 $gen_count++;
143 0 pop(@gens);
144 };
145 0 return $self->{ANCESTORS};
146 }
147  
148 0 ▶sub locate_descendants {
Subroutine locate_descendants: not called CC=7 SCAR=40.3
Pod undocumented
149 0 my $self = shift;
150 0 my $match;
151 0 ▶ my $regex = eval { qr/^$self->{VERSION}(?:\d+_)+?(?:0_)*_$self->{FILE}/ }
Condition0%0/2
eval { do { qr/^$self->{'VERSION'}(?:\d+_)+?(?:0_)*_$self->{'FILE'}/ } } || croak("Invalid pattern.\n")
l!l
00
MC/DC0%0/1
eval { do { qr/^$self->{'VERSION'}(?:\d+_)+?(?:0_)*_$self->{'FILE'}/ } } || croak("Invalid pattern.\n")
eval { do { qr/^$self->{'VERSION'}(?:\d+_)+?(?:0_)*_$self->{'FILE'}/ } }
Truth table0%0/2
A
A= eval { do { qr/^$self->{'VERSION'}(?:\d+_)+?(?:0_)*_$self->{'FILE'}/ } }
Aresult
11
00
152 || croak "Invalid pattern.\n";
153 0 ▶ ($match = &$recursive_find($self, $regex)) && do {
Branch: 1 branch
BranchTrueFalseTotal
$match = &$recursive_find($self, $regex) and do { foreach $_ (@$match) { if (my($gen) = /$self->{"VERSION"}((\d+_)+?)(?:0_)*_/) { my $count = $gen =~ tr/_//; (my $last = $+) =~ s/\D//g if $+; $self->{"DESCENDANTS"}{$count}{$_} = $last || 0; } ; } } 0 0 0
154 0 for(@$match) {
155 0 ▶ if( my($gen) = /$self->{VERSION}((\d+_)+?)(?:0_)*_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($gen) = /$self->{"VERSION"}((\d+_)+?)(?:0_)*_/) 0 0 0
156 0 my $count = ($gen =~ tr/\_//);
157 0 ▶ (my $last = $+) =~ s/\D//g if $+;
Branch: 1 branch
BranchTrueFalseTotal
if $+ 0 0 0
158 0 ▶ $self->{DESCENDANTS}{$count}{$_} = $last || 0;
Condition0%0/2
$last || 0
l!l
00
MC/DC0%0/1
$last || 0
$last
Truth table0%0/2
A
A= $last
Aresult
11
00
159 }
160 }
161 };
162 0 return $self->{DESCENDANTS};
163 };
164  
165 0 ▶sub next_version {
Subroutine next_version: not called CC=5 SCAR=34.0
Pod undocumented
166 0 my $self = shift;
167 0 my $high = 0;
168 0 ▶ &locate_descendants($self) unless $self->{DESCENDANTS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"DESCENDANTS"} 0 0 0
169 0 ▶ $self->{DESCENDANTS} && do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DESCENDANTS"} and do { foreach $_ (keys %{$$self{"DESCENDANTS"}{1};}) { $high = $self->{"DESCENDANTS"}{1}{$_} if $high < $self->{"DESCENDANTS"}{1}{$_}; } } 0 0 0
170 0 for(keys(% { $self->{DESCENDANTS}{1} } )) {
171 0 ▶ $high = $self->{DESCENDANTS}{1}{$_} if $high < $self->{DESCENDANTS}{1}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $high < $self->{"DESCENDANTS"}{1}{$_} 0 0 0
172 }
173 };
174 0 $self->{NEXT_VERSION} = $self->{VERSION} . ++$high . "__$self->{FILE}";
175 0 return $self->{NEXT_VERSION};
176 }
177  
178 1 1;
179 __END__
↑ Summary cp_version.pl »