| 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
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | my $opts = ref($_[0]) ? shift : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 14 | 2 | ▶ | my $caller = $opts->{into} // scalar(caller($opts->{into_level} // 0)); | ||||||||||||||||||
|
Condition50%2/4
$opts->{"into"} // scalar caller($opts->{"into_level"} // 0)
$opts->{"into_level"} // 0
MC/DC0%0/1
$opts->{"into"} // scalar caller($opts->{"into_level"} // 0)
$opts->{"into"}
Truth table50%1/2
A
A= $opts->{"into"}
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | ▶ | while (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 2 | my $name = shift; | |||||||||||||||||||
| 18 | 2 | ▶ | my $args = ref($_[0]) ? shift : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | my $gen = $class->can("$name\_generator") | ||||||||||||||||||||
| 20 | 0 | ▶ | or do { require Carp; Carp::croak("$class does not export sub '$name'") }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
MC/DC0%0/2
$args->{"-as"} // $name
$args->{"-as"}
$name
Truth table33%1/3
A // B
A= $args->{"-as"}B= $name
| |||||||||||||||||||||
| 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"
MC/DC0%0/1
$args->{"run_sub_name"} // "run_$name"
$args->{"run_sub_name"}
Truth table50%1/2
A
A= $args->{"run_sub_name"}
| |||||||||||||||||||||
| 38 | 2 | ▶ | my $exit = $args->{'exit'} // 1; | ||||||||||||||||||
|
Condition50%1/2
$args->{"exit"} // 1
MC/DC0%0/1
$args->{"exit"} // 1
$args->{"exit"}
Truth table50%1/2
A
A= $args->{"exit"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 45 | 1 | $main_sub->(); | |||||||||||||||||||
| 46 | 1 | ▶ | exit(0) if $exit; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||