line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::EX::Func; |
2
|
11
|
|
|
11
|
|
80887
|
use version; our $VERSION = version->declare("2.1.2"); |
|
11
|
|
|
|
|
2139
|
|
|
11
|
|
|
|
|
76
|
|
3
|
|
|
|
|
|
|
|
4
|
11
|
|
|
11
|
|
1018
|
use v5.14; |
|
11
|
|
|
|
|
43
|
|
5
|
11
|
|
|
11
|
|
66
|
use warnings; |
|
11
|
|
|
|
|
36
|
|
|
11
|
|
|
|
|
350
|
|
6
|
11
|
|
|
11
|
|
60
|
use Carp; |
|
11
|
|
|
|
|
47
|
|
|
11
|
|
|
|
|
714
|
|
7
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
100
|
use Exporter 'import'; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
950
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(); |
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw(parse_func callable); |
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); |
12
|
|
|
|
|
|
|
|
13
|
11
|
|
|
11
|
|
139
|
use Data::Dumper; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
559
|
|
14
|
|
|
|
|
|
|
|
15
|
11
|
|
|
11
|
|
71
|
use Scalar::Util qw(blessed); |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
2176
|
|
16
|
|
|
|
|
|
|
sub callable { |
17
|
0
|
|
|
0
|
0
|
0
|
my $target = shift; |
18
|
0
|
0
|
|
|
|
0
|
blessed $target and $target->can('call'); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
8
|
|
|
8
|
0
|
15
|
my $class = shift; |
23
|
8
|
|
|
|
|
53
|
my $obj = bless [ @_ ], $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub append { |
27
|
0
|
|
|
0
|
0
|
0
|
my $obj = shift; |
28
|
0
|
|
|
|
|
0
|
push @$obj, @_; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub call { |
32
|
8
|
|
|
8
|
0
|
150
|
my $obj = shift; |
33
|
8
|
|
|
|
|
15
|
unshift @_, @$obj; |
34
|
8
|
|
|
|
|
17
|
my $name = shift; |
35
|
|
|
|
|
|
|
|
36
|
11
|
|
|
11
|
|
78
|
no strict 'refs'; |
|
11
|
|
|
|
|
24
|
|
|
11
|
|
|
|
|
1095
|
|
37
|
8
|
|
|
|
|
73
|
goto &$name; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub closure { |
41
|
0
|
|
|
0
|
0
|
0
|
my $name = shift; |
42
|
0
|
|
|
|
|
0
|
my @argv = @_; |
43
|
|
|
|
|
|
|
sub { |
44
|
|
|
|
|
|
|
package main; # XXX |
45
|
11
|
|
|
11
|
|
92
|
no strict 'refs'; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
3755
|
|
46
|
0
|
|
|
0
|
|
0
|
unshift @_, @argv; |
47
|
0
|
|
|
|
|
0
|
goto &$name; |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
0
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
## |
52
|
|
|
|
|
|
|
## sub { ... } |
53
|
|
|
|
|
|
|
## funcname(arg1,arg2,arg3=val3) |
54
|
|
|
|
|
|
|
## funcname=arg1,arg2,arg3=val3 |
55
|
|
|
|
|
|
|
## |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $paren_re = qr/( \( (?: [^()]++ | (?-1) )*+ \) )/x; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub parse_func { |
60
|
8
|
100
|
|
8
|
0
|
25
|
my $opt = ref $_[0] eq 'HASH' ? shift : {}; |
61
|
8
|
|
|
|
|
17
|
local $_ = shift; |
62
|
8
|
|
|
|
|
15
|
my $noinline = $opt->{noinline}; |
63
|
8
|
|
|
|
|
22
|
my $pointer = $opt->{pointer}; |
64
|
8
|
|
|
|
|
17
|
my $caller = caller; |
65
|
8
|
|
66
|
|
|
27
|
my $pkg = $opt->{PACKAGE} || $caller; |
66
|
|
|
|
|
|
|
|
67
|
8
|
|
|
|
|
13
|
my @func; |
68
|
|
|
|
|
|
|
|
69
|
8
|
100
|
66
|
|
|
340
|
if (not $noinline and /^sub\s*{/) { |
|
|
50
|
|
|
|
|
|
70
|
2
|
|
|
|
|
151
|
my $sub = eval "package $pkg; $_"; |
71
|
2
|
50
|
|
|
|
8
|
if ($@) { |
72
|
0
|
|
|
|
|
0
|
warn "Error in function -- $_ --.\n"; |
73
|
0
|
|
|
|
|
0
|
die $@; |
74
|
|
|
|
|
|
|
} |
75
|
2
|
50
|
|
|
|
7
|
croak "Unexpected result from eval.\n" if ref $sub ne 'CODE'; |
76
|
2
|
|
|
|
|
5
|
@func = ($sub); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
elsif (m{^ &? (? [\w:]+ ) (? $paren_re | =.* )? $}x) { |
79
|
11
|
|
|
11
|
|
5313
|
my $name = $+{name}; |
|
11
|
|
|
|
|
4898
|
|
|
11
|
|
|
|
|
3447
|
|
|
6
|
|
|
|
|
47
|
|
80
|
6
|
|
50
|
|
|
58
|
my $arg = $+{arg} // ''; |
81
|
6
|
|
|
|
|
18
|
$arg =~ s/^ (?| \( (.*) \) | = (.*) ) $/$1/x; |
82
|
6
|
100
|
|
|
|
37
|
$name =~ s/^/$pkg\::/ unless $name =~ /::/; |
83
|
6
|
|
|
|
|
33
|
@func = ($name, arg2kvlist($arg)); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
0
|
|
|
|
|
0
|
return undef; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
8
|
50
|
|
|
|
37
|
__PACKAGE__->new( $pointer ? closure(@func) : @func ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
## |
93
|
|
|
|
|
|
|
## convert "key1,key2,key3=val3" to (key1=>1, key2=>1, key3=>"val3") |
94
|
|
|
|
|
|
|
## |
95
|
|
|
|
|
|
|
sub arg2kvlist { |
96
|
13
|
|
|
13
|
0
|
127
|
my @kv; |
97
|
13
|
|
|
|
|
47
|
for (@_) { |
98
|
13
|
|
|
|
|
315
|
while (/\G \s* |
99
|
|
|
|
|
|
|
(? [^,=]+ ) |
100
|
|
|
|
|
|
|
(?: = (? (?: [^,()]++ | ${paren_re} )*+ ) )? |
101
|
|
|
|
|
|
|
,*/xgc |
102
|
|
|
|
|
|
|
) { |
103
|
18
|
|
100
|
|
|
284
|
push @kv, ( $+{k}, $+{v} // 1 ); |
104
|
|
|
|
|
|
|
} |
105
|
13
|
|
100
|
|
|
49
|
my $pos = pos() // 0; |
106
|
13
|
50
|
|
|
|
43
|
if ($pos != length) { |
107
|
0
|
|
|
|
|
0
|
die "parse error in \"$_\".\n"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
13
|
|
|
|
|
105
|
@kv; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NAME |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Getopt::EX::Func - Function call interface |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
use Getopt::EX::Func qw(parse_func); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $func = parse_func(...); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
$func->call; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 DESCRIPTION |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
This module provides the way to create function call object used in |
131
|
|
|
|
|
|
|
L module set. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
If your script has B<--begin> option which tells the script to call |
134
|
|
|
|
|
|
|
specific function at the beginning of execution. You can do it like |
135
|
|
|
|
|
|
|
this: |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
use Getopt::EX::Func qw(parse_func); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
GetOptions("begin:s" => $opt_begin); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $func = parse_func($opt_begin); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$func->call; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Then script can be invoked like this: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
% example -Mfoo --begin 'repeat(debug,msg=hello,count=2)' |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
In this example, function C should be declared in module |
150
|
|
|
|
|
|
|
C or in start up rc file such as F<~/.examplerc>. Actual |
151
|
|
|
|
|
|
|
function call is done in this way: |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
repeat ( debug => 1, msg => 'hello', count => '2' ); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
As you can notice, arguments in the function call string is passed in |
156
|
|
|
|
|
|
|
I =E I style. Parameter without value (C in |
157
|
|
|
|
|
|
|
this example) is assigned value 1. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Function itself can be implemented like this: |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
our @EXPORT = qw(repeat); |
162
|
|
|
|
|
|
|
sub repeat { |
163
|
|
|
|
|
|
|
my %opt = @_; |
164
|
|
|
|
|
|
|
print Dumper \%opt if $opt{debug}; |
165
|
|
|
|
|
|
|
for (1 .. $opt{count}) { |
166
|
|
|
|
|
|
|
say $opt{msg}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
It is also possible to declare the function in-line: |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
% example -Mfoo --begin 'sub{ say "wahoo!!" }' |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Function C can be used because the function is executed under |
175
|
|
|
|
|
|
|
C |