blib/lib/Devel/Main.pm

filter: Statementstmt 95.2% 40 / 42 Branchbran 64.2% 9 / 14 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 76.5% 62 / 81 CC 13 SCAR 18.1
CC
13
Cov
81%
import
11.1
main_generator
3.0
__ANON__
3.0
CRAP 6.1
↑ Summary
1 # ABSTRACT: Syntactic sugar for a script's main routine
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Devel::Main;
6  
7 2 our $VERSION = 0.005;
8  
9 2 ▶sub import {
Subroutine import: called CC=9 SCAR=24.0
10 2 my $class = shift;
11 2 ▶ @_ = 'main' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
12
13 2 ▶ my $opts = ref($_[0]) ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
14 2 ▶ my $caller = $opts->{into} // scalar(caller($opts->{into_level} // 0));
Condition50%2/4
$opts->{"into"} // scalar caller($opts->{"into_level"} // 0)
l!l
02
$opts->{"into_level"} // 0
l!l
02
MC/DC0%0/1
$opts->{"into"} // scalar caller($opts->{"into_level"} // 0)
$opts->{"into"}
Truth table50%1/2
A
A= $opts->{"into"}
Aresult
11
00
15
16 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
17 2 my $name = shift;
18 2 ▶ my $args = ref($_[0]) ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
19 my $gen = $class->can("$name\_generator")
20 0 ▶ or do { require Carp; Carp::croak("$class does not export sub '$name'") };
Branch: 1 branch
BranchTrueFalseTotal
my $gen = $class->can("${name}_generator") or do { require Carp; Carp::croak("$class does not export sub '${name}'") } 2 0 2
21 2 my $code = $gen->( $class, $name, $args );
22
23 my $as = join '', grep defined, (
24 $args->{ -prefix },
25 ($args->{ -as } // $name),
26 $args->{ -suffix },
27 2 ▶ );
Condition33%1/3
$args->{"-as"} // $name
l!l&&r!l&&!r
020
MC/DC0%0/2
$args->{"-as"} // $name
$args->{"-as"} $name
Truth table33%1/3
A // B
A= $args->{"-as"}B= $name
ABresult
1X1
011
000
28
29 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 *{"$caller\::$as"} = $code;
31 }
32 }
33  
34 2 ▶sub main_generator {
Subroutine main_generator: called CC=3 SCAR=11.0
Pod undocumented
35 2 my ( $class, $name, $args ) = @_;
36  
37 2 ▶ my $run_sub_name = $args->{'run_sub_name'} // "run_$name";
Condition50%1/2
$args->{"run_sub_name"} // "run_$name"
l!l
02
MC/DC0%0/1
$args->{"run_sub_name"} // "run_$name"
$args->{"run_sub_name"}
Truth table50%1/2
A
A= $args->{"run_sub_name"}
Aresult
11
00
38 2 ▶ my $exit = $args->{'exit'} // 1;
Condition50%1/2
$args->{"exit"} // 1
l!l
02
MC/DC0%0/1
$args->{"exit"} // 1
$args->{"exit"}
Truth table50%1/2
A
A= $args->{"exit"}
Aresult
11
00
39  
40 2 ▶ return sub (&) {
Subroutine __ANON__: called CC=3 SCAR=11.0
41 2 my ($main_sub) = @_;
42  
43 # If we're called from a script, run main and exit
44 2 ▶ if ( !defined caller(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined caller 1) { } 1 1 2
45 1 $main_sub->();
46 1 ▶ exit(0) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 1 0 1
47 }
48  
49 # Otherwise, create a sub that turns its arguments into @ARGV
50 else {
51 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 my $package = caller;
53 1 ▶ *{"${package}::$run_sub_name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 local @ARGV = @_;
55 1 return $main_sub->();
56 1 };
57  
58 # Return 1 to make the script pass 'require'
59 1 return 1;
60 }
61 2 };
62 }
63  
64 2 1;
65  
66 __END__
↑ Summary