line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: Syntactic sugar for a script's main routine |
2
|
2
|
|
|
2
|
|
41189
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
79
|
|
3
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
787
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Devel::Main; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = 0.005; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub import { |
10
|
2
|
|
|
2
|
|
15
|
my $class = shift; |
11
|
2
|
50
|
|
|
|
10
|
@_ = 'main' unless @_; |
12
|
|
|
|
|
|
|
|
13
|
2
|
50
|
|
|
|
10
|
my $opts = ref($_[0]) ? shift : {}; |
14
|
2
|
|
50
|
|
|
25
|
my $caller = $opts->{into} // scalar(caller($opts->{into_level} // 0)); |
|
|
|
50
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
8
|
while (@_) { |
17
|
2
|
|
|
|
|
8
|
my $name = shift; |
18
|
2
|
50
|
|
|
|
8
|
my $args = ref($_[0]) ? shift : {}; |
19
|
|
|
|
|
|
|
my $gen = $class->can("$name\_generator") |
20
|
2
|
50
|
|
|
|
28
|
or do { require Carp; Carp::croak("$class does not export sub '$name'") }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
21
|
2
|
|
|
|
|
7
|
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
|
|
33
|
|
|
36
|
); |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
|
14
|
no strict 'refs'; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
372
|
|
30
|
2
|
|
|
|
|
4
|
*{"$caller\::$as"} = $code; |
|
2
|
|
|
|
|
3032
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub main_generator { |
35
|
2
|
|
|
2
|
0
|
5
|
my ( $class, $name, $args ) = @_; |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
33
|
|
|
9
|
my $run_sub_name = $args->{'run_sub_name'} // "run_$name"; |
38
|
2
|
|
50
|
|
|
15
|
my $exit = $args->{'exit'} // 1; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return sub (&) { |
41
|
2
|
|
|
2
|
|
21
|
my ($main_sub) = @_; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# If we're called from a script, run main and exit |
44
|
2
|
100
|
|
|
|
8
|
if ( !defined caller(1) ) { |
45
|
1
|
|
|
|
|
3
|
$main_sub->(); |
46
|
1
|
50
|
|
|
|
916
|
exit(0) if $exit; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Otherwise, create a sub that turns its arguments into @ARGV |
50
|
|
|
|
|
|
|
else { |
51
|
2
|
|
|
2
|
|
10
|
no strict 'refs'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
275
|
|
52
|
1
|
|
|
|
|
2
|
my $package = caller; |
53
|
1
|
|
|
|
|
6
|
*{"${package}::$run_sub_name"} = sub { |
54
|
1
|
|
|
1
|
|
2368
|
local @ARGV = @_; |
55
|
1
|
|
|
|
|
5
|
return $main_sub->(); |
56
|
1
|
|
|
|
|
3
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Return 1 to make the script pass 'require' |
59
|
1
|
|
|
|
|
7
|
return 1; |
60
|
|
|
|
|
|
|
} |
61
|
2
|
|
|
|
|
14
|
}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
__END__ |