EX/Module.pm

filter: Statementstmt 76.4% 237 / 310 Branchbran 60.8% 73 / 120 Conditioncond 51.2% 20 / 39 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 84.9% 45 / 53 Podpod 56.0% 14 / 25 totaltotal 69.0% 392 / 568 CC 83 SCAR 25.3
CC
83
Cov
70%
parseline
38.7
configure
22.8
autoload
20.0
CRAP 12.6
« EX/Numbers.pm ↑ Summary EX/Loader.pm »
1 package Getopt::EX::Module;
2  
3 9 our $VERSION = "3.03";
4  
5 9 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 our @EXPORT = qw();
11 9 our %EXPORT_TAGS = ( );
12 9 our @EXPORT_OK = qw();
13  
14 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use List::Util qw(first pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 22 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
21 22 my $class = shift;
22 22 my $obj = bless {
23 Module => undef,
24 Base => undef,
25 Mode => { FUNCTION => 0, WILDCARD => 0 },
26 Define => [],
27 Expand => [],
28 Option => [],
29 Builtin => [],
30 Automod => [],
31 Autoload => {},
32 Call => [],
33 Help => [],
34 }, $class;
35  
36 22 ▶ $obj->configure(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 22 0 22
37  
38 12 $obj;
39 }
40  
41 22 ▶sub configure {
Subroutine configure: called CC=18 SCAR=31.3
Pod documented
42 22 my $obj = shift;
43 22 my %opt = @_;
44  
45 22 ▶ if (my $base = delete $opt{BASECLASS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = delete $opt{"BASECLASS"}) 22 0 22
46 22 $obj->{Base} = $base;
47 }
48  
49 22 ▶ if (my $file = delete $opt{FILE}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $file = delete $opt{'FILE'}) { } 2 20 22
elsif (my $module = delete $opt{'MODULE'}) { } 20 0 20
50 2 ▶ open my $fh, "<:encoding(utf8)", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $file or croak("Can't open file '${file}': $!") 2 0 2
51 or croak "Can't open file '$file': $!";
52 2 $obj->module($file);
53 2 $obj->readrc($fh);
54 }
55 elsif (my $module = delete $opt{MODULE}) {
56 20 ▶ my $pkg = $opt{PACKAGE} || 'main';
Condition50%1/2
$opt{'PACKAGE'} || 'main'
l!l
020
MC/DC0%0/1
$opt{'PACKAGE'} || 'main'
$opt{'PACKAGE'}
Truth table50%1/2
A
A= $opt{'PACKAGE'}
Aresult
11
00
57 20 my @base = do {
58 20 ▶ if (ref $obj->{Base} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{'Base'} eq 'ARRAY') { } 2 18 20
59 2 @{$obj->{Base}};
60 } else {
61 18 ▶ ($obj->{Base} // '');
Condition50%1/2
$obj->{"Base"} // ""
l!l
180
MC/DC0%0/1
$obj->{"Base"} // ""
$obj->{"Base"}
Truth table50%1/2
A
A= $obj->{"Base"}
Aresult
11
00
62 }
63 };
64 20 ▶ while (@base) {
Branch: 1 branch
BranchTrueFalseTotal
if (@base) 23 0 23
65 23 my $base = shift @base;
66 23 ▶ my $mod = $base ? "$base\::$module" : $module;
Branch: 1 branch
BranchTrueFalseTotal
$base ? : 22 1 23
67 23 my $path = $mod =~ s{::}{/}gr . ".pm";
68 23 ▶ eval "package $pkg; use $mod;";
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
69 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 13 10 23
70 13 ▶ next if @base and $@ =~ /Can't locate \Q$path\E/;
Branch: 1 branch
BranchTrueFalseTotal
if @base and $@ =~ /Can't locate \Q$path\E/u 3 10 13
Condition66%2/3
@base and $@ =~ /Can't locate \Q$path\E/u
!ll&&!rl&&r
1003
MC/DC50%1/2
@base and $@ =~ /Can't locate \Q$path\E/u
@base $@ =~ /Can't locate \Q$path\E/u
Truth table66%2/3
A and B
A= @baseB= $@ =~ /Can't locate \Q$path\E/u
ABresult
0X0
100
111
71 10 croak "$mod: $@";
72 }
73 10 $obj->module($mod);
74 10 $obj->define('__PACKAGE__' => $mod);
75 10 local *data = "$mod\::DATA";
76 10 ▶ if (not eof *data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof data) 10 0 10
77 10 my $pos = tell *data;
78 10 $obj->readrc(*data);
79 # recover position in case called multiple times
80 10 ▶ seek *data, $pos, 0 or die "seek: $!" if $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= 0 10 0 10
Condition40%2/5
$pos >= 0 && (seek(*data, $pos, 0) || die("seek: $!"))
!ll&&!rl&&r
0100
seek *data, $pos, 0 or die "seek: $!"
l!l
100
MC/DC0%0/2
$pos >= 0 && (seek(*data, $pos, 0) || die("seek: $!"))
$pos >= 0 seek *data, $pos, 0
Truth table0%0/3
A && B
A= $pos >= 0B= seek *data, $pos, 0
ABresult
0X0
100
111
81 }
82 10 last;
83 }
84 }
85  
86 12 ▶ if (my $builtin = delete $opt{BUILTIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $builtin = delete $opt{"BUILTIN"}) 0 12 12
87 0 $obj->builtin(@$builtin);
88 }
89  
90 12 ▶ warn "Unprocessed option: ", Dumper \%opt if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 12 12
91  
92 12 $obj;
93 }
94  
95 12 ▶sub readrc {
Subroutine readrc: called CC=4 SCAR=14.8
Pod undocumented
96 12 my $obj = shift;
97 12 my $fh = shift;
98 12 my $text = do { local $/; <$fh> };
99 12 for ($text) {
100 12 ▶ s/^__(?:CODE|PERL5?)__\s*\n(.*)//ms and do {
Branch: 1 branch
BranchTrueFalseTotal
s/^__(?:CODE|PERL5?)__\s*\n(.*)//msu and do { package main; BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} local $main::MODULE = $obj; eval $1; die if $@ } 0 12 12
101 package main;
102 9 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 0 local $main::MODULE = $obj;
104 0 eval $1;
105 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
106 };
107 12 s/^\s*(?:#.*)?\n//mg;
108 12 s/\\\n//g;
109 }
110 12 $obj->parsetext($text);
111 12 $obj;
112 }
113  
114 ############################################################
115  
116 67 ▶sub module {
Subroutine module: called CC=3 SCAR=11.1
Pod documented
117 67 my $obj = shift;
118 @_ ? $obj->{Module} = shift
119 67 ▶ : $obj->{Module} // '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 55 67
Condition50%1/2
$obj->{"Module"} // ""
l!l
550
MC/DC0%0/1
$obj->{"Module"} // ""
$obj->{"Module"}
Truth table50%1/2
A
A= $obj->{"Module"}
Aresult
11
00
120 }
121  
122 0 ▶sub title {
Subroutine title: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my $obj = shift;
124 0 my $mod = $obj->module;
125 0 ▶ $mod =~ m{ .* [:/] (.+) }x ? $1 : $mod;
Branch: 1 branch
BranchTrueFalseTotal
$mod =~ m[ .* [:/] (.+) ]ux ? : 0 0 0
126 }
127  
128 18 ▶sub define {
Subroutine define: called CC=2 SCAR=7.5
Pod documented
129 18 my $obj = shift;
130 18 my $name = shift;
131 18 my $list = $obj->{Define};
132 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 18 0 18
133 18 my $re = qr/\Q$name/;
134 18 unshift(@$list, [ $name, $re, shift ]);
135 } else {
136 0 ▶ first { $_->[0] eq $name } @$list;
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 }
138 }
139  
140 101 ▶sub expand {
Subroutine expand: called CC=3 SCAR=12.3
Pod documented
141 101 my $obj = shift;
142 101 local *_ = shift;
143 101 for my $defent (@{$obj->{Define}}) {
144 107 my($name, $re, $string) = @$defent;
145 107 s/$re/$string/g;
146 }
147 101 ▶ s{ (\$ENV\{ (['"]?) \w+ \g{-1} \}) }{ eval($1) // $1 }xge;
Condition0%0/3
eval $1 // $1
l!l&&r!l&&!r
000
MC/DC0%0/2
eval $1 // $1
eval $1 $1
Truth table0%0/3
A // B
A= eval $1B= $1
ABresult
1X1
011
000
148 }
149  
150 40 ▶sub mode {
Subroutine mode: called CC=3 SCAR=11.0
Pod documented
151 40 my $obj = shift;
152 40 ▶ @_ == 1 and return $obj->{Mode}->{uc shift};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and return $obj->{"Mode"}{uc shift()} 38 2 40
153 2 ▶ die "Unexpected parameter." if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 2 2
154 2 ▶ pairmap {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 2 $obj->{Mode}->{uc $a} = $b;
156 2 } @_;
157 }
158  
159 9 ▶use constant BUILTIN => "__BUILTIN__";
Subroutine BEGIN: called CC=1 SCAR=0.0
160 28 ▶sub validopt { $_[0] ne BUILTIN }
Subroutine validopt: called CC=1 SCAR=0.0
Pod undocumented
161  
162 0 ▶sub setlocal {
Subroutine setlocal: not called CC=1 SCAR=6.9
Pod documented
163 0 my $obj = shift;
164 0 $obj->setlist("Expand", @_);
165 }
166  
167 101 ▶sub setopt {
Subroutine setopt: called CC=1 SCAR=0.0
Pod documented
168 101 my $obj = shift;
169 101 $obj->setlist("Option", @_);
170 }
171  
172 101 ▶sub setlist {
Subroutine setlist: called CC=5 SCAR=17.1
Pod undocumented
173 101 my $obj = shift;
174 101 my $list = $obj->{+shift};
175 101 my $name = shift;
176 101 my @args = do {
177 101 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 101 101
178 0 @{ $_[0] };
179 } else {
180 101 map { shellwords $_ } @_;
181 }
182 };
183  
184 101 ▶ for (my $i = 0; $i < @args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 101 101 202
185 101 ▶ if (my @opt = $obj->getlocal($args[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@opt) = $obj->getlocal($args[$i])) 0 101 101
186 0 splice @args, $i, 1, @opt;
187 0 redo;
188 }
189 }
190  
191 101 for (@args) {
192 101 $obj->expand(\$_);
193 }
194 101 unshift @$list, [ $name, @args ];
195 }
196  
197 118 ▶sub getopt {
Subroutine getopt: called CC=8 SCAR=22.2
Pod documented
198 118 my $obj = shift;
199 118 my($name, %opt) = @_;
200 118 ▶ return () if $name eq 'default' and not $opt{DEFAULT} || $opt{ALL};
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "default" and not $opt{"DEFAULT"} || $opt{"ALL"} 0 118 118
Condition50%3/6
$name eq "default" and not $opt{"DEFAULT"} || $opt{"ALL"}
!ll&&!rl&&r
106120
$opt{'DEFAULT'} || $opt{'ALL'}
l!l&&r!l&&!r
1200
MC/DC0%0/3
$name eq "default" and not $opt{"DEFAULT"} || $opt{"ALL"}
$name eq "default" $opt{'DEFAULT'} $opt{'ALL'}
Truth table50%2/4
A and not(B || C)
A= $name eq "default"B= $opt{'DEFAULT'}C= $opt{'ALL'}
ABCresult
0XX0
11X0
1010
1001
201  
202 118 my $list = $obj->{Option};
203 910 ▶ my $e = first {
Subroutine __ANON__: called CC=3 SCAR=11.0
204 910 ▶ $_->[0] eq $name and $opt{ALL} || validopt($_->[1])
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq $name and $opt{"ALL"} || validopt($_->[1]) 28 882 910
Condition83%5/6
$_->[0] eq $name && ($opt{'ALL'} || validopt($_->[1]))
!ll&&!rl&&r
882919
$opt{"ALL"} or validopt($_->[1])
l!l&&r!l&&!r
0199
MC/DC66%2/3
$_->[0] eq $name && ($opt{'ALL'} || validopt($_->[1]))
$_->[0] eq $name $opt{"ALL"} validopt($_->[1])
Truth table75%3/4
A && B or C
A= $_->[0] eq $nameB= $opt{"ALL"}C= validopt($_->[1])
ABCresult
0XX0
1000
11X1
1011
205 118 } @$list;
206 118 ▶ my @e = $e ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 19 99 118
207 118 shift @e;
208  
209 # check autoload
210 118 ▶ unless (@e) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@e) 99 19 118
211 99 my $hash = $obj->{Autoload};
212 99 for my $mod (@{$obj->{Automod}}) {
213 0 ▶ if (exists $hash->{$mod}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$mod}{$name}) 0 0 0
214 0 delete $hash->{$mod};
215 0 return ($mod, $name);
216 }
217 }
218 }
219  
220 118 @e;
221 }
222  
223 101 ▶sub getlocal {
Subroutine getlocal: called CC=2 SCAR=7.1
Pod undocumented
224 101 my $obj = shift;
225 101 my($name, %opt) = @_;
226  
227 101 ▶ my $e = first { $_->[0] eq $name } @{$obj->{Expand}};
Subroutine __ANON__: not called CC=1 SCAR=0.4
228 101 ▶ my @e = $e ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 0 101 101
229 101 shift @e;
230 101 @e;
231 }
232  
233 19 ▶sub expand_args {
Subroutine expand_args: called CC=6 SCAR=18.0
Pod undocumented
234 19 my $obj = shift;
235 19 my @args = @_;
236  
237 ##
238 ## Expand `&function' style arguments.
239 ##
240 19 ▶ if ($obj->mode('function')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->mode("function")) 1 18 19
241 @args = map {
242 1 ▶ if (/^&(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^&(.+)/u) { } 1 0 1
243 1 my $func = parse_func $obj->module . "::$1";
244 1 ▶ $func ? $func->call : $_;
Branch: 1 branch
BranchTrueFalseTotal
$func ? : 1 0 1
245 } else {
246 0 $_;
247 }
248 }
249 @args;
250 }
251  
252 ##
253 ## Expand wildcards.
254 ##
255 19 ▶ if ($obj->mode('wildcard')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->mode("wildcard")) 4 15 19
256 @args = map {
257 9 ▶ my @glob = glob $_;
Subroutine BEGIN: called CC=1 SCAR=0.0
258 4 ▶ @glob ? @glob : $_;
Branch: 1 branch
BranchTrueFalseTotal
@glob ? : 3 1 4
259 } @args;
260 }
261  
262 19 @args;
263 }
264  
265 12 ▶sub default {
Subroutine default: called CC=1 SCAR=0.0
Pod documented
266 12 my $obj = shift;
267 12 $obj->getopt('default', DEFAULT => 1);
268 }
269  
270 0 ▶sub options {
Subroutine options: not called CC=1 SCAR=6.9
Pod documented
271 0 my $obj = shift;
272 0 my $opt = $obj->{Option};
273 0 my $automod = $obj->{Automod};
274 0 my $auto = $obj->{Autoload};
275 0 my @opt = reverse map { $_->[0] } @$opt;
276 0 my @auto = map { sort keys %{$auto->{$_}} } @$automod;
277 0 (@opt, @auto);
278 }
279  
280 0 ▶sub help {
Subroutine help: not called CC=3 SCAR=24.8
Pod documented
281 0 my $obj = shift;
282 0 my $name = shift;
283 0 my $list = $obj->{Help};
284 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
285 0 unshift(@$list, [ $name, shift ]);
286 } else {
287 0 ▶ my $e = first { $_->[0] eq $name } @$list;
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 0 ▶ $e ? $e->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 0 0 0
289 }
290 }
291  
292 12 ▶sub parsetext {
Subroutine parsetext: called CC=3 SCAR=11.1
Pod undocumented
293 12 my $obj = shift;
294 12 my $text = shift;
295 12 my $re = qr{
296 (?|
297 # HERE document
298 (.+\s) << (?<mark>\w+) \n
299 (?<here> (?s:.*?) \n )
300 \g{mark}\n
301 |
302 (.+)\n?
303 )
304 }x;
305 12 ▶ while ($text =~ m/$re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/gu) 111 12 123
306 111 my $line = do {
307 111 ▶ if (defined $+{here}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{'here'}) { } 0 111 111
308 0 $1 . $+{here};
309 } else {
310 111 $1;
311 }
312 };
313 111 $obj->parseline($line);
314 }
315 12 $obj;
316 }
317  
318 111 ▶sub parseline {
Subroutine parseline: called CC=23 SCAR=36.6
Pod undocumented
319 111 my $obj = shift;
320 111 my $line = shift;
321 111 my @arg = split ' ', $line, 3;
322  
323 111 my %min_args = ( mode => 1, DEFAULT => 3 );
324 111 ▶ my $min_args = $min_args{$arg[0]} || $min_args{DEFAULT};
Condition66%2/3
$min_args{$arg[0]} || $min_args{'DEFAULT'}
l!l&&r!l&&!r
21090
MC/DC0%0/2
$min_args{$arg[0]} || $min_args{'DEFAULT'}
$min_args{$arg[0]} $min_args{'DEFAULT'}
Truth table66%2/3
A || B
A= $min_args{$arg[0]}B= $min_args{'DEFAULT'}
ABresult
1X1
011
000
325 111 ▶ if (@arg < $min_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg < $min_args) 0 111 111
326 0 warn sprintf("Parse error in %s: %s\n", $obj->title, $line);
327 0 return;
328 }
329  
330 ##
331 ## in-line help document after //
332 ##
333 111 ▶ my $optname = $arg[1] // '';
Condition50%1/2
$arg[1] // ""
l!l
1110
MC/DC0%0/1
$arg[1] // ""
$arg[1]
Truth table50%1/2
A
A= $arg[1]
Aresult
11
00
334 111 ▶ if ($arg[0] eq "builtin") {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg[0] eq "builtin") 32 79 111
335 32 for ($optname) {
336 32 s/[^\w\-].*//; # remove alternative names after `|'.
337 32 ▶ s/^(?=([\w\-]+))/length($1) == 1 ? '-' : '--'/e;
Branch: 1 branch
BranchTrueFalseTotal
length $1 == 1 ? : 0 32 32
338 }
339 }
340 111 ▶ if ($arg[2] and $arg[2] =~ s{ (?:^|\s+) // \s+ (?<message>.*) }{}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg[2] and $arg[2] =~ s[ (?:^|\s+) // \s+ (?<message>.*) ][]ux) 0 111 111
Condition66%2/3
$arg[2] and $arg[2] =~ s[ (?:^|\s+) // \s+ (?<message>.*) ][]ux
!ll&&!rl&&r
21090
MC/DC0%0/2
$arg[2] and $arg[2] =~ s[ (?:^|\s+) // \s+ (?<message>.*) ][]ux
$arg[2] $arg[2] =~ s[ (?:^|\s+) // \s+ (?<message>.*) ][]ux
Truth table66%2/3
A and B
A= $arg[2]B= $arg[2] =~ s[ (?:^|\s+) // \s+ (?<message>.*) ][]ux
ABresult
0X0
100
111
341 0 $obj->help($optname, $+{message});
342 }
343  
344 ##
345 ## Commands
346 ##
347 111 ▶ if ($arg[0] eq "define") {
Branch: 8 branches
BranchTrueFalseTotal
if ($arg[0] eq 'define') { } 8 103 111
elsif ($arg[0] eq 'option') { } 69 34 103
elsif ($arg[0] eq 'expand') { } 0 34 34
elsif ($arg[0] eq 'defopt') { } 0 34 34
elsif ($arg[0] eq 'builtin') { } 32 2 34
elsif ($arg[0] eq 'autoload') { } 0 2 2
elsif ($arg[0] eq 'mode') { } 2 0 2
elsif ($arg[0] eq 'help') { } 0 0 0
348 8 $obj->define($arg[1], $arg[2]);
349 }
350 elsif ($arg[0] eq "option") {
351 69 $obj->setopt($arg[1], $arg[2]);
352 }
353 elsif ($arg[0] eq "expand") {
354 0 $obj->setlocal($arg[1], $arg[2]);
355 }
356 elsif ($arg[0] eq "defopt") {
357 0 $obj->define($arg[1], $arg[2]);
358 0 $obj->setopt($arg[1], $arg[1]);
359 }
360 elsif ($arg[0] eq "builtin") {
361 32 $obj->setopt($optname, BUILTIN);
362 32 ▶ if ($arg[2] =~ /^\\?(?<mark>[\$\@\%\&])(?<name>[\w:]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg[2] =~ /^\\?(?<mark>[\$\@\%\&])(?<name>[\w:]+)/u) 32 0 32
363 32 my($mark, $name) = @+{"mark", "name"};
364 32 my $mod = $obj->module;
365 32 ▶ /:/ or s/^/$mod\::/ for $name;
Branch: 1 branch
BranchTrueFalseTotal
/:/u or s/^/${mod}::/u 0 32 32
366 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
367 32 $obj->builtin($arg[1] => {'$' => \${$name},
368 32 '@' => \@{$name},
369 32 '%' => \%{$name},
370 32 '&' => \&{$name}}->{$mark});
371 }
372 }
373 elsif ($arg[0] eq "autoload") {
374 0 shift @arg;
375 0 $obj->autoload(@arg);
376 }
377 elsif ($arg[0] eq "mode") {
378 2 shift @arg;
379 2 for (@arg) {
380 2 ▶ if (/^(no-?)?(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(no-?)?(.*)/iu) 2 0 2
381 2 ▶ $obj->mode($2 => $1 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 2 2
382 }
383 }
384 }
385 elsif ($arg[0] eq "help") {
386 0 $obj->help($arg[1], $arg[2]);
387 }
388 else {
389 0 warn sprintf("Unknown operator \"%s\" in %s\n",
390 $arg[0], $obj->title);
391 }
392  
393 111 $obj;
394 }
395  
396 36 ▶sub builtin {
Subroutine builtin: called CC=2 SCAR=6.9
Pod documented
397 36 my $obj = shift;
398 36 my $list = $obj->{Builtin};
399 36 ▶ @_ ? push @$list, @_
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 32 4 36
400 : @$list;
401 }
402  
403 0 ▶sub autoload {
Subroutine autoload: not called CC=4 SCAR=30.0
Pod documented
404 0 my $obj = shift;
405 0 my $module = shift;
406 0 my @option = map { split ' ' } @_;
407  
408 0 ▶ my $hash = ($obj->{Autoload}->{$module} //= {});
Condition0%0/2
$obj->{"Autoload"}{$module} //= {}
l!l
00
MC/DC0%0/1
$obj->{"Autoload"}{$module} //= {}
$obj->{"Autoload"}{$module}
Truth table0%0/2
A
A= $obj->{"Autoload"}{$module}
Aresult
11
00
409 0 my $list = $obj->{Automod};
410 0 for (@option) {
411 0 $hash->{$_} = 1;
412 0 $obj->help($_, "autoload: $module");
413 }
414 0 ▶ push @$list, $module if not grep { $_ eq $module } @$list;
Branch: 1 branch
BranchTrueFalseTotal
if not grep({$_ eq $module;} @$list) 0 0 0
415 }
416  
417 11 ▶sub call {
Subroutine call: called CC=2 SCAR=7.1
Pod undocumented
418 11 my $obj = shift;
419 11 my $list = $obj->{Call};
420 11 ▶ @_ ? push @$list, @_
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 11 11
421 : @$list;
422 }
423  
424 22 ▶sub call_if_defined {
Subroutine call_if_defined: called CC=2 SCAR=6.9
Pod undocumented
425 22 my($module, $name, @param) = @_;
426 22 my $func = "$module\::$name";
427 22 ▶ if (defined &$func) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$func) 5 17 22
428 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
429 5 $func->(@param);
430 }
431 }
432  
433 11 ▶sub run_inits {
Subroutine run_inits: called CC=3 SCAR=12.3
Pod undocumented
434 11 my $obj = shift;
435 11 my $argv = shift;
436 11 my $module = $obj->module;
437 11 local @ARGV = ();
438  
439 11 call_if_defined $module, "initialize" => ($obj, $argv);
440  
441 11 for my $call ($obj->call) {
442 0 ▶ my $func = $call->can('call') ? $call : parse_func($call);
Branch: 1 branch
BranchTrueFalseTotal
$call->can('call') ? : 0 0 0
443 0 $func->call;
444 }
445  
446 11 call_if_defined $module, "finalize" => ($obj, $argv);
447 }
448  
449 9 1;
450  
451 =head1 NAME
452  
453 Getopt::EX::Module - RC/Module data container
454  
455 =head1 SYNOPSIS
456  
457 use Getopt::EX::Module;
458  
459 my $bucket = Getopt::EX::Module->new(
460 BASECLASS => $baseclass,
461 FILE => $file_name / MODULE => $module_name,
462 );
463  
464 =head1 DESCRIPTION
465  
466 This module is usually used from L<Getopt::EX::Loader>, and keeps
467 all data about the loaded rc file or module.
468  
469 =head2 MODULE
470  
471 After a user-defined module is loaded, the subroutine C<initialize> is
472 called if it exists in the module. At this time, the container object is
473 passed to the function as the first argument and the following command
474 argument pointer as the second. So you can use it to directly access
475 the object contents through the class interface.
476  
477 Following C<initialize>, the function defined with the module option is called.
478  
479 Finally, the subroutine C<finalize> is called if defined, to finalize the startup
480 process of the module.
481  
482 =head2 FILE
483  
484 For rc files, the section after the C<__PERL__> or C<__PERL5__> mark is
485 executed as a Perl program. At this time, the module object is assigned to the
486 variable C<$MODULE>, and you can access the module API through it.
487  
488 if (our $MODULE) {
489 $MODULE->setopt('default', '--number');
490 }
491  
492 =head1 RC FILE FORMAT
493  
494 =over 7
495  
496 =item B<option> I<name> I<string>
497  
498 Define option I<name>. Argument I<string> is processed by the
499 I<shellwords> routine defined in the L<Text::ParseWords> module. Be aware
500 that this module sometimes requires escaped backslashes.
501  
502 Any kind of string can be used for an option name but it is not combined
503 with other options.
504  
505 option --fromcode --outside='(?s)\/\*.*?\*\/'
506 option --fromcomment --inside='(?s)\/\*.*?\*\/'
507  
508 If the option named B<default> is defined, it will be used as a
509 default option.
510  
511 For the purpose of including following arguments within replaced
512 strings, two special notations can be used in option definitions.
513  
514 The string C<< $<n> >> is replaced by the I<n>th argument after the
515 substituted option, where I<n> is a number starting from one. Because C<<
516 $<0> >> is replaced by the defined option itself, you have to be careful
517 about infinite loops.
518  
519 The string C<< $<shift> >> is replaced by the following command line argument
520 and the argument is removed from the list.
521  
522 For example, when
523  
524 option --line --le &line=$<shift>
525  
526 is defined, the command
527  
528 greple --line 10,20-30,40
529  
530 will be evaluated as:
531  
532 greple --le &line=10,20-30,40
533  
534 There are special arguments to manipulate option behavior and the rest
535 of the arguments. Argument C<< $<move> >> moves all following arguments
536 there, C<< $<remove> >> just removes them, and C<< $<copy> >> copies
537 them. These do not work when included as part of a string.
538  
539 They take one or two optional parameters, which are passed to the Perl
540 C<splice> function as I<offset> and I<length>. C<< $<move(0,1)> >> is the
541 same as C<< $<shift> >>; C<< $<copy(0,1)> >> is the same as C<< $<1> >>;
542 C<< $<move> >> is the same as C<< $<move(0)> >>; C<< $<move(-1)> >> moves
543 the last argument; C<< $move(1,1) >> moves the second argument. The next
544 example exchanges the following two arguments.
545  
546 option --exch $<move(1,1)>
547  
548 You can use the recently introduced C<< $<ignore> >> to ignore the
549 argument. Some existing modules use C<< $<move(0,0)> >> for the same
550 purpose, because it effectively does nothing.
551  
552 option --deprecated $<ignore>
553 option --deprecated $<move(0,0)>
554  
555 =item B<expand> I<name> I<string>
556  
557 Define local option I<name>. The B<expand> command is almost the same as the
558 B<option> command in terms of its function. However, options defined
559 by this command are expanded in, and only in, the process of
560 definition, while option definitions are expanded when command arguments
561 are processed.
562  
563 This is similar to a string macro defined by the following B<define>
564 command. But macro expansion is done by simple string replacement, so
565 you have to use B<expand> to define options composed of multiple
566 arguments.
567  
568 =item B<define> I<name> I<string>
569  
570 Define a string macro. This is similar to B<option>, but the argument is
571 not processed by I<shellwords> and is treated as simple text, so
572 meta-characters can be included without escaping. Macro expansion is
573 performed for option definitions and other macro definitions. Macros are not
574 evaluated in command line options. Use the option directive if you want to
575 use it in the command line,
576  
577 define (#kana) \p{InKatakana}
578 option --kanalist --nocolor -o --join --re '(#kana)+(\n(#kana)+)*'
579 help --kanalist List up Katakana string
580  
581 A here-document can be used to define strings including newlines.
582  
583 define __script__ <<EOS
584 {
585 ...
586 }
587 EOS
588  
589 The special macro C<__PACKAGE__> is pre-defined to the module name.
590  
591 =item B<help> I<name>
592  
593 Define a help message for option I<name>.
594  
595 =item B<builtin> I<spec> I<variable>
596  
597 Define a built-in option which should be processed by the option parser.
598 The defined option spec can be retrieved by the B<builtin> method, and the script is
599 responsible for passing them to the parser.
600  
601 Arguments are assumed to be L<Getopt::Long> style specs, and
602 I<variable> is a string starting with C<$>, C<@> or C<%>. They will be
603 replaced by a reference to the object which the string represents.
604  
605 =item B<autoload> I<module> I<options>
606  
607 Define a module which should be loaded automatically when the specified
608 option is found in the command arguments.
609  
610 For example,
611  
612 autoload -Mdig --dig
613  
614 replaces the option "I<--dig>" with "I<-Mdig --dig>", and the I<dig> module is
615 loaded before processing the I<--dig> option.
616  
617 =item B<mode> [I<no>]I<name>
618  
619 Set or unset mode I<name>. Currently, B<function> and B<wildcard> can
620 be used as a name. See the METHODS section.
621  
622 The next example is used in the L<App::Greple::subst::dyncmap> module to
623 produce parameters on the fly.
624  
625 mode function
626 option --dyncmap &dyncmap($<shift>)
627  
628 =back
629  
630 =head1 METHODS
631  
632 =over 4
633  
634 =item B<new> I<configure option>
635  
636 Create an object. Parameters are passed to the C<configure> method.
637  
638 =item B<configure>
639  
640 Configure the object. Parameters are passed in hash name and value style.
641  
642 =over 4
643  
644 =item B<BASECLASS> =E<gt> I<class>
645  
646 Set base class.
647  
648 =item B<FILE> =E<gt> I<filename>
649  
650 Load file.
651  
652 =item B<MODULE> =E<gt> I<modulename>
653  
654 Load module.
655  
656 =back
657  
658 =item B<define> I<name>, I<macro>
659  
660 Define macro.
661  
662 =item B<setopt> I<name>, I<option>
663  
664 Set option.
665  
666 =item B<setlocal> I<name>, I<option>
667  
668 Set option which is effective only in the module.
669  
670 =item B<getopt> I<name>
671  
672 Get an option. Takes an option name and returns its definition if
673 available. It doesn't return the I<default> option; get it by the I<default>
674 method.
675  
676 =item B<default>
677  
678 Get the default option. Use C<setopt(default =E<gt> ...)> to set it.
679  
680 =item B<builtin>
681  
682 Get built-in options.
683  
684 =item B<autoload>
685  
686 Set autoload module.
687  
688 =item B<mode>
689  
690 Set the argument treatment mode. Arguments produced by option expansion
691 will be the subject of post-processing. This method defines the behavior
692 of it.
693  
694 =over 4
695  
696 =item B<mode>(B<function> => 1)
697  
698 Interpret arguments starting with '&' as a function, and replace them with
699 the result of the function call.
700  
701 =item B<mode>(B<wildcard> => 1)
702  
703 Replace wildcard arguments with matched file names.
704  
705 =back
706  
707 =back
« EX/Numbers.pm ↑ Summary EX/Loader.pm »