blib/lib/Config/PFiles/Path.pm

filter: Statementstmt 94.9% 93 / 98 Branchbran 88.4% 23 / 26 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 91.4% 150 / 164 CC 20 SCAR 14.7
CC
20
Cov
92%
AUTOLOAD
9.6
__init
6.0
import
3.0
CRAP 4.3
↑ Summary
1 package Config::PFiles::Path;
2  
3 # ABSTRACT: manipulate PFILES path for IRAF Compatible parameter files
4  
5 12 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Sub::Uplevel ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 our $VERSION = '0.04';
14  
15 12 ▶use overload '""' => '_export' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 my %is_mutator
18 12 = map { $_ => 1 } qw( _append _prepend _replace _remove );
19  
20 12 our $AUTOLOAD;
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31 2 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
32 2 require Carp;
33 2 goto &Carp::croak;
34 }
35  
36 # allow the user to do thing when loading the package
37 11 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
38  
39 11 my $package = shift;
40  
41 11 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 6 5 11
42  
43 5 my $method = shift;
44  
45 _croak( "Can't call method '$method' in this context\n" )
46 5 ▶ unless $is_mutator{ "_$method" };
Branch: 1 branch
BranchTrueFalseTotal
unless $is_mutator{"_$method"} 1 4 5
47  
48 4 $AUTOLOAD = $method;
49 4 Sub::Uplevel::uplevel( 1, \&AUTOLOAD, $package, @_ );
50 }
51  
52 124 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=9 SCAR=22.6
53 124 (my $method = our $AUTOLOAD) =~ s/.*:://;
54  
55 # we don't have a DESTROY method, so ignore it.
56 124 ▶ return if $method =~ /DESTROY/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /DESTROY/ 43 81 124
57  
58 81 my $imethod = '_' . $method;
59  
60 81 my $subref = *{Symbol::qualify_to_ref($imethod,__PACKAGE__)}{CODE};
61  
62 # make sure it's an existing method
63 81 ▶ _croak( qq{Can't locate object method "$method" via package "},
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^_/ or not defined $subref 0 81 81
Condition33%1/3
$method =~ /^_/ or not defined $subref
l!l&&r!l&&!r
0081
MC/DC0%0/2
$method =~ /^_/ or not defined $subref
$method =~ /^_/ not defined $subref
Truth table33%1/3
A or not(B)
A= $method =~ /^_/B= not defined $subref
ABresult
1X1
011
000
64 __PACKAGE__, q{"} )
65 if $method =~ /^_/ || ! defined $subref;
66  
67  
68 # is this an object invocation?
69 81 ▶ if ( ref $_[0] && $_[0]->isa(__PACKAGE__) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa(__PACKAGE__)) 61 20 81
Condition66%2/3
ref $_[0] and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
20061
MC/DC50%1/2
ref $_[0] and $_[0]->isa(__PACKAGE__)
ref $_[0] $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $_[0]B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
70 {
71 61 goto &$imethod;
72 }
73  
74 # nope. create default object based on $ENV{PFILES} and replace
75 # the class name in the argument list with the new object
76 20 my $package = shift;
77 20 my $env = $package->new( $ENV{PFILES} );
78 20 unshift @_, $env;
79  
80  
81 # if the method will alter the path, make sure to update $ENV{PFILES}
82 # after it has been run
83 20 ▶ if ( $is_mutator{$imethod} )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_mutator{$imethod}) { } 17 3 20
84 {
85 # respect calling context
86 17 my $wantarray = wantarray();
87  
88 # void
89 17 ▶ if ( ! defined $wantarray )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $wantarray) { } 13 4 17
elsif ($wantarray) { } 4 0 4
90 {
91 13 Sub::Uplevel::uplevel( 1, $subref, @_ );
92 13 $ENV{PFILES} = $env->_export;
93 13 return;
94 }
95  
96 # list
97 elsif ( $wantarray)
98 {
99 4 my @results = Sub::Uplevel::uplevel( 1, $subref, @_ );
100 4 $ENV{PFILES} = $env->_export;
101 4 return @results;
102 }
103  
104 # scalar
105 else
106 {
107 0 my $result = Sub::Uplevel::uplevel( 1, $subref, @_ );
108 0 $ENV{PFILES} = $env->_export;
109 0 return $result;
110 }
111 }
112  
113 # nope, just execute the method
114 else
115 {
116 3 goto &$imethod;
117 }
118 }
119  
120 43 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
121 43 my ( $class, $pfiles ) = @_;
122  
123 43 my $self = bless {}, $class;
124  
125 43 $self->__init( $pfiles );
126  
127 42 return $self;
128 }
129  
130 43 ▶sub __init {
Subroutine __init: called CC=6 SCAR=17.9
131 43 my ( $self, $pfiles ) = @_;
132  
133 43 ▶ $pfiles ||= q{};
Condition100%2/2
$pfiles ||= ""
l!l
367
MC/DC100%1/1
$pfiles ||= ""
$pfiles
Truth table100%2/2
A
A= $pfiles
Aresult
11
00
134  
135 43 my %dirs;
136 43 @dirs{ qw( RW RO ) } =
137 $pfiles =~ /^
138 ([^;]*) # grab everything that's not a semicolon (RW)
139 (?:|;(.*)) # and everything that's after a semicolon (RO)
140 $/x;
141  
142 _croak( "illegal path: too many semi-colons: $pfiles\n" )
143 43 ▶ if defined $dirs{RO} && $dirs{RO} =~ /;/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dirs{"RO"} and $dirs{"RO"} =~ /;/ 1 42 43
Condition100%3/3
defined $dirs{"RO"} and $dirs{"RO"} =~ /;/
!ll&&!rl&&r
12301
MC/DC100%2/2
defined $dirs{"RO"} and $dirs{"RO"} =~ /;/
defined $dirs{"RO"} $dirs{"RO"} =~ /;/
Truth table100%3/3
A and B
A= defined $dirs{"RO"}B= $dirs{"RO"} =~ /;/
ABresult
0X0
100
111
144  
145 # split and store non-empty paths
146 88 ▶ $self->{$_} = [ grep { $_ ne '' } split( /:/, $dirs{$_} || q{} ) ]
Condition100%2/2
$dirs{$_} || ''
l!l
5826
MC/DC100%1/1
$dirs{$_} || ''
$dirs{$_}
Truth table100%2/2
A
A= $dirs{$_}
Aresult
11
00
147 42 for qw( RW RO );
148  
149 42 return;
150 }
151  
152 69 ▶sub __check_set {
Subroutine __check_set: called CC=2 SCAR=7.9
153 69 my ( $dir_set ) = shift;
154  
155 69 my $match;
156 69 ▶ unless ( ($match ) = $dir_set =~ /^(RW|RO)$/i )
Branch: 1 branch
BranchTrueFalseTotal
unless (($match) = $dir_set =~ /^(RW|RO)$/i) 0 69 69
157 {
158 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
159 0 _croak( "illegal value for directory set: $dir_set\n" )
160 }
161  
162 69 return uc($match);
163 }
164  
165 7 ▶sub _append {
Subroutine _append: called CC=1 SCAR=0.0
166 7 my ( $self, $dir_set, @dirs ) = @_;
167  
168 7 push @{$self->{__check_set($dir_set)}}, @dirs;
169  
170 7 return;
171 }
172  
173 8 ▶sub _prepend {
Subroutine _prepend: called CC=1 SCAR=0.0
174 8 my ( $self, $dir_set, @dirs ) = @_;
175  
176 8 unshift @{$self->{__check_set($dir_set)}}, @dirs;
177  
178 8 return;
179 }
180  
181 32 ▶sub _extract {
Subroutine _extract: called CC=1 SCAR=0.0
182 32 my ( $self, $dir_set ) = @_;
183  
184 32 return @{$self->{__check_set($dir_set)}};
185 }
186  
187 22 ▶sub _replace {
Subroutine _replace: called CC=2 SCAR=6.9
188 22 my ( $self, $dir_set, @dirs ) = @_;
189  
190 22 $dir_set = __check_set($dir_set);
191  
192 22 my @old = @{$self->{$dir_set}};
193  
194 22 $self->{$dir_set} = [ @dirs ];
195  
196 return @old
197 22 ▶ if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 8 14 22
198  
199 14 return;
200 }
201  
202 11 ▶sub _remove {
Subroutine _remove: called CC=1 SCAR=0.0
203 11 my ( $self, $dir_set ) = @_;
204  
205 11 return $self->replace( $dir_set );
206 }
207  
208 23 ▶sub _export {
Subroutine _export: called CC=3 SCAR=11.0
209 23 my ( $self ) = @_;
210  
211 # join together the non-empty directories in the sets;
212 my ( $rw, $ro ) =
213 23 map { join( q{:}, grep { $_ ne q{} } @{$self->{$_}} ) }
214 qw( RW RO );
215  
216 # construct a rational path
217 return
218 23 ▶ $rw eq q{} ? ";$ro"
Branch: 2 branches
BranchTrueFalseTotal
$rw eq '' ? : 6 17 23
$ro eq '' ? : 8 9 17
219 : $ro eq q{} ? $rw
220 : "$rw;$ro";
221 }
222  
223  
224 12 1;
225  
226 #
227 # This file is part of Config-PFiles-Path
228 #
229 # This software is Copyright (c) 2007 by Smithsonian Astrophysical Observatory.
230 #
231 # This is free software, licensed under:
232 #
233 # The GNU General Public License, Version 3, June 2007
234 #
235  
236 __END__
↑ Summary