blib/lib/Astro/App/Satpass2/Utils.pm

filter: Statementstmt 75.6% 211 / 279 Branchbran 59.2% 77 / 130 Conditioncond 42.8% 15 / 35 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 82.6% 38 / 46 Podpod 100% 9 / 9 totaltotal 68.5% 355 / 518 CC 90 SCAR 24.6
CC
90
Cov
68%
find_package_pod
64.0
__back_end_class_name_of_record
30.0
back_end
20.0
CRAP 11.8
« blib/lib/Astro/App/Satpass2/ParseTime/ISO8601.pm ↑ Summary blib/lib/Astro/App/Satpass2/Locale.pm »
1 package Astro::App::Satpass2::Utils;
2  
3 22 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use parent qw{ Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Getopt::Long 2.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Scalar::Util 1.26 qw{ blessed looks_like_number };
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use Text::ParseWords ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 our $VERSION = '0.059';
18  
19 22 our @CARP_NOT = qw{
20 Astro::App::Satpass2
21 Astro::App::Satpass2::Copier
22 Astro::App::Satpass2::Format
23 Astro::App::Satpass2::Format::Dump
24 Astro::App::Satpass2::Format::Template
25 Astro::App::Satpass2::Format::Template::Provider
26 Astro::App::Satpass2::FormatTime
27 Astro::App::Satpass2::FormatTime::Cldr
28 Astro::App::Satpass2::FormatTime::DateTime
29 Astro::App::Satpass2::FormatTime::DateTime::Cldr
30 Astro::App::Satpass2::FormatTime::DateTime::Strftime
31 Astro::App::Satpass2::FormatTime::POSIX::Strftime
32 Astro::App::Satpass2::FormatTime::Strftime
33 Astro::App::Satpass2::FormatValue
34 Astro::App::Satpass2::FormatValue::Formatter
35 Astro::App::Satpass2::Geocode
36 Astro::App::Satpass2::Geocode::OSM
37 Astro::App::Satpass2::Locale
38 Astro::App::Satpass2::Locale::C
39 Astro::App::Satpass2::Macro
40 Astro::App::Satpass2::Macro::Code
41 Astro::App::Satpass2::Macro::Command
42 Astro::App::Satpass2::ParseTime
43 Astro::App::Satpass2::ParseTime::Code
44 Astro::App::Satpass2::ParseTime::Date::Manip
45 Astro::App::Satpass2::ParseTime::Date::Manip::v5
46 Astro::App::Satpass2::ParseTime::Date::Manip::v6
47 Astro::App::Satpass2::ParseTime::ISO8601
48 Astro::App::Satpass2::Utils
49 Astro::App::Satpass2::Warner
50 Astro::App::Satpass2::Wrap::Array
51 };
52  
53 22 our @EXPORT_OK = qw{
54 __arguments
55 back_end
56 __back_end_class_name_of_record
57 expand_tilde find_package_pod
58 has_method instance load_package merge_hashes my_dist_config quoter
59 __date_manip_backend
60 __legal_options
61 __parse_class_and_args
62 ARRAY_REF CODE_REF HASH_REF REGEXP_REF SCALAR_REF
63 HAVE_DATETIME
64 OS_IS_WINDOWS
65 @CARP_NOT
66 };
67  
68 our %EXPORT_TAGS = (
69 484 os => [ grep { m/ \A OS_ /smx } @EXPORT_OK ],
70 22 ref => [ grep { m/ _REF \z /smx } @EXPORT_OK ],
71 );
72  
73 22 ▶use constant ARRAY_REF => ref [];
Subroutine BEGIN: called CC=1 SCAR=0.0
74 22 ▶use constant CODE_REF => ref sub {};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
75 22 ▶use constant HASH_REF => ref {};
Subroutine BEGIN: called CC=1 SCAR=0.0
76 22 ▶use constant REGEXP_REF => ref qr{};
Subroutine BEGIN: called CC=1 SCAR=0.0
77 22 ▶use constant SCALAR_REF => ref \1;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 {
80 22 local $@ = undef;
81  
82 22 ▶ use constant HAVE_DATETIME => eval {
Subroutine BEGIN: called CC=2 SCAR=7.9
Condition50%1/2
eval { do { require DateTime; require DateTime::TimeZone; 1 } } || 0
l!l
022
MC/DC0%0/1
eval { do { require DateTime; require DateTime::TimeZone; 1 } } || 0
eval { do { require DateTime; require DateTime::TimeZone; 1 } }
Truth table50%1/2
A
A= eval { do { require DateTime; require DateTime::TimeZone; 1 } }
Aresult
11
00
83 22 require DateTime;
84 0 require DateTime::TimeZone;
85 0 1;
86 22 } || 0;
87 }
88  
89 22 ▶use constant OS_IS_WINDOWS => {
Subroutine BEGIN: called CC=2 SCAR=7.0
90 dos => 1,
91 MSWin32 => 1,
92 22 ▶}->{$^O} || 0;
Condition50%1/2
{'dos', 1, 'MSWin32', 1}->{$^O} || 0
l!l
022
MC/DC0%0/1
{'dos', 1, 'MSWin32', 1}->{$^O} || 0
{'dos', 1, 'MSWin32', 1}->{$^O}
Truth table50%1/2
A
A= {'dos', 1, 'MSWin32', 1}->{$^O}
Aresult
11
00
93  
94 # Documented in POD
95  
96 {
97  
98 22 my @default_config = qw{default pass_through};
99  
100 360 ▶ sub __arguments {
Subroutine __arguments: called CC=12 SCAR=25.0
101 360 my ( $self, @args ) = @_;
102  
103 360 ▶ has_method( $self, '__parse_time_reset' )
Branch: 1 branch
BranchTrueFalseTotal
has_method($self, "__parse_time_reset") and $self->__parse_time_reset 342 18 360
104 and $self->__parse_time_reset();
105  
106 @args = map {
107 360 ▶ has_method( $_, 'dereference' ) ? $_->dereference() : $_
Branch: 1 branch
BranchTrueFalseTotal
has_method($_, 'dereference') ? : 1 1212 1213
108 } @args;
109  
110 360 my $code = \&{ ( caller 1 )[3] };
111  
112 360 ▶ if ( HASH_REF eq ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $args[0]) 7 353 360
113 7 my $opt = shift @args;
114 7 my @orig_keys = sort keys %{ $opt };
115 7 my $lgl = $self->__legal_options( $code, $opt );
116 7 my %opt_name = (
117 level1 => 1,
118 );
119 7 my $name;
120 7 foreach my $inx ( 0 .. $#$lgl ) {
121 44 ▶ if ( CODE_REF eq ref $lgl->[$inx] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $lgl->[$inx]) { } 6 38 44
122 6 ▶ defined $name
Branch: 1 branch
BranchTrueFalseTotal
defined $name or die "Bug - \$name undefined. Inx $inx; lgl @$lgl" 6 0 6
123 or die "Bug - \$name undefined. Inx $inx; lgl @$lgl";
124 6 ▶ if ( exists $opt->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{$name}) 2 4 6
125 2 $lgl->[$inx]->( $name, $opt->{$name} );
126 }
127 } else {
128 38 ( $name = $lgl->[ $inx ] ) =~ s/ \W .* //smx;
129 38 $opt_name{$name} = 1;
130 }
131 }
132 7 foreach my $key ( @orig_keys ) {
133 10 ▶ $opt_name{$key}
Branch: 1 branch
BranchTrueFalseTotal
$opt_name{$key} or __error_out($self, "wail", "Illegal option '${key}'") 10 0 10
134 or __error_out( $self, wail => "Illegal option '$key'" );
135 }
136 7 _apply_default( $self, $opt, \@args );
137 7 return( $self, $opt, @args );
138 }
139  
140 =begin comment
141  
142 my @data = caller(1);
143 my $code = \&{$data[3]};
144  
145 my ( $err, %opt );
146 my $lgl = $self->__get_attr($code, 'Verb') || [];
147 if ( @{ $lgl } && ':compute' eq $lgl->[0] ) {
148 my $method = $lgl->[1];
149 unless ( defined $method ) {
150 ( $method = $data[3] ) =~ s/ .* :: //smx;
151 $method = "__${method}_options";
152 }
153 $lgl = $self->$method( \%opt, $lgl );
154 }
155  
156 =end comment
157  
158 =cut
159  
160 353 my ( $err, %opt );
161 353 my $lgl = $self->__legal_options( $code, \%opt );
162  
163 0 ▶ local $SIG{__WARN__} = sub {$err = $_[0]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
164 353 ▶ my $config =
Condition50%1/2
$self->__get_attr($code, 'Configure') || \@default_config
l!l
0353
MC/DC0%0/1
$self->__get_attr($code, 'Configure') || \@default_config
$self->__get_attr($code, 'Configure')
Truth table50%1/2
A
A= $self->__get_attr($code, 'Configure')
Aresult
11
00
165 $self->__get_attr($code, 'Configure') || \@default_config;
166 353 my $go = Getopt::Long::Parser->new(config => $config);
167 353 ▶ if ( ! $go->getoptionsfromarray(
Branch: 1 branch
BranchTrueFalseTotal
unless ($go->getoptionsfromarray(\@args, \%opt, "default=s", @$lgl)) 0 353 353
168 \@args, \%opt, 'default=s', @$lgl) ) {
169 0 __error_out( $self, wail => $err );
170 }
171  
172 353 _apply_default( $self, \%opt, \@args );
173  
174 353 return ( $self, \%opt, @args );
175 }
176 }
177  
178 22 ▶sub __legal_options {
Subroutine __legal_options: called CC=7 SCAR=19.7
179 369 my ( $self, $code, $opt ) = @_;
180 0 ▶ $code ||= \&{ ( caller 1 )[3] };
Condition50%1/2
$code ||= \&{(caller 1)[3];}
l!l
3690
MC/DC0%0/1
$code ||= \&{(caller 1)[3];}
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
181 369 ▶ CODE_REF eq ref $code
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $code or __error_out($self, "weep", "$code not a CODE ref") 369 0 369
182 or __error_out( $self, weep => "$code not a CODE ref" );
183 369 ▶ $opt ||= {};
Condition100%2/2
$opt ||= {}
l!l
3609
MC/DC100%1/1
$opt ||= {}
$opt
Truth table100%2/2
A
A= $opt
Aresult
11
00
184 369 my $lgl = $self->__get_attr( $code, Verb => [] );
185 369 ▶ if ( @{ $lgl } && ':compute' eq $lgl->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$lgl;} and ":compute" eq $lgl->[0]) 24 345 369
Condition100%3/3
@{$lgl;} and ":compute" eq $lgl->[0]
!ll&&!rl&&r
19115424
MC/DC100%2/2
@{$lgl;} and ":compute" eq $lgl->[0]
@{$lgl;} ":compute" eq $lgl->[0]
Truth table100%3/3
A and B
A= @{$lgl;}B= ":compute" eq $lgl->[0]
ABresult
0X0
100
111
186 24 ▶ my $method = $lgl->[1]
Branch: 1 branch
BranchTrueFalseTotal
my $method = $lgl->[1] or __error_out($self, "weep", ":compute did not specify method") 24 0 24
187 or __error_out( $self, weep => ':compute did not specify method' );
188 24 $lgl = $self->$method( $opt, $lgl );
189 }
190 369 return $lgl;
191 }
192  
193 360 ▶sub _apply_default {
Subroutine _apply_default: called CC=8 SCAR=22.3
194 360 my ( $self, $opt, $args ) = @_;
195  
196 my $dflt = delete $opt->{default}
197 360 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dflt = delete $opt->{"default"} or return 2 358 360
198  
199 2 ▶ if ( ARRAY_REF eq ref $dflt ) {
Branch: 3 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $dflt) { } 0 2 2
elsif (ref $dflt) { } 0 2 2
elsif (my $code = $self->can('__tokenize')) { } 2 0 2
200 # Do nothing -- we already have what we want
201 } elsif ( ref $dflt ) {
202 0 __error_out( $self,
203 wail => "Invalid default specification $dflt" );
204 } elsif ( my $code = $self->can( '__tokenize' ) ) {
205 2 ( $dflt ) = $code->( $self, $dflt );
206 } else {
207 0 $dflt = [ Text::ParseWords::shellwords( $dflt ) ];
208 }
209  
210 2 foreach my $inx ( 0 .. $#$dflt ) {
211 14 ▶ defined $args->[$inx]
Branch: 1 branch
BranchTrueFalseTotal
defined $args->[$inx] and "" ne $args->[$inx] or $args->[$inx] = $dflt->[$inx] 8 6 14
Condition66%2/3
defined $args->[$inx] and "" ne $args->[$inx]
!ll&&!rl&&r
608
MC/DC50%1/2
defined $args->[$inx] and "" ne $args->[$inx]
defined $args->[$inx] "" ne $args->[$inx]
Truth table66%2/3
A and B
A= defined $args->[$inx]B= "" ne $args->[$inx]
ABresult
0X0
100
111
212 and '' ne $args->[$inx]
213 or $args->[$inx] = $dflt->[$inx];
214 }
215  
216 2 return;
217 }
218  
219 0 ▶sub back_end {
Subroutine back_end: not called CC=4 SCAR=30.0
Pod documented
220 0 my ( $self, @arg ) = @_;
221 0 ▶ if ( @arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg) { } 0 0 0
222 0 my ( $pkg, @cls_arg ) = ( $self->__parse_class_and_args(
223 $self->__back_end_default( $arg[0] ) ), @arg[ 1 .. $#arg ] );
224 0 my $cls = $self->load_package( { fatal => 1 }, $pkg,
225 'DateTime::Calendar' );
226 0 $self->__back_end_validate( $cls, @cls_arg );
227 $self->{_back_end} = {
228 0 arg => \@cls_arg,
229 class => $cls,
230 pkg => $pkg,
231 };
232 0 $self->{back_end} = shift @arg;
233 0 ▶ while ( @arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg) 0 0 0
234 0 my ( $name, $value ) = splice @arg, 0, 2;
235 0 $self->{back_end} .= ",$name=$value";
236 }
237 0 return $self;
238 } else {
239 wantarray
240 and return ( $self->{_back_end}{pkg}, @{
241 0 ▶ $self->{_back_end}{arg} } );
Branch: 1 branch
BranchTrueFalseTotal
wantarray and return $self->{"_back_end"}{"pkg"}, @{$self->{"_back_end"}{"arg"};} 0 0 0
242 0 return $self->{back_end};
243 }
244 }
245  
246 0 ▶sub __back_end_class_name_of_record {
Subroutine __back_end_class_name_of_record: not called CC=5 SCAR=34.0
247 0 my ( $self, $name ) = @_;
248 defined( my $back_end = $self->{_back_end}{class} )
249 0 ▶ or return $name;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $back_end = $self->{"_back_end"}{"class"}) or return $name 0 0 0
250 0 ▶ $back_end eq $self->__back_end_default()
Branch: 1 branch
BranchTrueFalseTotal
$back_end eq $self->__back_end_default and return $name 0 0 0
251 and return $name;
252 0 $back_end =~ s/ \A DateTime::Calendar:: //smx;
253 0 ▶ @{ $self->{_back_end}{arg} }
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"_back_end"}{"arg"};} or return "$name,back_end=$back_end" 0 0 0
254 or return "$name,back_end=$back_end";
255 0 my %dt_arg = @{ $self->{_back_end}{arg} };
256 0 foreach my $key ( sort keys %dt_arg ) {
257 0 $back_end .= ",$key=$dt_arg{$key}";
258 }
259 0 return "$name,back_end='$back_end'";
260 }
261  
262 # $backend = __date_manip_backend()
263 #
264 # This subroutine loads Date::Manip and returns the backend available,
265 # either 5 or 6. If Date::Manip can not be loaded it returns undef.
266 #
267 # The idea here is to return 6 if the O-O interface is available, and 5
268 # if it is not but Date::Manip is.
269  
270 10 ▶sub __date_manip_backend {
Subroutine __date_manip_backend: called CC=3 SCAR=18.4
271 10 ▶ load_package( 'Date::Manip' )
Branch: 1 branch
BranchTrueFalseTotal
load_package("Date::Manip") or return 0 10 10
272 or return;
273 0 ▶ Date::Manip->isa( 'Date::Manip::DM6' )
Branch: 1 branch
BranchTrueFalseTotal
"Date::Manip"->isa("Date::Manip::DM6") and return 6 0 0 0
274 and return 6;
275 0 return 5;
276 }
277  
278 {
279 22 my %method_to_sub = (
280 whinge => 'carp',
281 wail => 'croak',
282 weep => 'confess',
283 );
284  
285 # __error_out( $invocant, $method, @arg )
286 #
287 # $method must be 'carp', 'croak', or 'confess'.
288 #
289 # If the $invocant is a blessed reference having method $method,
290 # that method is called with @arg as arguments.
291 #
292 # Otherwise Carp is loaded, $method is mapped to the corresponding
293 # Carp subroutine, and that subroutine is called with @arg as
294 # arguments.
295 #
296 # If we have not thrown an exception as a result of all this, we
297 # just return.
298 11 ▶ sub __error_out {
Subroutine __error_out: called CC=5 SCAR=17.1
299 11 my ( $obj, $method, @arg ) = @_;
300 11 ▶ $method_to_sub{$method}
Branch: 1 branch
BranchTrueFalseTotal
$method_to_sub{$method} or $method = "weep" 11 0 11
301 or $method = 'weep';
302 11 ▶ if ( blessed( $obj ) && $obj->can( $method )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($obj) and $obj->can($method)) { } 4 7 11
Condition66%2/3
builtin::blessed($obj) and $obj->can($method)
!ll&&!rl&&r
704
MC/DC50%1/2
builtin::blessed($obj) and $obj->can($method)
builtin::blessed($obj) $obj->can($method)
Truth table66%2/3
A and B
A= builtin::blessed($obj)B= $obj->can($method)
ABresult
0X0
100
111
303 ) {
304 4 $obj->$method( @arg );
305 } else {
306 7 require Carp;
307 7 ▶ if ( my $code = Carp->can( $method_to_sub{ $method } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = 'Carp'->can($method_to_sub{$method})) { } 7 0 7
308 7 $code->( @arg );
309 } else {
310 0 Carp::confess( @arg );
311 }
312 }
313 0 return;
314 }
315 }
316  
317 22 ▶sub expand_tilde {
Subroutine expand_tilde: called CC=3 SCAR=11.0
Pod documented
318 40 my @args = @_;
319 40 ▶ my ( $self, $fn ) = @args > 1 ? @args : ( undef, @args );
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 30 10 40
320 40 ▶ defined $fn
Branch: 1 branch
BranchTrueFalseTotal
defined $fn and $fn =~ s[ \A ~ ( [^/]* ) ][_user_home_dir($self, $1);]emsx 40 0 40
321 22 and $fn =~ s{ \A ~ ( [^/]* ) }{ _user_home_dir( $self, $1 ) }smxe;
322 29 return $fn;
323 }
324  
325 {
326 my %special = (
327 2 ▶ '+' => sub { return Cwd::cwd() },
Subroutine __ANON__: called CC=1 SCAR=0.0
328 14 ▶ '~' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
329 14 return my_dist_config();
330 },
331 2 ▶ '' => sub { return File::HomeDir->my_home() },
Subroutine __ANON__: called CC=1 SCAR=0.0
332 22 );
333 # $dir = $self->_user_home_dir( $user );
334 #
335 # Find the home directory for the given user, croaking if this can
336 # not be done. If $user is '' or undef, returns the home directory
337 # for the current user.
338  
339 22 ▶ sub _user_home_dir {
Subroutine _user_home_dir: called CC=5 SCAR=16.1
340 22 my ( $self, $user ) = @_;
341 22 ▶ defined $user
Branch: 1 branch
BranchTrueFalseTotal
defined $user or $user = "" 22 0 22
342 or $user = '';
343  
344 22 ▶ if ( my $code = $special{$user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $special{$user}) { } 18 4 22
345 18 ▶ defined( my $special_dir = $code->( $user ) )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $special_dir = &$code($user)) or _wail($self, "Unable to find ~$user") 9 9 18
346 or _wail( $self, "Unable to find ~$user" );
347 9 return $special_dir;
348 } else {
349 4 ▶ defined( my $home_dir = File::HomeDir->users_home( $user ) )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $home_dir = "File::HomeDir"->users_home($user)) or _wail($self, "Unable to find home for $user") 2 2 4
350 or _wail( $self, "Unable to find home for $user" );
351 2 return $home_dir;
352 }
353 }
354 }
355  
356 22 ▶sub find_package_pod {
Subroutine find_package_pod: not called CC=8 SCAR=41.6
Pod documented
357 0 my ( $pkg ) = @_;
358 0 ( my $fn = $pkg ) =~ s{ :: }{/}smxg;
359 0 foreach my $dir ( @INC ) {
360 0 ▶ defined $dir
Branch: 1 branch
BranchTrueFalseTotal
defined $dir and not ref $dir and -d $dir and -x _ or next 0 0 0
Condition0%0/9
defined $dir and not ref $dir and -d $dir and -x _
!ll&&!rl&&r
000
defined $dir and not ref $dir and -d $dir
!ll&&!rl&&r
000
defined $dir and not ref $dir
!ll&&!rl&&r
000
MC/DC0%0/4
defined $dir and not ref $dir and -d $dir and -x _
defined $dir not ref $dir -d $dir -x _
Truth table0%0/5
A and not(B) and C and D
A= defined $dirB= not ref $dirC= -d $dirD= -x _
ABCDresult
0XXX0
10XX0
110X0
11100
11111
361 and not ref $dir
362 and -d $dir
363 and -x _
364 or next;
365 0 foreach my $sfx ( qw{ pod pm } ) {
366 0 my $path = "$dir/$fn.$sfx";
367 0 ▶ -r $path
Branch: 1 branch
BranchTrueFalseTotal
-r $path or next 0 0 0
368 or next;
369 0 return Cwd::abs_path( $path );
370 }
371 }
372 0 return;
373 }
374  
375 11 ▶sub _wail {
Subroutine _wail: called CC=1 SCAR=0.4
376 11 my ( $invocant, @msg ) = @_;
377 11 __error_out( $invocant, wail => @msg );
378 0 return; # We should never get here, but Perl::Critic does not
379 # know this.
380 }
381  
382 2751 ▶sub has_method {
Subroutine has_method: called CC=3 SCAR=11.0
Pod documented
383 2751 my ( $object, $method ) = @_;
384  
385 2751 ▶ ref $object or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $object or return 1006 1745 2751
386 1006 ▶ blessed( $object ) or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) or return 980 26 1006
387 980 return $object->can( $method );
388 }
389  
390 1508 ▶sub instance {
Subroutine instance: called CC=3 SCAR=11.0
Pod documented
391 1508 my ( $object, $class ) = @_;
392 1508 ▶ ref $object or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $object or return 1478 30 1508
393 1478 ▶ blessed( $object ) or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) or return 984 494 1478
394 984 return $object->isa( $class );
395 }
396  
397 223 ▶sub _get_my_lib {
Subroutine _get_my_lib: called CC=3 SCAR=15.1
398 223 my $my_lib = my_dist_config();
399 223 ▶ if ( defined $my_lib ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $my_lib) 0 223 223
400 0 $my_lib = File::Spec->catdir( $my_lib, 'lib' );
401 0 ▶ -d $my_lib
Branch: 1 branch
BranchTrueFalseTotal
-d $my_lib or $my_lib = undef 0 0 0
402 or $my_lib = undef;
403 }
404 223 return $my_lib;
405 }
406  
407 {
408 22 my %loaded;
409  
410 # CAVEAT:
411 #
412 # Unfortunately as things currently stand, the version needs to be
413 # maintained three places:
414 # - lib/Astro/App/Satpass2/Utils.pm
415 # - inc/My/Module/Recommend.pm
416 # - inc/My/Module/Test/App.pm
417 # These all need to stay the same. Sigh.
418 # Any such should be in xt/author/consistent_module_versions.t
419  
420 22 my %version = (
421 'DateTime::Calendar::Christian' => 0.06,
422 );
423  
424 # Expose the module version so we can test for consistent definition.
425 # IM(NS)HO the following annotation silences a false positive.
426 0 ▶ sub __module_version { ## no critic (RequireArgUnpacking)
Subroutine __module_version: not called CC=2 SCAR=17.9
427 0 my $module = $_[-1];
428 0 require Carp;
429 0 ▶ exists $version{$module}
Branch: 1 branch
BranchTrueFalseTotal
exists $version{$module} or Carp::confess("Bug - Module $module has no defined version") 0 0 0
430 or Carp::confess( "Bug - Module $module has no defined version" );
431 0 return $version{$module};
432 }
433  
434 # my %valid_complaint = map { $_ => 1 } qw{ whinge wail weep };
435  
436 244 ▶ sub load_package {
Subroutine load_package: called CC=15 SCAR=27.7
Pod documented
437 # my ( $module, @prefix ) = @_;
438 244 my @prefix = @_;
439 244 my $self;
440 244 ▶ blessed( $prefix[0] )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($prefix[0]) and $self = shift @prefix 50 194 244
441 and $self = shift @prefix;
442 244 ▶ my $opt = HASH_REF eq ref $prefix[0] ? shift @prefix : {};
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $prefix[0] ? : 70 174 244
443 244 my $module = shift @prefix;
444  
445 244 local @INC = @INC;
446  
447 244 ▶ my $use_lib = exists $opt->{lib} ? $opt->{lib} : _get_my_lib();
Branch: 1 branch
BranchTrueFalseTotal
exists $opt->{'lib'} ? : 21 223 244
448 244 ▶ if ( defined $use_lib ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $use_lib) 1 243 244
449 1 require lib;
450 1 lib->import( $use_lib );
451 }
452  
453 244 foreach ( $module, @prefix ) {
454 308 ▶ '' eq $_
Branch: 1 branch
BranchTrueFalseTotal
"" eq $_ and next 0 308 308
455 and next;
456 308 ▶ m/ \A [[:alpha:]]\w* (?: :: [[:alpha:]]\w* )* \z /smx
Branch: 1 branch
BranchTrueFalseTotal
/ \A [[:alpha:]]\w* (?: :: [[:alpha:]]\w* )* \z /msx and next 308 0 308
457 and next;
458  
459 0 ▶ __error_out( $self, $opt->{complaint} || 'weep',
Condition0%0/2
$opt->{'complaint'} || 'weep'
l!l
00
MC/DC0%0/1
$opt->{'complaint'} || 'weep'
$opt->{'complaint'}
Truth table0%0/2
A
A= $opt->{'complaint'}
Aresult
11
00
460 "Invalid package name '$_'",
461 );
462 }
463  
464 244 my $key = join ' ', $module, @prefix;
465 exists $loaded{$key}
466 244 ▶ and return $loaded{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $loaded{$key} and return $loaded{$key} 113 131 244
467  
468 131 local $@ = undef;
469  
470 131 push @prefix, '';
471 131 foreach my $pfx ( @prefix ) {
472 177 my $package = join '::', grep { $_ ne '' } $pfx, $module;
473 177 ▶ '' eq $package
Branch: 1 branch
BranchTrueFalseTotal
"" eq $package and next 0 177 177
474 and next;
475 177 ( my $fn = $package ) =~ s{ :: }{/}smxg;
476 177 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "$fn.pm"; 1 } } or next 81 96 177
477 177 require "$fn.pm"; ## no critic (RequireBarewordIncludes)
478 81 1;
479 } or next;
480  
481 not $version{$package}
482 81 ▶ or $package->VERSION( $version{$package} );
Branch: 1 branch
BranchTrueFalseTotal
$version{$package} and $package->VERSION($version{$package}) 0 81 81
483  
484 81 return ( $loaded{$key} = $package );
485 }
486  
487 50 ▶ if ( $opt->{fatal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"fatal"}) 0 50 50
488 0 __error_out( $self, $opt->{fatal}, "Can not load $module: $@" );
489 }
490  
491 50 $loaded{$key} = undef;
492  
493 50 return;
494 }
495 }
496  
497 # The Perl::Critic annotation on the following line should not (strictly
498 # speaking) be necessary - but Subroutines::RequireArgUnpacking does not
499 # understand the unpacking to be subject to the configuration
500 # allow_arg_unpacking = grep
501 22 ▶sub merge_hashes { ## no critic (RequireArgUnpacking)
Subroutine merge_hashes: not called CC=3 SCAR=23.0
Pod documented
502 0 my @args = grep { HASH_REF eq ref $_ } @_;
503 0 ▶ @args == 1
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 and return $args[0] 0 0 0
504 and return $args[0];
505 0 my %rslt;
506 0 foreach my $hash ( @args ) {
507 0 @rslt{ keys %{ $hash } } = values %{ $hash };
508 }
509 0 return \%rslt;
510 }
511  
512 22 ▶use constant MY_PACKAGE_NAME => 'Astro-App-Satpass2';
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 237 ▶sub my_dist_config {
Subroutine my_dist_config: called CC=3 SCAR=11.0
Pod documented
515 237 my ( $opt ) = @_;
516  
517 defined $ENV{ASTRO_APP_SATPASS2_CONFIG_DIR}
518 237 ▶ and return Cwd::abs_path( $ENV{ASTRO_APP_SATPASS2_CONFIG_DIR} );
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{"ASTRO_APP_SATPASS2_CONFIG_DIR"} and return Cwd::abs_path($ENV{"ASTRO_APP_SATPASS2_CONFIG_DIR"}) 3 234 237
519  
520 234 ▶ my $code = __PACKAGE__->can( "_my_dist_config_$^O" ) || \&_my_dist_config_;
Condition50%1/2
__PACKAGE__->can("_my_dist_config_$^O") || \&Astro::App::Satpass2::Utils::_my_dist_config_
l!l
0234
MC/DC0%0/1
__PACKAGE__->can("_my_dist_config_$^O") || \&Astro::App::Satpass2::Utils::_my_dist_config_
__PACKAGE__->can("_my_dist_config_$^O")
Truth table50%1/2
A
A= __PACKAGE__->can("_my_dist_config_$^O")
Aresult
11
00
521 234 return $code->( $opt );
522 }
523  
524 234 ▶sub _my_dist_config_ {
Subroutine _my_dist_config_: called CC=1 SCAR=0.0
525 234 my ( $opt ) = @_;
526 return File::HomeDir->my_dist_config(
527 MY_PACKAGE_NAME,
528 234 { create => $opt->{'create-directory'} },
529 );
530 }
531  
532 # Called dynamically by my_dist_config() if $^O is 'darwin'.
533 0 ▶sub _my_dist_config_darwin { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _my_dist_config_darwin: not called CC=2 SCAR=17.9
534 # my ( $opt ) = @_;
535 0 ▶ my $rslt = File::HomeDir->my_dist_data( MY_PACKAGE_NAME )
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = "File::HomeDir"->my_dist_data("Astro-App-Satpass2") or goto \&Astro::App::Satpass2::Utils::_my_dist_config_ 0 0 0
536 or goto &_my_dist_config_;
537 0 return $rslt;
538 }
539  
540 43 ▶sub __parse_class_and_args {
Subroutine __parse_class_and_args: called CC=7 SCAR=26.4
541 43 my ( $self, $arg, @rest ) = @_;
542 43 my ( $cls, @val ) =
543 Text::ParseWords::parse_line( qr{ , }smx, 0, $arg );
544 43 ▶ unless ( defined $cls &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cls and $cls =~ / \A [_[:alpha:]] \w* (?: :: \w+ )* \z /msx) 0 43 43
Condition33%1/3
defined $cls and $cls =~ / \A [_[:alpha:]] \w* (?: :: \w+ )* \z /msx
!ll&&!rl&&r
0043
MC/DC0%0/2
defined $cls and $cls =~ / \A [_[:alpha:]] \w* (?: :: \w+ )* \z /msx
defined $cls $cls =~ / \A [_[:alpha:]] \w* (?: :: \w+ )* \z /msx
Truth table33%1/3
A and B
A= defined $clsB= $cls =~ / \A [_[:alpha:]] \w* (?: :: \w+ )* \z /msx
ABresult
0X0
100
111
545 $cls =~ m/ \A [_[:alpha:]] \w* (?: :: \w+ )* \z /smx ) {
546 0 ▶ $cls = defined $cls ? "'$cls'" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $cls ? : 0 0 0
547 0 ▶ my $warner = $self->can( 'wail' ) ? $self : $self->warner();
Branch: 1 branch
BranchTrueFalseTotal
$self->can('wail') ? : 0 0 0
548 0 $warner->wail( "Invalid class name $cls" );
549 }
550 43 foreach ( @val ) {
551 4 ▶ m/ = /smx
Branch: 1 branch
BranchTrueFalseTotal
/ = /msx or $_ .= '=' 4 0 4
552 or $_ .= '=';
553 };
554 43 return ( $cls, ( map { split qr{ = }smx, $_, 2 } @val ), @rest );
555 }
556  
557 56 ▶sub quoter {
Subroutine quoter: called CC=2 SCAR=6.9
Pod documented
558 56 my @args = @_;
559 56 my @rslt = map { _quoter( $_ ) } @args;
560 56 ▶ return wantarray ? @rslt : join ' ', @rslt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 31 56
561 }
562  
563 142 ▶sub _quoter {
Subroutine _quoter: called CC=5 SCAR=16.2
564 142 my ( $string ) = @_;
565 142 ▶ return 'undef' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 142 142
566 142 ▶ return $string if looks_like_number ($string);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($string) 16 126 142
567 126 ▶ return q{''} unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 126 126
568 126 ▶ return $string unless $string =~ m/ [\s'"\$] /smx;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ / [\s'"\$] /msx 110 16 126
569 16 $string =~ s/ ( [\\'] ) /\\$1/smxg;
570 16 return qq{'$string'};
571 }
572  
573 22 1;
574  
575 __END__
« blib/lib/Astro/App/Satpass2/ParseTime/ISO8601.pm ↑ Summary blib/lib/Astro/App/Satpass2/Locale.pm »