pl

filter: Statementstmt 70.7% 293 / 414 Branchbran 70.5% 240 / 340 Conditioncond 58.8% 80 / 136 MC/DCmcdc 39.2% 31 / 79 Subroutinesub 69.2% 27 / 39 Podpod n/a totaltotal 66.5% 671 / 1008 CC 254 SCAR 36.2
CC
254
Cov
69%
hosts
110.0
__ANON__
110.0
keydiff
73.8
CRAP 37.3
↑ Summary
1 #! /usr/bin/env perl
2  
3 # Beware: DON'T PERFORM ANY ACTIONS AT TOP LEVEL: this file runs many times under -n!
4  
5 197 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use warnings 'all';
7  
8 114 ▶BEGIN {
Subroutine BEGIN: called CC=37 SCAR=41.0
9 730 $pl::VERSION = 'v0.91.1';
10 730 ▶ my $execed = shift if @ARGV && $ARGV[0] eq '+'; # don't recursively call perl again for -p
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV and $ARGV[0] eq "+" 593 109 702
Condition66%2/3
@ARGV and $ARGV[0] eq "+"
!ll&&!rl&&r
08331
MC/DC50%1/2
@ARGV and $ARGV[0] eq "+"
@ARGV $ARGV[0] eq "+"
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] eq "+"
ABresult
0X0
100
111
11 676 ▶ return unless @ARGV and $ARGV[0] =~ /^-/s;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV and $ARGV[0] =~ /^-/su 19 95 114
Condition66%2/3
@ARGV and $ARGV[0] =~ /^-/su
!ll&&!rl&&r
01995
MC/DC50%1/2
@ARGV and $ARGV[0] =~ /^-/su
@ARGV $ARGV[0] =~ /^-/su
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] =~ /^-/su
ABresult
0X0
100
111
12 355 my $onepl = qr/[rhv?]|[oOpP](?:[1-9][0-9]*)?/; # simple pl opts, reflect these in pod/html
13 195 my $one = qr/[cfTtUuWwXan]|$onepl/; # simple opts to pass to perl, which handles them well
14 95 my( $perl, @perl, @pl1, %seen );
15 95 ▶ while( @ARGV ) { # no for, may need to shift
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 230 1 231
16 230 $_ = $ARGV[0];
17 230 ▶ last unless /^-/s;
Branch: 1 branch
BranchTrueFalseTotal
unless /^-/su 63 167 230
18 167 shift;
19 167 ▶ last if /^--$/s;
Branch: 1 branch
BranchTrueFalseTotal
if /^--$/su 31 136 167
20 136 ▶ if( /^--(?:help|version|color(?:|=never|=always|=auto))$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^--(?:help|version|color(?:|=never|=always|=auto))$/su) 16 120 136
21 16 push @pl::opts, $_;
22 16 next;
23 }
24 120 ▶ unshift @ARGV, "-$1" if s/^-$one*[0l][0-7]*$onepl+\K(0.*)//; # avoid -l012o0 -> -l0120
Branch: 1 branch
BranchTrueFalseTotal
if s/^-$one*[0l][0-7]*$onepl+\K(0.*)//u 0 120 120
25 120 ▶ if( /^(-$one*(?:[0l][0-7]*$one*)*)(?:([CDdFiIMm0ABbEeV])(.*))?$/s ) { # Perl does 0 & l inline only with oct arg.
Branch: 1 branch
BranchTrueFalseTotal
if (/^(-$one*(?:[0l][0-7]*$one*)*)(?:([CDdFiIMm0ABbEeV])(.*))?$/su) { } 120 0 120
26 120 my( $opts, $opt, $arg ) = ($1, $2, $3);
27 120 ▶ $seen{$_} ||= $opts =~ $_ for qw(a n O o P p r);
Condition100%3/3
$seen{$_} ||= $opts =~ /$_/u
l!l&&r!l&&!r
2959752
MC/DC100%2/2
$seen{$_} ||= $opts =~ /$_/u
$seen{$_} $opts =~ /$_/u
Truth table100%3/3
A ||= B
A= $seen{$_}B= $opts =~ /$_/u
ABresult
1X1
011
000
28 120 ▶ push @pl1, $1 while $opts =~ s/($onepl)//;
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ s/($onepl)//u and do { push @pl1, $1 } 54 120 174
29 120 ▶ if( defined $opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt) 89 31 120
30 89 ▶ if( ! length $arg and $opt =~ /[IABbEeV]/ ) { # These can have arg glued or separated
Branch: 1 branch
BranchTrueFalseTotal
if (not length $arg and $opt =~ /[IABbEeV]/u) 56 33 89
Condition66%2/3
not length $arg and $opt =~ /[IABbEeV]/u
!ll&&!rl&&r
33056
MC/DC50%1/2
not length $arg and $opt =~ /[IABbEeV]/u
not length $arg $opt =~ /[IABbEeV]/u
Truth table66%2/3
not(A) and B
A= not length $argB= $opt =~ /[IABbEeV]/u
ABresult
0X0
100
111
31 56 $arg = shift;
32 56 ▶ unless( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 0 56 56
33 0 warn "Missing argument to -$opt.\n";
34 0 exit 29;
35 }
36 }
37 89 ▶ if( $opt eq 'V' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt eq 'V') { } 0 89 89
elsif ($opt eq 'I') { } 0 89 89
elsif ($opt =~ /[ABbEe]/u) { } 77 12 89
38 0 $perl = "perl$arg";
39 0 undef $opt; # treat $opts below
40 } elsif( $opt eq 'I' ) {
41 0 push @perl, $opts.'I', $arg; # let perl check for empty arg
42 } elsif( $opt =~ /[ABbEe]/ ) {
43 77 ▶ $seen{$opt} ||= $opt =~ /[be]/;
Condition66%2/3
$seen{$opt} ||= $opt =~ /[be]/u
l!l&&r!l&&!r
02453
MC/DC50%1/2
$seen{$opt} ||= $opt =~ /[be]/u
$seen{$opt} $opt =~ /[be]/u
Truth table66%2/3
A ||= B
A= $seen{$opt}B= $opt =~ /[be]/u
ABresult
1X1
011
000
44 77 push @pl::opts, join( '', '-', @pl1, $opt ), $arg;
45 77 @pl1 = ();
46 77 undef $opt; # treat $opts below
47 } else {
48 12 ▶ $seen{F} ||= $opt eq 'F';
Condition66%2/3
$seen{"F"} ||= $opt eq "F"
l!l&&r!l&&!r
0102
MC/DC50%1/2
$seen{"F"} ||= $opt eq "F"
$seen{"F"} $opt eq "F"
Truth table66%2/3
A ||= B
A= $seen{"F"}B= $opt eq "F"
ABresult
1X1
011
000
49 12 push @perl, $opts.$opt.$arg;
50 }
51 }
52 120 ▶ unless( defined $opt ) { # no else because of -[bBeEV] above
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opt) 108 12 120
53 108 ▶ push @perl, $opts if 1 < length $opts;
Branch: 1 branch
BranchTrueFalseTotal
if 1 < length $opts 7 101 108
54 }
55 } else {
56 0 s/^-$one*(?:[0l][0-7]*$one*)*(.).*/-$1/;
57 0 warn "Unrecognized switch: $_ (-h will show valid options).\n";
58 0 exit 29;
59 }
60 }
61 95 ▶ $seen{a} ||= push @perl, '-a' if $seen{F};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{"F"} 10 85 95
Condition33%1/3
$seen{"a"} ||= push(@perl, "-a")
l!l&&r!l&&!r
0100
MC/DC0%0/2
$seen{"a"} ||= push(@perl, "-a")
$seen{"a"} push(@perl, "-a")
Truth table33%1/3
A ||= B
A= $seen{"a"}B= push(@perl, "-a")
ABresult
1X1
011
000
62 $seen{n} ||= push @perl, '-n'
63 if ! grep $seen{$_}, qw(o O)
64 95 ▶ and grep $seen{$_}, qw(a b e P p r);
Branch: 1 branch
BranchTrueFalseTotal
if not grep($seen{$_}, ("o", "O")) and grep $seen{$_}, ("a", "b", "e", "P", "p", "r") 39 56 95
Condition66%4/6
not grep($seen{$_}, ("o", "O")) and grep $seen{$_}, ("a", "b", "e", "P", "p", "r")
!ll&&!rl&&r
173939
$seen{"n"} ||= push(@perl, "-n")
l!l&&r!l&&!r
0390
MC/DC100%2/2
not grep($seen{$_}, ("o", "O")) and grep $seen{$_}, ("a", "b", "e", "P", "p", "r")
not grep($seen{$_}, ("o", "O")) grep $seen{$_}, ("a", "b", "e", "P", "p", "r")
MC/DC0%0/2
$seen{"n"} ||= push(@perl, "-n")
$seen{"n"} push(@perl, "-n")
Truth table100%3/3
not(A) and B
A= not grep($seen{$_}, ("o", "O"))B= grep $seen{$_}, ("a", "b", "e", "P", "p", "r")
ABresult
0X0
100
111
Truth table33%1/3
A ||= B
A= $seen{"n"}B= push(@perl, "-n")
ABresult
1X1
011
000
65 95 ▶ push @pl::opts, join '', '-', @pl1 if @pl1;
Branch: 1 branch
BranchTrueFalseTotal
if @pl1 25 70 95
66 #warn join '|', $perl // $^X, @perl, '--', $0, '+', @pl::opts, '--', @ARGV, "\n";
67 95 ▶ if( ! $execed and $perl || @perl ) { # let perl do perl-opts
Branch: 1 branch
BranchTrueFalseTotal
if (not $execed and $perl || @perl) 31 64 95
Condition83%5/6
not $execed and $perl || @perl
!ll&&!rl&&r
313331
$perl or @perl
l!l&&r!l&&!r
03133
MC/DC66%2/3
not $execed and $perl || @perl
not $execed $perl @perl
Truth table75%3/4
not(A) and B or C
A= not $execedB= $perlC= @perl
ABCresult
0XX0
1000
11X1
1011
68 31 ▶ unshift @ARGV, $perl //= $^X, @perl, '--', $0, '+', @pl::opts, '--';
Condition33%1/3
$perl //= $^X
l!l&&r!l&&!r
0310
MC/DC0%0/2
$perl //= $^X
$perl $^X
Truth table33%1/3
A //= B
A= $perlB= $^X
ABresult
1X1
011
000
69 31 ▶ if( $^O =~ /^MSWin/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/u) 0 31 31
70 0 require Win32::ShellQuote;
71 0 my $ret = system {$ARGV[0]} Win32::ShellQuote::quote_system_list( @ARGV );
72 0 ▶ warn $perl, ": $!\n" if $ret < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret < 0 0 0 0
73 0 exit $ret >> 8;
74 }
75 31 exec @ARGV;
76 0 warn $perl, ": $!\n";
77 0 exit 1;
78 }
79 }
80  
81 83 ▶use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 83 ▶use List::Util '/./'; # all EXPORT_OK
Subroutine BEGIN: called CC=1 SCAR=0.0
84 83 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
85 83 ▶ eval q{
Branch: 1 branch
BranchTrueFalseTotal
if $List::Util::VERSION lt "1.33" 0 83 83
86 sub any(&@) { my $prog = shift; &$prog && return 1 for @_ };
87 sub all(&@) { my $prog = shift; &$prog || return for @_; 1 };
88 sub none(&@) { my $prog = shift; &$prog && return for @_; 1 };
89 sub notall(&@) { my $prog = shift; &$prog || return 1 for @_ };
90 sub product(@) { reduce { $a * $b } 1, @_ };
91 *sum0 = sub(@) { sum 0, @_ } if !defined \&sum0; # perl < 5.18
92 } if $List::Util::VERSION lt '1.33'; # perl < 5.20
93 }
94  
95  
96 our( $A, @A,
97 @FIELD,
98 $H, $HEX_SORT,
99 $I, $ARGIND,
100 %K, %KEYDIFF,
101 %N, %NUMBER,
102 $q, $quote,
103 $Q, $Quote,
104 $R, $RESULT,
105 @R, @RESULT,
106 %R, %RESULT,
107 %T, %TEMPLATE,
108 $T, $TXT_SORT );
109  
110  
111  
112 # Echo without newline
113 1002 ▶sub Echo(@) {
Subroutine Echo: called CC=7 SCAR=19.5
114 1002 ▶ if( wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 764 238 1002
elsif (defined wantarray) { } 3 235 238
115 map {
116 764 ▶ if( ! defined ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $_) { } 6 1007 1013
elsif (not ref $_) { } 1005 2 1007
elsif (eval { do { $_->can('(""') } }) { } 1 1 2
117 6 "$pl::c{I}undef$pl::c{E}";
118 } elsif( !ref ) {
119 1005 $_;
120 2 } elsif( eval { $_->can( '(""' ) } ) {
121 1 "$_";
122 } else {
123 1 require Data::Dumper;
124 1 ▶ Data::Dumper::Dumper( $_ )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 707 57 764
125 =~ s/;?\n?$//rs
126 =~ s/\bundef\b/$pl::c{I}undef$pl::c{E}/grs;
127 }
128 } @_ ? @_ : $_;
129 } elsif( defined wantarray ) {
130 3 join ' ', &Echo;
131 } else {
132 235 local( $,, $\, $| ) = (' ', '', 1);
133 235 print &Echo;
134 }
135 }
136  
137 334 ▶sub echo(@) {
Subroutine echo: called CC=4 SCAR=17.2
138 334 ▶ if( wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 334 334
elsif (defined wantarray) { } 3 331 334
139 0 my @ret = &Echo;
140 0 ▶ $ret[-1] .= "\n" if @ret;
Branch: 1 branch
BranchTrueFalseTotal
if @ret 0 0 0
141 0 @ret;
142 } elsif( defined wantarray ) {
143 3 join( ' ', &Echo ) . "\n";
144 } else {
145 331 local $, = ' ';
146 331 say &Echo;
147 }
148 }
149  
150  
151 # Form without newline
152 192 ▶sub Form($@) {
Subroutine Form: called CC=1 SCAR=0.0
153 192 my $form; # don't spill $1..., overriding ones passed in
154 192 { $form = shift =~ s/%\K(%|[^a-z]+)/ my $x = $1; $x =~ tr!:!$!; $x /egr } # also matches %%
155 192 $pl::Echo->( sprintf $form, &Echo );
156 }
157  
158 # Would call it format (which is useless in one-liners,) but that's not overridable.
159 190 ▶sub form($@) {
Subroutine form: called CC=1 SCAR=0.0
160 190 local $pl::Echo = \&echo;
161 190 &Form;
162 }
163  
164  
165 # Template without newline
166 21 ▶sub Template(_;@) {
Subroutine Template: called CC=24 SCAR=32.0
167 21 ▶ local *TEMPLATE = ref( $_[1] ) ? $_[1] : { @_[1..$#_] } if @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 8 13 21
ref $_[1] ? : 1 7 8
168 21 my @_template; # only closure to leak into sub
169 21 ▶ $pl::Echo->( &{$pl::template{$_[0]} //= eval do {
Condition60%3/5
($pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ('GLOB' ne ref $tmpl) { open my $fh, '<', $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join('', qq[#line 1 "template"\nsub { my ], map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq '|' and $part[3] eq '') { ' . &Template( $' . ($part[0] eq '' ? '_' : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq '!') { $part[3] =~ s/([({])\K/$1 eq '(' ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : '') . ($local && $part[1] // '') . $part[3] . ($part[2] eq ':' ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? '.=' : '=') . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{'PLDUMP'} // 0) == 2) { &echo('my @_template =', \@_template, ';'); say $t; exit; } ; $t }) || die($@)
l!l
210
$pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ("GLOB" ne ref $tmpl) { open my $fh, "<", $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join("", "#line 1 \"template\"\nsub { my ", map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq "|" and $part[3] eq "") { ' . &Template( $' . ($part[0] eq "" ? "_" : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq "!") { $part[3] =~ s/([({])\K/$1 eq "(" ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : "") . ($local && $part[1] // "") . $part[3] . ($part[2] eq ":" ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? ".=" : "=") . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{"PLDUMP"} // 0) == 2) { &echo("my \@_template =", \@_template, ";"); say $t; exit; } ; $t }
l!l&&r!l&&!r
4170
MC/DC0%0/1
($pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ('GLOB' ne ref $tmpl) { open my $fh, '<', $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join('', qq[#line 1 "template"\nsub { my ], map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq '|' and $part[3] eq '') { ' . &Template( $' . ($part[0] eq '' ? '_' : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq '!') { $part[3] =~ s/([({])\K/$1 eq '(' ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : '') . ($local && $part[1] // '') . $part[3] . ($part[2] eq ':' ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? '.=' : '=') . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{'PLDUMP'} // 0) == 2) { &echo('my @_template =', \@_template, ';'); say $t; exit; } ; $t }) || die($@)
($pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ('GLOB' ne ref $tmpl) { open my $fh, '<', $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join('', qq[#line 1 "template"\nsub { my ], map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq '|' and $part[3] eq '') { ' . &Template( $' . ($part[0] eq '' ? '_' : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq '!') { $part[3] =~ s/([({])\K/$1 eq '(' ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : '') . ($local && $part[1] // '') . $part[3] . ($part[2] eq ':' ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? '.=' : '=') . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{'PLDUMP'} // 0) == 2) { &echo('my @_template =', \@_template, ';'); say $t; exit; } ; $t })
MC/DC0%0/2
$pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ("GLOB" ne ref $tmpl) { open my $fh, "<", $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join("", "#line 1 \"template\"\nsub { my ", map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq "|" and $part[3] eq "") { ' . &Template( $' . ($part[0] eq "" ? "_" : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq "!") { $part[3] =~ s/([({])\K/$1 eq "(" ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : "") . ($local && $part[1] // "") . $part[3] . ($part[2] eq ":" ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? ".=" : "=") . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{"PLDUMP"} // 0) == 2) { &echo("my \@_template =", \@_template, ";"); say $t; exit; } ; $t }
$pl::template{$_[0]} eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ("GLOB" ne ref $tmpl) { open my $fh, "<", $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join("", "#line 1 \"template\"\nsub { my ", map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq "|" and $part[3] eq "") { ' . &Template( $' . ($part[0] eq "" ? "_" : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq "!") { $part[3] =~ s/([({])\K/$1 eq "(" ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : "") . ($local && $part[1] // "") . $part[3] . ($part[2] eq ":" ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? ".=" : "=") . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{"PLDUMP"} // 0) == 2) { &echo("my \@_template =", \@_template, ";"); say $t; exit; } ; $t }
Truth table50%1/2
A
A= ($pl::template{$_[0]} //= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ('GLOB' ne ref $tmpl) { open my $fh, '<', $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join('', qq[#line 1 "template"\nsub { my ], map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq '|' and $part[3] eq '') { ' . &Template( $' . ($part[0] eq '' ? '_' : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq '!') { $part[3] =~ s/([({])\K/$1 eq '(' ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : '') . ($local && $part[1] // '') . $part[3] . ($part[2] eq ':' ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? '.=' : '=') . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{'PLDUMP'} // 0) == 2) { &echo('my @_template =', \@_template, ';'); say $t; exit; } ; $t })
Aresult
11
00
Truth table66%2/3
A //= B
A= $pl::template{$_[0]}B= eval do { my $tmpl = $_[0]; if (ref $tmpl) { if ("GLOB" ne ref $tmpl) { open my $fh, "<", $$tmpl; $tmpl = $fh; } ; local $/; $tmpl = readline $tmpl; } ; () = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/u; my(@split) = $b ? split(/(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/su, $tmpl, 0) : $tmpl; my $t = join("", "#line 1 \"template\"\nsub { my ", map({if ($_ % 2) { my @part = split(/\s*(\??)([|:!])\s*/u, $split[$_], 2); if (@part < 2) { " . (\$TEMPLATE{$part[0]} // '');"; } elsif ($part[2] eq "|" and $part[3] eq "") { ' . &Template( $' . ($part[0] eq "" ? "_" : "TEMPLATE{$part[0]}") . ');'; } else { $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"; my $local = $part[0] =~ s[.+][local \$_ = \$TEMPLATE{$&} // '']u; if ($part[2] eq "!") { $part[3] =~ s/([({])\K/$1 eq "(" ? "($part[0])?():()," : "$part[0];";/eu if $local; "\n; $part[3]\n;"; } else { ' . (do {{ ' . ($local ? "$part[0]; " : "") . ($local && $part[1] // "") . $part[3] . ($part[2] eq ":" ? "\n; \$_ }} // '');" : "\n }} // '');"); } ; } ; } else { push @_template, $split[$_]; '$_template ' . ($_ ? ".=" : "=") . " \$_template[$#_template]"; } ;} 0 .. $#split), "\n; \$_template }"); if (($ENV{"PLDUMP"} // 0) == 2) { &echo("my \@_template =", \@_template, ";"); say $t; exit; } ; $t }
ABresult
1X1
011
000
170 17 my $tmpl = $_[0];
171 17 ▶ if( ref $tmpl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmpl) 2 15 17
172 2 ▶ if( 'GLOB' ne ref $tmpl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("GLOB" ne ref $tmpl) 1 1 2
173 1 open my $fh, '<', $$tmpl;
174 1 $tmpl = $fh;
175 }
176 2 local $/;
177 2 $tmpl = <$tmpl>;
178 }
179 17 my( $b, $e ) = $tmpl =~ /(?|(\[%).+?(%\])|(\{\{).+?(\}\})|(<\?pl\b).+?(\?>))/; # which syntax comes 1st?
180 17 ▶ my @split = $b ?
Branch: 1 branch
BranchTrueFalseTotal
$b ? : 16 1 17
181 split /(?:\h*\Q$b~\E|\Q$b\E(?(?<=l)\b))\s*(.+?)\s*(?:\Q~$e\E\h*\n?|\Q$e\E)/s, $tmpl :
182 $tmpl;
183 my $t = join '',
184 qq[#line 1 "template"\nsub { my ], # todo use/no strict... preserve linenos
185 (map {
186 17 ▶ if( $_ % 2 ) { # {{markup...}}
Branch: 1 branch
BranchTrueFalseTotal
if ($_ % 2) { } 139 151 290
187 139 my @part = split /\s*(\??)([|:!])\s*/, $split[$_], 2;
188 139 ▶ if( @part < 2 ) { # {{markup}}
Branch: 2 branches
BranchTrueFalseTotal
if (@part < 2) { } 23 116 139
elsif ($part[2] eq '|' and $part[3] eq '') { } 3 113 116
189 23 " . (\$TEMPLATE{$part[0]} // '');";
190 ▶ } elsif( $part[2] eq '|' && $part[3] eq '' ) {
Condition100%3/3
$part[2] eq "|" and $part[3] eq ""
!ll&&!rl&&r
87263
MC/DC100%2/2
$part[2] eq "|" and $part[3] eq ""
$part[2] eq "|" $part[3] eq ""
Truth table100%3/3
A and B
A= $part[2] eq "|"B= $part[3] eq ""
ABresult
0X0
100
111
191 3 ▶ ' . &Template( $' . ($part[0] eq '' ? '_' : "TEMPLATE{$part[0]}") . ');';
Branch: 1 branch
BranchTrueFalseTotal
$part[0] eq '' ? : 1 2 3
192 } else {
193 113 ▶ $part[1] &&= "last unless exists \$TEMPLATE{$part[0]};";
Condition100%2/2
$part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"
!ll
1112
MC/DC100%1/1
$part[1] &&= "last unless exists \$TEMPLATE{$part[0]};"
$part[1]
Truth table100%2/2
A
A= $part[1]
Aresult
00
11
194 113 my $local = $part[0] =~ s!.+!local \$_ = \$TEMPLATE{$&} // ''!;
195 113 ▶ if( $part[2] eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part[2] eq '!') { } 50 63 113
196 50 ▶ $part[3] =~ s!([({])\K!$1 eq '(' ? "($part[0])?():()," : "$part[0];"!e if $local;
Branch: 2 branches
BranchTrueFalseTotal
if $local 20 30 50
$1 eq '(' ? : 16 4 20
197 50 "\n; $part[3]\n;";
198 } else {
199 63 ▶ ' . (do {{ ' . ($local ? "$part[0]; " : '') .
Branch: 2 branches
BranchTrueFalseTotal
$local ? : 44 19 63
$part[2] eq ':' ? : 37 26 63
Condition80%4/5
$local && $part[1] // ""
l!l
630
$local and $part[1]
!ll&&!rl&&r
19422
MC/DC100%2/2
$local && $part[1] // ""
$local $part[1]
Truth table100%3/3
A and B
A= $localB= $part[1]
ABresult
111
0X0
100
200 ($local && $part[1] // '') .
201 $part[3] .
202 ($part[2] eq ':' ? "\n; \$_ }} // '');" : "\n }} // '');");
203 }
204 }
205 } else { # text
206 151 push @_template, $split[$_];
207 151 ▶ '$_template ' . ($_ ? '.=' : '=') . " \$_template[$#_template]";
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 134 17 151
208 }
209 } 0..$#split),
210 "\n; \$_template }";
211 0 ▶ if( ($ENV{PLDUMP} // 0) == 2 ) { echo 'my @_template =', \@_template, ';'; say $t; exit }
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{"PLDUMP"} // 0) == 2) 0 17 17
Condition50%1/2
$ENV{"PLDUMP"} // 0
l!l
017
MC/DC0%0/1
$ENV{"PLDUMP"} // 0
$ENV{"PLDUMP"}
Truth table50%1/2
A
A= $ENV{"PLDUMP"}
Aresult
11
00
212 17 $t;
213 } or die $@} );
214 }
215  
216 3 ▶sub template(_;@) {
Subroutine template: called CC=1 SCAR=0.0
217 3 local $pl::Echo = \&echo;
218 3 &Template;
219 }
220  
221  
222 0 ▶sub benchmark(&@) {
Subroutine benchmark: not called CC=4 SCAR=30.0
223 0 my( $code, $name, @args ) = @_;
224 0 local( $a, $b ) = ($a, $b);
225 0 ▶ $name //= 'noname';
Condition0%0/2
$name //= "noname"
l!l
00
MC/DC0%0/1
$name //= "noname"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
226 0 require Benchmark;
227 0 ▶ local $SIG{__WARN__} = sub { die @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 0 ▶ if( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 0 0 0
229 say Benchmark::timestr( Benchmark::countit( 10, $code )), " $name: $_"
230 0 for @args;
231 } else {
232 0 say Benchmark::timestr( Benchmark::countit( 10, $code )), " $name";
233 }
234 }
235  
236 # Do it 100x for very fast test code (to increase weight compared to Benchmark overhead).
237 0 ▶sub Benchmark(&@) {
Subroutine Benchmark: not called CC=1 SCAR=6.9
238 0 my( $code, @rest ) = @_;
239 0 ▶ benchmark { for my $i (0..99) { &$code() }} @rest;
Subroutine __ANON__: not called CC=2 SCAR=17.9
240 }
241  
242 0 ▶sub Config(@) {
Subroutine Config: not called CC=5 SCAR=34.0
243 0 require Config;
244 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
245 #{map { pairgrep { $a =~ /$_/ } %Config::Config } @_}; # needs 5.20
246 0 my %config;
247 0 ▶ while( my( $k, $v ) = each %Config::Config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Config::Config) 0 0 0
248 $k =~ $_ and $config{$k} = $v, last
249 0 ▶ for @_;
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /$_/u and $config{$k} = $v, last 0 0 0
250 }
251 0 \%config;
252 } else {
253 0 \%Config::Config;
254 }
255 }
256  
257 36 ▶sub Date(@) {
Subroutine Date: called CC=28 SCAR=35.0
258 36 my( $s, $us, $tz, $tzo );
259 36 require Time::HiRes;
260 36 for( @_ ) {
261 84 ▶ if( ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_) { } 6 78 84
elsif (/^([-+]?)(1[0-4]|0?[0-9])(?:\.([0-9])([0-9])?|:([0-5])?([0-9]))?$/iu) { } 36 42 78
elsif (/^([-+]?)(?=.)([0-9]*)(?:\.([0-9]*))?$/iu) { } 42 0 42
262 6 ($s, $us) = @$_;
263 } elsif( /^([-+]?)(1[0-4]|0?[0-9])(?:\.([0-9])([0-9])?|:([0-5])?([0-9]))?$/i ) {
264 36 ▶ $tz = $2 * 3600 + ($3 ? ($3.($4//0)) * 36 : $6 ? (($5//0).$6) * 60 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$3 ? : 6 30 36
$6 ? : 6 24 30
Condition50%2/4
$4 // 0
l!l
06
$5 // 0
l!l
60
MC/DC0%0/1
$4 // 0
$4
MC/DC0%0/1
$5 // 0
$5
Truth table50%1/2
A
A= $4
Aresult
11
00
Truth table50%1/2
A
A= $5
Aresult
11
00
265 36 ▶ $tzo = sprintf ' %s%02d:%02d', $1 || '+', $tz / 3600, $tz % 3600 / 60;
Condition100%2/2
$1 || '+'
l!l
1224
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
266 36 ▶ $tz = -$tz if $1 eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "-" 6 30 36
267 } elsif( /^([-+]?)(?=.)([0-9]*)(?:\.([0-9]*))?$/i ) {
268 42 ▶ my $nus = substr +($3 // '') . '0'x6, 0, 6;
Condition100%2/2
$3 // ""
l!l
2418
MC/DC100%1/1
$3 // ""
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
269 42 ▶ if( $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) { } 12 30 42
270 12 ▶ ($s, $us) = Time::HiRes::gettimeofday() unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 12 12
271 12 ▶ if( $1 eq '+' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq '+') { } 6 6 12
272 6 ▶ $s += $2 // 0;
Condition50%1/2
$2 // 0
l!l
60
MC/DC0%0/1
$2 // 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
273 6 $us += $nus;
274 6 ▶ ++$s, $us -= 1e6 if $us >= 1e6;
Branch: 1 branch
BranchTrueFalseTotal
if $us >= 1000000 0 6 6
275 } else {
276 6 ▶ $s -= $2 // 0;
Condition50%1/2
$2 // 0
l!l
60
MC/DC0%0/1
$2 // 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
277 6 $us -= $nus;
278 6 ▶ --$s, $us += 1e6 if $us < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $us < 0 0 6 6
279 }
280 } else {
281 30 ▶ $s = $2 // 0;
Condition50%1/2
$2 // 0
l!l
300
MC/DC0%0/1
$2 // 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
282 30 $us = $nus;
283 }
284 }
285 }
286 36 ▶ ($s, $us) = Time::HiRes::gettimeofday() unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 36 36
287 36 ▶ if( $pl::isodate ) {;
Branch: 1 branch
BranchTrueFalseTotal
if ($pl::isodate) { } 24 12 36
288 24 ▶ my @time = reverse +(defined( $tz ) ? gmtime $s + $tz : localtime $s)[0..5];
Branch: 1 branch
BranchTrueFalseTotal
defined $tz ? : 24 0 24
289 24 ▶ $s = sprintf '%04d-%02d-%02dT%02d:%02d:%02d.%06d%s', 1900 + shift( @time ), 1 + shift( @time ), @time, $us, $tzo // '';
Condition50%1/2
$tzo // ""
l!l
240
MC/DC0%0/1
$tzo // ""
$tzo
Truth table50%1/2
A
A= $tzo
Aresult
11
00
290 } else {
291 12 ▶ $s = defined( $tz ) ? gmtime $s + $tz : localtime $s;
Branch: 1 branch
BranchTrueFalseTotal
defined $tz ? : 12 0 12
292 12 ▶ substr $s, 19, 0, $tzo if $tzo;
Branch: 1 branch
BranchTrueFalseTotal
if $tzo 12 0 12
293 12 ▶ substr $s, 19, 0, sprintf '.%06d', $us // 0;
Condition50%1/2
$us // 0
l!l
120
MC/DC0%0/1
$us // 0
$us
Truth table50%1/2
A
A= $us
Aresult
11
00
294 }
295 36 ▶ defined wantarray ? $s : say $s;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 30 6 36
296 }
297  
298 24 ▶sub Isodate(@) {
Subroutine Isodate: called CC=1 SCAR=0.0
299 24 local $pl::isodate = 1;
300 24 &Date;
301 }
302  
303 # turns list of ipv4/6 addresses & hostnames or $_ into /etc/hosts format
304 # todo comment not found
305 0 ▶sub hosts(@) {
Subroutine hosts: not called CC=10 SCAR=47.0
306 0 require Socket;
307 package Socket;
308 0 my( %res, %name );
309 0 ▶ my $res = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
310 0 my( $v4, $name, $aliases, undef, undef, @addr ) = @_;
311 0 undef $name{$_} for $name, split ' ', $aliases;
312 0 for( @addr ) {
313 0 $_ = unpack 'H*', $_;
314 0 ▶ if( $v4 ) { # make sortable by kind
Branch: 1 branch
BranchTrueFalseTotal
if ($v4) { } 0 0 0
315 0 ▶ s/^(?=7f)/g/ or # loopback
Branch: 1 branch
BranchTrueFalseTotal
s/^(?=7f)/g/u or s/^(?=a9fe)/i/u or s/^(?=0a|ac1|c0a8)/k/u or substr $_, 0, 0, "m" 0 0 0
Condition0%0/6
s/^(?=7f)/g/u or s/^(?=a9fe)/i/u or s/^(?=0a|ac1|c0a8)/k/u
l!l&&r!l&&!r
000
s/^(?=7f)/g/u or s/^(?=a9fe)/i/u
l!l&&r!l&&!r
000
MC/DC0%0/3
s/^(?=7f)/g/u or s/^(?=a9fe)/i/u or s/^(?=0a|ac1|c0a8)/k/u
s/^(?=7f)/g/u s/^(?=a9fe)/i/u s/^(?=0a|ac1|c0a8)/k/u
Truth table0%0/4
A or B or C
A= s/^(?=7f)/g/uB= s/^(?=a9fe)/i/uC= s/^(?=0a|ac1|c0a8)/k/u
ABCresult
1XX1
01X1
0011
0000
316 s/^(?=a9fe)/i/ or # link local
317 s/^(?=0a|ac1|c0a8)/k/ or # private
318 substr $_, 0, 0, 'm';
319 } else {
320 0 ▶ s/^(?=0+1$)/h/ or # loopback
Branch: 1 branch
BranchTrueFalseTotal
s/^(?=0+1$)/h/u or s/^(?=fe[89ab])/j/u or s/^(?=fd)/l/u or substr $_, 0, 0, "n" 0 0 0
Condition0%0/6
s/^(?=0+1$)/h/u or s/^(?=fe[89ab])/j/u or s/^(?=fd)/l/u
l!l&&r!l&&!r
000
s/^(?=0+1$)/h/u or s/^(?=fe[89ab])/j/u
l!l&&r!l&&!r
000
MC/DC0%0/3
s/^(?=0+1$)/h/u or s/^(?=fe[89ab])/j/u or s/^(?=fd)/l/u
s/^(?=0+1$)/h/u s/^(?=fe[89ab])/j/u s/^(?=fd)/l/u
Truth table0%0/4
A or B or C
A= s/^(?=0+1$)/h/uB= s/^(?=fe[89ab])/j/uC= s/^(?=fd)/l/u
ABCresult
1XX1
01X1
0011
0000
321 s/^(?=fe[89ab])/j/ or # link local
322 s/^(?=fd)/l/ or # private
323 substr $_, 0, 0, 'n';
324 }
325 0 @{$res{$_}}{keys %name} = (); # don't just store %name, different names might point to same IP but not vice versa
326 }
327 0 };
328 0 my @unpack = (\&unpack_sockaddr_in6, \&unpack_sockaddr_in);
329 0 ▶ for my $name ( @_ ? @_ : $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
330 0 for( getaddrinfo( $name, undef, {socktype => SOCK_STREAM()} )) {
331 0 ▶ next unless ref; # 1st is return code
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 0 0
332 0 %name = ();
333 0 ▶ undef $name{$_->{canonname}} if defined $_->{canonname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"canonname"} 0 0 0
334 0 my $v4 = $_->{family} == AF_INET();
335 0 my $addr = $unpack[$v4]->( $_->{addr} );
336 0 my @get = gethostbyaddr( $addr, $_->{family} );
337 0 ▶ &$res( $v4, @get ? @get : ($name, (undef)x3, $addr) );
Branch: 1 branch
BranchTrueFalseTotal
@get ? : 0 0 0
338 }
339 }
340 0 for( sort keys %res ) {
341 0 ▶ next if 1 == length; # IPv6 on old perl
Branch: 1 branch
BranchTrueFalseTotal
if 1 == length $_ 0 0 0
342 0 my $ip = pack 'H*', substr $_, 1;
343 ::echo 4 == length $ip ? inet_ntoa( $ip ) : inet_ntop( AF_INET6(), $ip ),
344 0 ▶ sort grep ! /^[0-9.]+$|^(?=.*:)[0-9a-f:]+$/i, keys %{$res{$_}};
Branch: 1 branch
BranchTrueFalseTotal
4 == length $ip ? : 0 0 0
345 }
346 }
347  
348 # Fill keydiff arrays
349 160 ▶sub keydiff(;$$) {
Subroutine keydiff: called CC=3 SCAR=11.0
350 160 my $val;
351 160 ▶ if( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 20 140 160
352 20 $val = $_[1];
353 } else {
354 140 chomp( $val = $_ );
355 }
356 160 ▶ $KEYDIFF{$_[0] // $1}[$ARGIND] = $val;
Condition66%2/3
$_[0] // $1
l!l&&r!l&&!r
80800
MC/DC0%0/2
$_[0] // $1
$_[0] $1
Truth table66%2/3
A // B
A= $_[0]B= $1
ABresult
1X1
011
000
357 }
358 40 ▶sub Keydiff(;$$) {
Subroutine Keydiff: called CC=3 SCAR=12.5
359 40 ▶ my $key = $FIELD[$_[0] // 0];
Condition50%1/2
$_[0] // 0
l!l
040
MC/DC0%0/1
$_[0] // 0
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
360 40 ▶ if( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 40 40
361 0 keydiff $key, $_[1];
362 } else {
363 40 keydiff $key;
364 }
365 }
366  
367 # trim small or big if negative values from hash (default %NUMBER) at any depth
368 8 ▶sub Number(;$\[%@]) {
Subroutine Number: called CC=14 SCAR=26.5
369 8 ▶ my $n = $_[0] // 2;
Condition100%2/2
$_[0] // 2
l!l
71
MC/DC100%1/1
$_[0] // 2
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
370 8 ▶ my $ref = $_[1] // \%NUMBER;
Condition100%2/2
$_[1] // \%main::NUMBER
l!l
53
MC/DC100%1/1
$_[1] // \%main::NUMBER
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
371 8 ▶ if( 'HASH' eq ref $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $ref) { } 5 3 8
372 5 for( keys %$ref ) {
373 20 ▶ if( ref $ref->{$_} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->{$_}) { } 0 20 20
elsif (ref $n ? !&$n($ref, $_, $ref->{$_}) : ($n < 0 ? $ref->{$_} > -$n : $ref->{$_} < $n)) { } 10 10 20
374 0 Number( $n, $ref->{$_} );
375 ▶ } elsif( ref $n ? ! $n->( $ref, $_, $ref->{$_} )
Branch: 2 branches
BranchTrueFalseTotal
ref $n ? : 4 16 20
$n < 0 ? : 8 8 16
376 : $n < 0 ? $ref->{$_} > -$n
377 : $ref->{$_} < $n ) {
378 10 delete $ref->{$_};
379 }
380 }
381 } else {
382 3 ▶ for( my $i = @$ref; $i >= 0; --$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 20 3 23
383 20 ▶ if( ref $ref->[$i] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref->[$i]) { } 2 18 20
elsif (ref $n ? !&$n($ref, $i, $ref->[$i]) : ($n < 0 ? $ref->[$i] > -$n : $ref->[$i] < $n)) { } 9 9 18
384 2 Number( $n, $ref->[$i] );
385 ▶ } elsif( ref $n ? ! $n->( $ref, $i, $ref->[$i] )
Branch: 2 branches
BranchTrueFalseTotal
ref $n ? : 10 8 18
$n < 0 ? : 8 0 8
386 : $n < 0 ? $ref->[$i] > -$n
387 : $ref->[$i] < $n ) {
388 9 splice @$ref, $i, 1;
389 }
390 }
391 }
392 }
393  
394 # Pipe command to CODE
395 0 ▶sub piped(&$@) {
Subroutine piped: not called CC=3 SCAR=24.8
396 0 my $code = shift;
397 0 ▶ open my $fd, "-|", @_ or die "$_[0]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "-|", @_ or die "$_[0]: $!\n" 0 0 0
398 0 ▶ &$code() while <$fd>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fd) and do { &$code() } 0 0 0
399 }
400  
401 0 ▶sub help(;$) {
Subroutine help: not called CC=3 SCAR=24.8
402 0 ▶ if( @_ && ! defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not defined $_[0]) 0 0 0
Condition0%0/3
@_ and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not defined $_[0]
@_ not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_B= not defined $_[0]
ABresult
0X0
100
111
403 0 print <<\EOF;
404 usage: pl {-{BbeE} program} [-o] [-Vversion] [-perlopt...] [--] [main program] [arg ...]
405 -Aprog map prog over @A (or undef) returning new @A
406 -bprog & -eprog wrap begin/end program around each file in -n/-p...
407 -Bprog & -Eprog wrap begin/end program around program in same scope, my-vars work
408 -o[number] assume "for(@A) { ... }" loop around main program or array of number args
409 -O[number] like -o but use $A as loop var
410 -p[number] print on each loop (also -o/-O) iteration, at most number times
411 -P[number] like -p but print only if main program evaluates to true, like grep
412 -r reset "$." and -p/-P counter for each file
413 -VVERSION rerun with given version, which is just appended to "perl"
414 --color[=when] colorize the output; when can be 'never', 'always', or 'auto' (the default)
415 These options are handled by perl:
416 EOF
417 0 ▶ piped { print if /^\s+-[0acCdDfFiIlmMntTuUvwWX]/ } $^X, '-h';
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+-[0acCdDfFiIlmMntTuUvwWX]/u 0 0 0
418 }
419 0 print <<\EOF;
420 Predefined functions:
421 b { } name, arg... benchmark slow code for 10s, display name, looping over args.
422 B { } name, arg... same but run code 100 times in benchmark, to reduce overhead.
423 C re... %Config, e.g. C->{sitelib}, optionally only part matching regexps
424 D [arg...][, tz] Date (from arg [s, us], s{.us}, offset [+-]s{.us}, tz ([+-]0-14{:mm|.ff})
425 e arg... echo prettified args or $_ with spaces and newline
426 E arg... same but no newline
427 f fmt, arg... format prettified args with newline, index can be "%1:" instead of "%1\$"
428 F fmt, arg... same but no newline
429 h ip|name... dns-lookup as hosts file
430 I [arg...][, tz] Isodate (from arg [s, us], s{.us}, offset [+-]s{.us}, tz ([+-]0-14{:mm|.ff})
431 k [key, value] store value or chomped $_ in $K{key or $1}[$I] for keydiff
432 K [number, value] same but key is $F[number] or $F[0]
433 N [n, arg] trim arg (default %N) values < n or > -n (default 2) e.g.; -EN or -E 'N -5'
434 p { } cmd, arg... open pipe from cmd and loop over it.
435 t [tmpl, arg...] fill args (key, value...|\%hash, default %T) into template or $_
436 T [tmpl, arg...] same but no newline
437 Predefined & magic variables:
438 *A A, $A & @A are aliases to ARGV, $ARGV & @ARGV
439 $I 0..n index of ARG currently being processed in -o, -n or -p
440 %K ()[] at end, sort by keys, print keydiff of $I array elements. Filled by k.
441 %N () at end, sort numerically by values
442 $q '
443 $Q "
444 *R undef () {} at end, print each, if defined or not empty, %RESULT sorted by keys
445 %T () Default template values, if none given
446 EOF
447 }
448  
449
450  
451 # \todo help doc readline test:my
452 {
453 package pl;
454  
455 our( %c, %template );
456  
457 # It's annoyingly hard to figure out if all are unique & lexically compatible numbers, or whether to sort textually.
458 175 ▶ sub sort_keys(\%) {
Subroutine sort_keys: called CC=22 SCAR=30.9
459 175 my $hash = $_[0];
460 175 ▶ return () unless keys %$hash;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$hash 148 27 175
461 27 ▶ goto TXT if $::TXT_SORT;
Branch: 1 branch
BranchTrueFalseTotal
if $main::TXT_SORT 1 26 27
462 26 my( $hex, $perl, $no_oct, %seen, @seen_oct ) = $::HEX_SORT;
463 26 for( keys %$hash ) {
464 165 ▶ goto TXT if /[^0-9a-fx._+-]/i;
Branch: 1 branch
BranchTrueFalseTotal
if /[^0-9a-fx._+-]/iu 1 164 165
465 164 ▶ $hex = 0, last unless /^[0-9a-f](?:_?[0-9a-f]+)*$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[0-9a-f](?:_?[0-9a-f]+)*$/iu 6 158 164
466 158 ▶ goto TXT if exists $seen{hex $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{hex $_} 2 156 158
467 156 undef $seen{hex $_};
468 }
469 23 ▶ return sort { hex $a <=> hex $b } keys %$hash if $hex;
Branch: 1 branch
BranchTrueFalseTotal
if $hex 17 6 23
470  
471 6 %seen = ();
472 6 for( keys %$hash ) {
473 72 ▶ unless( $no_oct ||= /^[+-]?0(?=.*[89])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_oct ||= /^[+-]?0(?=.*[89])/u) 69 3 72
Condition100%3/3
$no_oct ||= /^[+-]?0(?=.*[89])/u
l!l&&r!l&&!r
2169
MC/DC100%2/2
$no_oct ||= /^[+-]?0(?=.*[89])/u
$no_oct /^[+-]?0(?=.*[89])/u
Truth table100%3/3
A ||= B
A= $no_octB= /^[+-]?0(?=.*[89])/u
ABresult
1X1
011
000
474 69 ▶ if( /^[+-]?0(?:b(?:_?[01]+)*|x(?:_?[0-9a-f]+)*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[+-]?0(?:b(?:_?[01]+)*|x(?:_?[0-9a-f]+)*)$/iu) 14 55 69
475 14 ▶ goto TXT if exists $seen{eval $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{eval $_} 1 13 14
476 13 undef $seen{eval $_};
477 13 $perl = 1, next;
478 }
479 55 ▶ if( /^[+-]?0(?:_?[0-7]+)*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[+-]?0(?:_?[0-7]+)*$/u) 13 42 55
480 13 push @seen_oct, $_; # can't decide yet
481 13 next;
482 }
483 }
484  
485 45 ▶ if( /^[+-]?(?=.)[0-9]*(?:\.[0-9]*)?(?:(?<=.)e[+-]?[0-9]+)?$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[+-]?(?=.)[0-9]*(?:\.[0-9]*)?(?:(?<=.)e[+-]?[0-9]+)?$/iu) 44 1 45
486 44 ▶ goto TXT if exists $seen{0 + $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{0 + $_} 1 43 44
487 43 undef $seen{0 + $_};
488 43 next;
489 }
490  
491 1 goto TXT;
492 }
493 3 ▶ if( $perl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perl) { } 2 1 3
494 2 ▶ goto TXT if $no_oct;
Branch: 1 branch
BranchTrueFalseTotal
if $no_oct 1 1 2
495 1 for( @seen_oct ) {
496 3 ▶ goto TXT if exists $seen{eval $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{eval $_} 0 3 3
497 3 undef $seen{eval $_};
498 }
499 1 return sort { eval $a <=> eval $b } keys %$hash;
500 } else {
501 1 for( @seen_oct ) {
502 2 ▶ goto TXT if exists $seen{0 + $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{0 + $_} 0 2 2
503 2 undef $seen{0 + $_};
504 }
505 1 return sort { $a <=> $b } keys %$hash;
506 }
507  
508 8 TXT: # clearest solution here
509 sort keys %$hash;
510 }
511  
512  
513 8 ▶ sub keydiff() {
Subroutine keydiff: called CC=19 SCAR=43.0
514 8 ▶ goto &_keydiff if $c{E} && eval { require Algorithm::Diff };
Branch: 1 branch
BranchTrueFalseTotal
if $pl::c{"E"} and eval { do { require Algorithm::Diff } } 7 1 8
Condition66%2/3
$pl::c{"E"} and eval { do { require Algorithm::Diff } }
!ll&&!rl&&r
107
MC/DC50%1/2
$pl::c{"E"} and eval { do { require Algorithm::Diff } }
$pl::c{"E"} eval { do { require Algorithm::Diff } }
Truth table66%2/3
A and B
A= $pl::c{"E"}B= eval { do { require Algorithm::Diff } }
ABresult
0X0
100
111
515 1 for my $key ( sort_keys %::KEYDIFF ) {
516 9 $#{$::KEYDIFF{$key}} = $ARGIND - 1; # lengthen list if needed
517 9 my $str;
518 9 ▶ next if ::all { ($str ||= $_) eq $_ if defined } @{$::KEYDIFF{$key}};
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
if &main::all(sub { ($str ||= $_) eq $_ if defined $_; } , @{$main::KEYDIFF{$key};}) 1 8 9
if defined $_ 15 4 19
Condition66%2/3
$str ||= $_
l!l&&r!l&&!r
780
MC/DC0%0/2
$str ||= $_
$str $_
Truth table66%2/3
A ||= B
A= $strB= $_
ABresult
1X1
011
000
519 8 say "$c{B}$key$c{E}";
520 8 my( $begin, $end );
521 8 ▶ if( $c{E} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pl::c{"E"}) 0 8 8
522 0 for( @{$::KEYDIFF{$key}} ) {
523 0 ▶ if( !defined ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
elsif (defined $end) { } 0 0 0
524 } elsif( defined $end ) {
525 0 ▶ chop $begin while length $begin and !/\A\Q$begin/;
Branch: 1 branch
BranchTrueFalseTotal
length $begin and not /\A\Q$begin\E/u and do { chop $begin } 0 0 0
Condition0%0/3
length $begin and not /\A\Q$begin\E/u
!ll&&!rl&&r
000
MC/DC0%0/2
length $begin and not /\A\Q$begin\E/u
length $begin not /\A\Q$begin\E/u
Truth table0%0/3
A and not(B)
A= length $beginB= not /\A\Q$begin\E/u
ABresult
0X0
100
111
526 #substr( $begin, -1 ) = '' until !length $begin or /^\Q$begin/;
527 0 ▶ substr( $end, 0, 1 ) = '' while length $end and !/\Q$end\E\Z/;
Branch: 1 branch
BranchTrueFalseTotal
length $end and not /\Q$end\E\Z/u and do { substr($end, 0, 1) = "" } 0 0 0
Condition0%0/3
length $end and not /\Q$end\E\Z/u
!ll&&!rl&&r
000
MC/DC0%0/2
length $end and not /\Q$end\E\Z/u
length $end not /\Q$end\E\Z/u
Truth table0%0/3
A and not(B)
A= length $endB= not /\Q$end\E\Z/u
ABresult
0X0
100
111
528 } else {
529 0 $begin = $end = $_;
530 }
531 }
532 0 $begin = length $begin;
533 0 $end = length $end;
534 #$_ = length for $begin, $end;
535 }
536 say "\t",
537 !defined() ? "$c{I}n/a$c{E}" :
538 !defined( $end ) ? $_ :
539 $begin == length() ? "$c{G}$_$c{E}" :
540  
541 ($begin ? $c{G} . substr( $_, 0, $begin ) : '') .
542 $c{R} . substr( $_, $begin, length() - $begin - $end ) .
543 ($end ? $c{G} . substr $_, length() - $end : '') .
544 $c{E}
545  
546 8 ▶ for @{$::KEYDIFF{$key}};
Branch: 5 branches
BranchTrueFalseTotal
!defined($_) ? : 7 17 24
!defined($end) ? : 17 0 17
$begin == length $_ ? : 0 0 0
$begin ? : 0 0 0
$end ? : 0 0 0
547 }
548 }
549 7 ▶ sub _keydiff() {
Subroutine _keydiff: called CC=14 SCAR=26.4
550 7 for my $key ( sort_keys %::KEYDIFF ) {
551 63 my( $max, $n, $ref ) = (0, 0);
552 63 for( @{$::KEYDIFF{$key}} ) {
553 161 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 21 140 161
554 140 ▶ $max = length if $max < length;
Branch: 1 branch
BranchTrueFalseTotal
if $max < length $_ 77 63 140
555 140 ++$n;
556 140 ▶ if( $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 77 63 140
557 77 $ref = Algorithm::Diff::LCS( $ref, [split //] );
558 } else {
559 63 $ref = [split //];
560 }
561 }
562 63 ▶ next if $n == $ARGIND && @$ref == $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $main::ARGIND and @$ref == $max 7 56 63
Condition100%3/3
$n == $main::ARGIND and @$ref == $max
!ll&&!rl&&r
42147
MC/DC100%2/2
$n == $main::ARGIND and @$ref == $max
$n == $main::ARGIND @$ref == $max
Truth table100%3/3
A and B
A= $n == $main::ARGINDB= @$ref == $max
ABresult
0X0
100
111
563 56 say "$c{B}$key$c{E}";
564 56 $#{$::KEYDIFF{$key}} = $ARGIND - 1; # lengthen list if needed
565 56 for( @{$::KEYDIFF{$key}} ) {
566 168 ▶ if( defined ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 119 49 168
567 119 ▶ if( ! @$ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @$ref) { } 0 119 119
elsif (@$ref == length $_) { } 21 98 119
568 0 substr $_, 0, 0, $c{R};
569 } elsif( @$ref == length ) {
570 21 substr $_, 0, 0, $c{G};
571 } else {
572 98 my( undef, $idx ) = Algorithm::Diff::LCSidx( $ref, [split //] );
573 98 for my $i ( reverse @$idx ) {
574 2511 substr $_, $i + 1, 0, $c{R};
575 2511 substr $_, $i, 0, $c{G};
576 }
577 98 substr $_, 0, 0, $c{R};
578 98 s/\e\[3.m(?=\e\[3.m|$)//g;
579 98 ▶ () while s/(\e\[3.m)[^\e]+\K\1//;
Branch: 1 branch
BranchTrueFalseTotal
s/(\e\[3.m)[^\e]+\K\1//u and do { } 2191 98 2289
580 }
581 } else {
582 49 $_ = "$c{I}n/a";
583 }
584 168 say "\t$_$c{E}";
585 }
586 }
587 }
588  
589 0 ▶ sub getline {
Subroutine getline: not called CC=4 SCAR=30.0
590 0 ▶ { local $\ = ''; print STDERR defined() ? '>> ' : '> ' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
591 0 my $part = <>;
592 0 ▶ if( defined $part ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $part) { } 0 0 0
593 0 ▶ if( defined ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 0 0 0
594 0 $_ .= "\n" . $part;
595 } else {
596 0 $_ = $part;
597 }
598 } else {
599 0 say '';
600 0 exit;
601 }
602 }
603 0 ▶ sub shell {
Subroutine shell: not called CC=7 SCAR=40.3
604 # No my, as that would inject into eval.
605 0 our $lp = eval { require Lexical::Persistence; Lexical::Persistence->new() };
606 0 while( 1 ) {
607 0 our $lines = undef;
608 0 for( $lines ) {
609 0 &getline;
610 0 ▶ &getline while s/\\$//s;
Branch: 1 branch
BranchTrueFalseTotal
s/\\$//su and do { &getline } 0 0 0
611 0 ▶ if( /\{$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\{$/su) 0 0 0
612 0 ▶ &getline until /\n\}$/s;
Branch: 1 branch
BranchTrueFalseTotal
/\n\}$/su or do { &getline } 0 0 0
613 }
614 }
615 {
616 0 package main;
617 0 ▶ $lp ? $lp->do( $lines ) : eval $lines;
Branch: 1 branch
BranchTrueFalseTotal
$pl::lp ? : 0 0 0
618 }
619 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
620 }
621 }
622 }
623  
624
625  
626 # Put this before compiling caller's main program, as these get run LIFO.
627 83 ▶END {
Subroutine END: called CC=6 SCAR=18.0
628 83 ▶ echo for $RESULT // (), @RESULT;
Condition66%2/3
$main::RESULT // ()
l!l&&r!l&&!r
1082
MC/DC50%1/2
$main::RESULT // ()
$main::RESULT ()
Truth table66%2/3
A // B
A= $main::RESULTB= ()
ABresult
1X1
011
000
629 form "%s: %s", $_, $RESULT{$_}
630 83 for pl::sort_keys %RESULT;
631  
632 # todo lenint.lenfloat %d/f
633 printf "%8d: %s\n", $NUMBER{$_}, $_
634 83 for sort { $NUMBER{$a} <=> $NUMBER{$b} } pl::sort_keys %NUMBER;
635 83 ▶ &pl::keydiff if keys %KEYDIFF;
Branch: 1 branch
BranchTrueFalseTotal
if keys %main::KEYDIFF 8 75 83
636 }
637  
638
639  
640 83 ▶BEGIN {
Subroutine BEGIN: called CC=38 SCAR=37.9
641 83 $pl::Echo = \&Echo;
642 83 *I = \$ARGIND;
643 83 *FIELD = \@::F;
644 83 eval '*' . substr( $_, 1, 1 ) . " = \\$_" for
645 qw'*ARGV &benchmark &Benchmark &Config &Date &echo &Echo &form &Form &hosts $HEX_SORT &Isodate &keydiff &Keydiff %KEYDIFF
646 &Number %NUMBER &piped $quote $Quote *RESULT &template &Template %TEMPLATE $TXT_SORT';
647  
648 83 ($ARGIND, $quote, $Quote, $H) = (0, "'", '"', 1);
649 83 ▶ unless( @pl::opts || @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@pl::opts or @ARGV) 0 83 83
Condition66%2/3
@pl::opts or @ARGV
l!l&&r!l&&!r
59240
MC/DC0%0/2
@pl::opts or @ARGV
@pl::opts @ARGV
Truth table66%2/3
A or B
A= @pl::optsB= @ARGV
ABresult
1X1
011
000
650 0 *pl::prog = \&pl::shell;
651 0 return;
652 }
653  
654 {
655 # Assemble a program that works under perl -n, etc., while adding in pl's options.
656 # This is wild stuff, as it has to blend in various options, while potentially accommodating an outer loop.
657 83 my @prog = "use feature ':'.substr \$^V, 1;\n"; # Enable latest optional features.
658 83 @prog[2, 4, 11, 13, 22] =
659 ('sub pl::prog { $pl::last = 1;',
660 'LINE: {', # dummy loop
661 '} continue {', # program didn't do last
662 '$pl::last = 0 }',
663 '}');
664 83 ▶ while( @pl::opts ) { # no for, need to shift
Branch: 1 branch
BranchTrueFalseTotal
if (@pl::opts) 76 83 159
665 76 $_ = shift @pl::opts;
666 76 ▶ if( /^--color(?:()|=(a)lways|=(n)ever|=auto)$/s ) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^--color(?:()|=(a)lways|=(n)ever|=auto)$/su) { } 8 68 76
elsif (/[?h]/u) { } 0 68 68
elsif (/v/u) { } 0 68 68
667 8 ▶ $pl::c = defined( $1 ) || $2 ? 1 : $3 ? 0 : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $1 || $2 ? : 7 1 8
$3 ? : 1 0 1
Condition100%3/3
defined $1 or $2
l!l&&r!l&&!r
521
MC/DC100%2/2
defined $1 or $2
defined $1 $2
Truth table100%3/3
A or B
A= defined $1B= $2
ABresult
1X1
011
000
668 8 next;
669 } elsif( /[?h]/ ) {
670 0 help undef;
671 0 exit;
672 } elsif( /v/ ) {
673 0 say 'This is ' . ($0 =~ s!.*[/\\]!!r) . " $pl::VERSION, with perl $^V
674  
675 Copyright 1997-2023, Daniel Pfeiffer
676  
677 Pl may be copied only under the terms of either the Artistic License or the
678 GNU General Public License, which may be found in the Perl 5 source kit.";
679 0 exit;
680 }
681  
682 68 ▶ if( /([ABbEe])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/([ABbEe])/u) 51 17 68
683 # put ; after \n as user may end with a comment
684 51 my $bit = "\n#line 1 \"-$1 option\"\n$pl::opts[0]\n;";
685 51 my $block = shift( @pl::opts ) =~ /\A\{.+\}\Z/s;
686 51 ▶ if( /A/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if (/A/u) { } 5 46 51
elsif (/b/u) { } 6 40 46
elsif (/B/u) { } 22 18 40
elsif (/e/u) { } 6 12 18
687 5 chop $bit;
688 5 ▶ $prog[0] .= '@ARGV = ' . ($block ? "grep $bit" : "map { $bit }") . " \@ARGV ? \@ARGV : undef;\n";
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 2 3 5
689 } elsif( /b/ ) {
690 # Do it each time ARGV changes
691 6 $prog[5] = 'if( ($pl::exARGV //= "") ne $ARGV ) {' . $bit . '$pl::exARGV = $ARGV; }';
692 } elsif( /B/ ) {
693 22 ▶ $prog[0] .= $block ? "BEGIN$bit" : $bit;
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 0 22 22
694 } elsif( /e/ ) {
695 # append 1 to not leak any wantarray into $bit
696 6 $prog[8] = "\nINIT { \$pl::_e = sub { $bit 1 }}";
697 6 $pl::_e = '&$pl::_e();';
698 } else {
699 12 $prog[9] = "\n;END { $bit 1 }";
700 }
701 }
702 68 ▶ if( /o([1-9][0-9]*)?/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/o([1-9][0-9]*)?/iu) 16 52 68
703 16 ▶ if( my $o = $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $1) { } 2 14 16
704 2 ▶ @prog[3, 14] =
Branch: 1 branch
BranchTrueFalseTotal
/o/u ? : 1 1 2
705 ('while( @ARGV > $ARGIND ) { sub {' . (/o/ ? '$_' : '$ARGV') . ' = \@_ }->( @ARGV[$ARGIND..$ARGIND+' . ($o - 1) . '] );', # hack to ref into @ARGV
706 '} continue { $ARGIND += ' . $o . '; last if $pl::last }')
707 } else {
708 14 ▶ @prog[3, 14] =
Branch: 1 branch
BranchTrueFalseTotal
/O/u ? : 2 12 14
709 (/O/ ? 'for $ARGV (@ARGV) {' : 'for(@ARGV) {',
710 '} continue { ++$ARGIND; last if $pl::last }');
711 }
712 }
713 68 ▶ if( /p([1-9][0-9]*)?/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/p([1-9][0-9]*)?/iu) 14 54 68
714 14 my $p = $1;
715 14 $prog[1] = '$pl::_pn = 0;';
716 14 ▶ @prog[6, 10] =
Branch: 1 branch
BranchTrueFalseTotal
if /P/u 4 10 14
717 ('$pl::_P = do {',
718 '}') if /P/;
719 14 ▶ $prog[12] = '++$pl::_pn, print or die "-p destination: $!\n"' . (/P/ ? 'if $pl::_P;' : ';');
Branch: 1 branch
BranchTrueFalseTotal
/P/u ? : 4 10 14
720 14 ▶ $prog[13] =~ s/0/\$pl::_pn >= $p ? 2 : 0/ if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 7 7 14
721 }
722 68 ▶ $pl::_r = 1 if /r/;
Branch: 1 branch
BranchTrueFalseTotal
if /r/u 7 61 68
723 }
724 83 ▶ $prog[7] = @ARGV ? "\n#line 1 \"main program\"\n" . shift() . "\n;" : ';';
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 82 1 83
725 #$prog[13] =~ s/\$pl::last =( \$pl::_pn >= \d+)/ if($1 ) { &\$pl::_e() if \$pl::_e; exit }/ unless $pl::_r || $prog[14];
726 83 ▶ $prog[14] //= # ! -[oO]
Branch: 2 branches
BranchTrueFalseTotal
$pl::_r ? : 7 60 67
$pl::_r ? : 7 60 67
Condition100%4/4
$prog[14] //= 'if( $pl::last || eof ) { ++$ARGIND;' . ($pl::_r ? "close ARGV; \$pl::_pn = 0;" : "if( \$pl::last ) { my \$d = \$.; close ARGV; \$. = \$d }") . ($pl::_e // "") . ($pl::_r ? "}" : "exit if \$pl::last == 2 }")
l!l
1667
$pl::_e // ""
l!l
661
MC/DC100%1/1
$prog[14] //= 'if( $pl::last || eof ) { ++$ARGIND;' . ($pl::_r ? "close ARGV; \$pl::_pn = 0;" : "if( \$pl::last ) { my \$d = \$.; close ARGV; \$. = \$d }") . ($pl::_e // "") . ($pl::_r ? "}" : "exit if \$pl::last == 2 }")
$prog[14]
MC/DC100%1/1
$pl::_e // ""
$pl::_e
Truth table100%2/2
A
A= $prog[14]
Aresult
11
00
Truth table100%2/2
A
A= $pl::_e
Aresult
11
00
727 'if( $pl::last || eof ) { ++$ARGIND;' .
728 ($pl::_r ? 'close ARGV; $pl::_pn = 0;' : 'if( $pl::last ) { my $d = $.; close ARGV; $. = $d }') .
729 ($pl::_e // '') .
730 ($pl::_r ? '}' : 'exit if $pl::last == 2 }');
731 # Don't pollute eval with my-vars
732 83 $_ = join '', grep defined, @prog;
733 };
734 #no warnings 'experimental'; # overridden by -W
735 83 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
736 0 ▶ if( ($ENV{PLDUMP} // 0) == 1 ) { open STDOUT, '| perltidy -cb'; say; exit }
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{"PLDUMP"} // 0) == 1) 0 83 83
Condition50%1/2
$ENV{"PLDUMP"} // 0
l!l
083
MC/DC0%0/1
$ENV{"PLDUMP"} // 0
$ENV{"PLDUMP"}
Truth table50%1/2
A
A= $ENV{"PLDUMP"}
Aresult
11
00
737 83 ▶ eval;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine prog: called CC=5 SCAR=16.1
Subroutine __ANON__: called CC=1 SCAR=0.0
738 83 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 83 83
739 0 warn $@;
740 0 exit 255;
741 }
742 83 undef $_;
743 83 ▶ @pl::c{qw(B I G R E)} = $pl::c // (-t STDOUT && $^O !~ /^MSWin/) ?
Branch: 1 branch
BranchTrueFalseTotal
$pl::c // -t STDOUT && !($^O =~ /^MSWin/u) ? : 7 76 83
Condition50%3/6
$pl::c // -t STDOUT && !($^O =~ /^MSWin/u)
l!l&&r!l&&!r
8750
-t STDOUT and not $^O =~ /^MSWin/u
!ll&&!rl&&r
7500
MC/DC33%1/3
$pl::c // -t STDOUT && !($^O =~ /^MSWin/u)
$pl::c -t STDOUT not $^O =~ /^MSWin/u
Truth table50%2/4
A // B and not(C)
A= $pl::cB= -t STDOUTC= not $^O =~ /^MSWin/u
ABCresult
1XX1
0111
00X0
0100
744 map "\e[${_}m", 1, 3, 32, 31, '' :
745 ('')x5;
746  
747 package Data::Dumper;
748 83 ▶ our( $Deparse, $Quotekeys, $Terse, $Sortkeys ) = (1, 0, 1, sub { [&pl::sort_keys] });
Subroutine __ANON__: called CC=1 SCAR=0.0
749 }
750 &pl::prog; # will be called repeatedly if -n
751  
752  
753  
754 =head1 NAME
755  
756 pl - Perl One-Liner Magic Wand
757  
758 =head1 SYNOPSIS
759  
760 Some tasks are too menial for a dedicated script but still too cumbersome
761 even with the many neat one-liner options of C<perl -E>. This small script
762 fills the gap: various one-letter commands & magic variables (with meaningful
763 aliases too) and more nifty loop options take Perl programming to the command
764 line. Fully imports C<List::Util>. With no program on the command line,
765 starts a pl Shell.
766  
767 How to C<e(cho)> values, including from C<@A(RGV)>, with single C<$q(uote)> &
768 double C<$Q(uote)>:
769  
770 pl 'echo "${quote}Perl$quote", "$Quote@ARGV$Quote"' one-liner
771 pl 'e "${q}Perl$q", "$Q@A$Q"' one-liner
772  
773 > 'Perl' "one-liner"
774  
775 Same for hard-to-print values:
776  
777 pl 'echo \"Perl", \@ARGV, undef' one-liner
778 pl 'e \"Perl", \@A, undef' one-liner
779  
780 > \'Perl' [
781 > 'one-liner'
782 > ] undef
783  
784 Loop over args, printing each with line ending. And same, SHOUTING:
785  
786 pl -opl '' Perl one-liner
787 pl -opl '$_ = uc' Perl one-liner
788  
789 > Perl
790 > one-liner
791 > PERL
792 > ONE-LINER
793  
794 Print up to 3 matching lines, resetting count (and C<$.>) for each file:
795  
796 pl -rP3 '/Perl.*one.*liner/' file*
797  
798 Count hits in magic statistics hash C<%N(UMBER)>:
799  
800 pl -n '++$NUMBER{$1} while /(Perl|one|liner)/g' file*
801 pl -n '++$N{$1} while /(Perl|one|liner)/g' file*
802  
803 > 2: one
804 > 7: liner
805 > 9: Perl
806  
807 Though they're sometimes slightly, sometimes quite a bit more complicated,
808 most Perl one-liners from the internet work, just by omitting C<-e> or C<-E>.
809 There's only one main program in pl, but you can just as well concatenate the
810 C<-e>s with C<;>. See L<minor differences|/"Minor Differences with perl -E">
811 for exceptions. Let's see L<many varied examples|https://metacpan.org/dist/App-pl/view/pod/examples.pod>.
812  
813 =head1 DESCRIPTION
814  
815 I<Don't believe everything you read on SourceForge^H^H^H the internet! --B< >Plato :-y>
816  
817 Pl follows Perl's philosophy for one-liners: the one variable solely used in
818 one-liners, C<@F>, is single-lettered. Because not everyone may like that, pl
819 has it both ways. Everything is aliased both as a word and as a single
820 letter, including Perl's own C<@F> & C<*ARGV>.
821  
822 Perl one-liners, and hence pl, are by nature bilingual. You must run the
823 command with its options & arguments, typically from Shell. By design, Perl
824 quotes mimic Shell quotes, so here they collide. As Perl also uses Shell
825 meta-characters like C<$>, the best solution is to protect Perl-code from the
826 Shell with single quotes. That means you can't use them inside. (An ugly way
827 around that, is C<'\''>, which ends a string, backslashes a quote and starts
828 another.) For literal quotes use C<$q(uote)>. For quoting use double quotes
829 or C<q{}>.
830  
831 I<Shell and Perl, unlike most other languages, don't make you stick your toe
832 up your nose to get newlines into strings. Thus, you see long "one-liners" as
833 legible many-liners. You get more features on the L<pl
834 homepage|https://perl1liner.sourceforge.io/>, like in the veggie-burger menu,
835 you can toggle many-line display. In normal text short and long name variants
836 are initial-bold as C<X(YZ)>. All examples use the long names, if applicable.
837 On the homepage those are in the darker blue upper half. They are repeated with the short variant. Many examples are followed by their output, indented with C<< > >>.>
838  
839 =head1 DOCUMENTATION
840  
841 =head2 Options
842  
843 Many of perl's options are also available in pl, sometimes enhanced with extra
844 functionality. And the new options complement what perl offers, specifically
845 oriented towards one-liners.
846  
847 =over
848  
849 =item C<-0I<[octal]>>
850  
851 I<perl:> Specify record separator with B<-n>/B<-p> (C<\0>, if no argument).
852  
853 =item C<-AI<prog>>
854  
855 Map program over already available C<@A(RGV)> (from command line or previous
856 B<-A>) or undef. If you wrap program in C<{}> uses C<grep> instead of C<map>.
857 The result becomes the new C<@A(RGV)>. You can mix it with B<-B>. The 1st
858 two are equivalent, except that the 1st one isn't limited by Shell line length
859 limitations. The third again greps by file size, reading only the Perl
860 modules less than 1E<thinsp>kB:
861  
862 pl -nA '<*.pm>' '...'
863 pl -n '...' *.pm
864 pl -nA '<*.pm>' -A '{ (stat)[7] < 1000 }' '...'
865  
866 =item C<-a>
867  
868 I<perl:> Autosplit mode with B<-n>/B<-p> (splits C<$_> into C<@F(IELD)>).
869  
870 =item C<-bI<prog>>
871  
872 Run program before reading a new file in B<-n>/B<-P>/B<-p>.
873  
874 =item C<-BI<prog>>
875  
876 Add program before main program in same scope. You can use it to initialise
877 C<my> variables. Whereas, if you define a C<my> variable in the main program
878 of a B<-n>, B<-p>, B<-P>, B<-o>, or B<-O> loop, that's a new variable on each
879 iteration. This doesn't do a C<BEGIN> block unless you wrap program in C<{}>.
880 You may mix it with B<-A>.
881  
882 =item C<-c>
883  
884 I<perl:> Check syntax only (runs C<BEGIN> and C<CHECK> blocks).
885  
886 =item C<-CI<[number/list]>>
887  
888 I<perl:> Enables the listed Unicode features.
889  
890 =item C<--colorI<[=when]>>
891  
892 Colorize (people with impairment may adapt their system, terminal, or browser)
893 some of the output; when can be C<never>, C<always>, or C<auto> (the default).
894  
895 =item C<-dI<[:debugger]>>
896  
897 I<perl:> Run program under debugger.
898  
899 =item C<-DI<[number/list]>>
900  
901 I<perl:> Set debugging flags (argument is a bit mask or alphabets).
902  
903 =item C<-eI<prog>>
904  
905 Run program after finishing reading a file in B<-n>/B<-p>.
906  
907 =item C<-EI<prog>>
908  
909 Add an C<END> block after main-program in same scope. So, C<my>-vars work as
910 follows: the C<END> block is a closure of the 1st C<$inner> variable. Perl
911 warns "Variable "$inner" will not stay shared":
912  
913 pl -OB 'my $outer' -E 'echo $inner, $outer' 'my $inner = $outer = $ARGV' a b c
914 pl -OB 'my $outer' -E 'e $inner, $outer' 'my $inner = $outer = $A' a b c
915  
916 > a c
917  
918 =item C<-f>
919  
920 I<perl:> Don't do F<$sitelib/sitecustomize.pl> at startup.
921  
922 =item C<-FI</pattern/>>
923  
924 I<perl:> Provide C<split()> pattern for B<-a> switch (C<//>'s are optional).
925  
926 =item C<-II<directory>>
927  
928 I<perl:> Specify C<@INC>/C<#include> directory (several B<-I>'s allowed).
929  
930 =item C<-iI<[extension]>>
931  
932 I<perl:> Edit C<< <> >> files in place (makes backup if extension supplied).
933  
934 =item C<-n>
935  
936 I<As I said before, I never repeat myself. :-)>
937  
938 I<perl:> Assume C<< while (<>) { ... } >> loop around program. It's a little
939 richer than that: if you use C<last>, it closes the current file, leaving you
940 to continue the loop on the next file.
941  
942 =item C<-oI<[number]>>
943  
944 Assume C<for(@ARGV) { ... }> loop around main program, and C<$ARGIND> (or
945 C<$I>) is the current position. In this case B<-p> doesn't imply B<-n>. If
946 you give number, passes that many args at once as an array, referencing the
947 original values. If there aren't enough on the last round, fills up
948 C<@A(RGV)> with C<undef>s.
949  
950 pl -opl '' I II III IV
951 pl -o3 'echo $ARGIND, @$_' i ii iii iv v vi vii viii ix
952 pl -opl '' I II III IV
953 pl -o3 'e $I, @$_' i ii iii iv v vi vii viii ix
954  
955 > I
956 > II
957 > III
958 > IV
959 > 0 i ii iii
960 > 3 iv v vi
961 > 6 vii viii ix
962  
963 =item C<-OI<[number]>>
964  
965 like B<-o> but use C<@A(RGV)> as loop variable.
966  
967 =item C<-pI<[number]>>
968  
969 I<Does C<pl -penis> do pussy? B< >It implements C<cat>. :-*>
970  
971 I<perl+:> On each loop C<print> (also B<-o> and B<-O>, in which case you must
972 fill C<$_>) iteration. If you give number, prints at most number times.
973  
974 =item C<-PI<[number]>>
975  
976 Like B<-p> but print only if main program evaluates to true, like C<grep>.
977  
978 =item C<-r>
979  
980 Reset C<$.> and B<-p>/B<-P> counter for each file.
981  
982 =item C<-T>
983  
984 I<perl:> Enable tainting checks.
985  
986 =item C<-t>
987  
988 I<perl:> Enable tainting warnings.
989  
990 =item C<-U>
991  
992 I<perl:> Allow unsafe operations.
993  
994 =item C<-u>
995  
996 I<perl:> Dump core after parsing program.
997  
998 =item C<-v>
999  
1000 I<perl:> Print version, patchlevel and license.
1001  
1002 =item C<-VI<version>>
1003  
1004 Rerun with given perl version, which is just a string appended to F<perl>.
1005  
1006 =item C<-W>
1007  
1008 I<perl:> Enable all warnings.
1009  
1010 =item C<-w>
1011  
1012 I<perl:> Enable many useful warnings.
1013  
1014 =item C<-X>
1015  
1016 I<perl:> Disable all warnings.
1017  
1018 =back
1019  
1020 =head2 Functions
1021  
1022 Various functions, always also with a one letter alias, perform little tasks
1023 that can be useful in one-liners.
1024  
1025 =over
1026  
1027 =item C<benchmark { } I<[name[, arg...]]>> |
1028 C<b { } I<[name[, arg...]]>>
1029  
1030 Benchmark slow code for 10E<thinsp>s, display name, looping over args.
1031  
1032 =item C<Benchmark { } I<[name[, arg...]]>> |
1033 C<B { } I<[name[, arg...]]>>
1034  
1035 Same but run code 100 times in benchmark, to reduce overhead.
1036  
1037 =item C<Config I<[regexp...]>> |
1038 C<C I<[regexp...]>>
1039  
1040 Import and return C<%Config>, e.g., C<< Config->{sitelib} >>, optionally only part matching regexps.
1041  
1042 =item C<Date I<[arg...][, tz]>> |
1043 C<D I<[arg...][, tz]>>
1044  
1045 I<Why is Halloween Christmas? Because Oct 31 = Dec 25. (^)>
1046  
1047 Date (from arg [s, us], s{.us}, offset [+-]s{.us}, tz ([+-]0-14{:mm|.ff}).
1048 You should pass microseconds as strings because floats have
1049 implementation-dependent rounding issues. You must pass positive offsets as
1050 strings because otherwise it loses the C<+>. Returns the date, if called in
1051 some context, else echoes it.
1052  
1053 pl 'Date;
1054 $_ = Date -86400, "+3600";
1055 echo $_, " -- ", Date "+8:45"'
1056 pl 'D;
1057 $_ = D -86400, "+3600";
1058 e $_, " -- ", D "+8:45"'
1059  
1060 > Thu Jul 13 20:24:51.009836 2023
1061 > Wed Jul 12 21:24:51.009956 2023 -- Fri Jul 14 03:09:51.009994 +08:45 2023
1062  
1063 =item C<echo I<[arg...]>> |
1064 C<e I<[arg...]>>
1065  
1066 Echo prettified args or C<$_> with spaces and newline. Prettified means,
1067 C<undef> becomes that string, italic if B<--color> is active. Anything that
1068 can be stringified, is. Any other reference goes through C<Data::Dumper>,
1069 which pl loads only if needed.
1070  
1071 If it's called in scalar context (e.g., C<$x = echo ...>) instead return the
1072 same as it would echo, in one string (inspired by Shell C<$(...)>). If it's
1073 called in list context (e.g., C<@l = echo ...>) return each arg prettified
1074 individually, with a newline on the last one.
1075  
1076 =item C<Echo I<[arg...]>> |
1077 C<E I<[arg...]>>
1078  
1079 Same but no newline.
1080  
1081 =item C<form I<format, [arg...]>> |
1082 C<f I<format, [arg...]>>
1083  
1084 Form(at) and echo prettified args or C<$_> with newline. If it's called in
1085 scalar or list context (e.g., C<$x = form ...>) instead return the same as it
1086 would echo, in one string. Parameter index can be C<"%1:"> instead of
1087 C<"%1\$">.
1088  
1089 =item C<Form I<format, [arg...]>> |
1090 C<F I<format, [arg...]>>
1091  
1092 Same but no newline.
1093  
1094 =item C<Isodate I<[arg...][, tz]>> |
1095 C<I I<[arg...][, tz]>>
1096  
1097 Same as C<D(ate)> but uses ISO format.
1098  
1099 pl 'Isodate;
1100 $_ = Isodate 7 * -86400;
1101 echo $_, " -- ", Isodate "+8.75"'
1102 pl 'I;
1103 $_ = I 7 * -86400;
1104 e $_, " -- ", I "+8.75"'
1105  
1106 > 2023-07-13T20:24:51.124180
1107 > 2023-07-06T20:24:51.124266 -- 2023-07-14T03:09:51.124298 +08:45
1108  
1109 =item C<keydiff I<[key[, value]]>> |
1110 C<k I<[key[, value]]>>
1111  
1112 Store value or chomped C<$_> in C<$KEYDIFF{key or $1}[$ARGIND]>. At the
1113 C<END> for each key (which pl sorts numerically if possible) pl diffs all
1114 values.
1115  
1116 =item C<Keydiff I<[number[, value]]>> |
1117 C<K I<[number[, value]]>>
1118  
1119 Same but key is C<$FIELD[number]> or C<$F[0]>.
1120  
1121 =item C<Number I<[n[, hash_or_array]]>> |
1122 C<N I<[n[, hash_or_array]]>>
1123  
1124 Trim hash_or_array (default C<%N(UMBER)>) values less than C<n> (default 2),
1125 or, if negative, more than C<-n> e.g., C<B<-E>N> or C<B<-E> 'Number -5,
1126 @RESULT'>. This happens recursively at any depth in nested hashes or arrays.
1127  
1128 The first argument can also be a function, where deletion happens for every
1129 element where it returns a falsy value. It gets called for each scalar element
1130 with 3 arguments: 0 - the current (nested) hash or array, 1 - its key or
1131 index, 2 - its value.
1132  
1133 pl '%RESULT = (neg => [-9..-1], pos => [1..9]); Number sub { ! ($_[2] % 3) }, %RESULT'
1134 pl '%R = (neg => [-9..-1], pos => [1..9]); N sub { ! ($_[2] % 3) }, %R'
1135  
1136 > neg: [
1137 > -9,
1138 > -6,
1139 > -3
1140 > ]
1141 > pos: [
1142 > 3,
1143 > 6,
1144 > 9
1145 > ]
1146  
1147 =item C<piped { } I<cmd[, arg...]>> |
1148 C<p { } I<cmd[, arg...]>>
1149  
1150 Open pipe from cmd and loop over it.
1151  
1152 =item C<template I<< [tmpl[, hash|key => value...]] >>> |
1153 C<t I<< [tmpl[, hash|key => value...]] >>>
1154  
1155 Replace values from hash in template (defaults to C<$_>), which may also be a
1156 filehandle of ref to a filename. Hash (defaults to C<%T(EMPLATE)>) may be
1157 given as a reference or key-value pairs. The template may use one of three
1158 markup styles: C<[% I<x> %]>, C<{{ I<x> }}>, or C<< <?pl I<x> ?> >>. These
1159 are totally equivalent. The 1st one found in the template will be used. If
1160 you put a C<~> just inside a delimiter (e.g., C<< <?pl~ I<x> ~?> >>), it will
1161 gobble horizontal whitespace on that side, behind also one newline.
1162  
1163 Within the markup I<x> may be any valid syntax for a Perl hash key. The 3
1164 characters C<|>, C<:>, or C<!> mark the end of the key name and introduce 3
1165 kinds of filter. The 1st two, can be C<?|> or C<?:>, meaning this item
1166 applies only if the key exists. Otherwise the key is optional. If you give a
1167 key, its value is locally assigned to C<$_>. If C<undef> it defaults to
1168 C<''>.
1169  
1170 You write the filter in Perl. You can abort the filter with C<last>. If the
1171 filter starts with C<|> then you pipe its scalar value into the template. If
1172 there is no filter after C<|>, you recursively treat the value as a template.
1173 If the filter starts with C<:> then you insert the value of C<$_>. An easy
1174 way to only do that is C<[%:%]>. The whole template gets compiled to an
1175 anonymous C<sub>, the 1st time. As this happens inside the function, filters
1176 have no access to your surrounding C<my> variables.
1177  
1178 You insert the Perl code following C<!> into the code the template compiles to
1179 verbatim. This is useful for flow control. E.g., if C<$TEMPLATE{x}> (or
1180 C<$T{x}>) is an array, you can loop over its values as C<@$_>. The loop logic
1181 is entirely in Perl, which again localizes C<$_>. If do-nothing C<[%!%]>
1182 starts the document, it "declares" its markup syntax. And with C<~> it
1183 suppresses surrounding space including one following newline. (See
1184 L<PLDUMP|/$ENV{PLDUMP}>)
1185  
1186 pl 'template q(A: {{ a }} B: {{ | "no" }}{{ b ?| "yes" }} C: {{ lc "C" }} C+1: {{ c|$_ + 1}}), qw(a 1 c 3)'
1187 pl 'template q([%x!for( @$_ ) { %] X: [% : %] [%~ ! } %][% y %]), { x => [1..5] }'
1188 pl 'template q(<ul><?pl l!for( @$_ ) { ?> <li><?pl:?></li><?pl ! } ?> </ul>), { l => [1..3] }'
1189 pl 't q(A: {{ a }} B: {{ | "no" }}{{ b ?| "yes" }} C: {{ lc "C" }} C+1: {{ c|$_ + 1}}), qw(a 1 c 3)'
1190 pl 't q([%x!for( @$_ ) { %] X: [% : %] [%~ ! } %][% y %]), { x => [1..5] }'
1191 pl 't q(<ul><?pl l!for( @$_ ) { ?> <li><?pl:?></li><?pl ! } ?> </ul>), { l => [1..3] }'
1192  
1193 > A: 1 B: no C: 3 C+1: 4
1194 > X: 1 X: 2 X: 3 X: 4 X: 5
1195 > 1 2 3
1196  
1197 =item C<Template I<< [tmpl[, hash|key => value...]] >> > |
1198 C<T I<< [tmpl[, hash|key => value...]] >> >
1199  
1200 Same but no newlines, also not on nested templates.
1201  
1202 pl '$_ = q( [ <?pl~inner|~?> ]);
1203 $TEMPLATE{inner} = q([ {{ who | $_ || "Jack" }} in the box ]);
1204 Template;
1205 $TEMPLATE{who} = "Sue";
1206 template'
1207 pl '$_ = q( [ <?pl~inner|~?> ]);
1208 $T{inner} = q([ {{ who | $_ || "Jack" }} in the box ]);
1209 T;
1210 $T{who} = "Sue";
1211 t'
1212  
1213 > [[ Jack in the box ]] [[ Sue in the box ]
1214 > ]
1215  
1216 =back
1217  
1218 =head2 Variables
1219  
1220 Various variables, always also with a one letter alias, often perform magic
1221 tasks at the C<END>.
1222  
1223 =over
1224  
1225 =item C<*ARGV> |
1226 C<*A>
1227  
1228 I<perl:> C<ARGV>, C<$ARGV> & C<@ARGV> are all aliased to C<A>, C<$A> & C<@A>.
1229  
1230 =item C<$ARGIND> |
1231 C<$I>
1232  
1233 Index of ARG which B<-o>, B<-n>, or B<-p> loop is currently processing.
1234  
1235 =item C<@FIELD> |
1236 C<@F>
1237  
1238 I<perl:> This is an alias to loop autosplit variable C<@F>.
1239  
1240 =item C<$quote> |
1241 C<$q>
1242  
1243 Predefined to a single quote C<'> without any magic. Perl's C<q()> makes it
1244 easy to integrate functional quotes under all circumstances. This does the
1245 same for literal quotes.
1246  
1247 =item C<$Quote> |
1248 C<$Q>
1249  
1250 Predefined to a double quote C<"> without any magic. Perl's C<qq()> makes it
1251 easy to integrate functional quotes under all circumstances. This does the
1252 same for literal quotes.
1253  
1254 =item C<%KEYDIFF> |
1255 C<%K>
1256  
1257 At END, sort by keys, print keydiff of C<$ARGIND> array elements. Filled by
1258 C<k(eydiff)>.
1259  
1260 =item C<%NUMBER> |
1261 C<%N>
1262  
1263 At END, sort numerically by values.
1264  
1265 =item C<*RESULT> |
1266 C<*R>
1267  
1268 At END, echo C<$RESULT> if defined, then C<@RESULT> one per line if not empty,
1269 then C<%RESULT> sorted by keys.
1270  
1271 =item C<$ENV{PLDUMP}>
1272  
1273 Since C<pl -MO=Deparse> won't show your parts of the program, it can be quite
1274 baffling when things go wrong. If you export this with value 1 before
1275 starting pl, you see how your parts get embedded in various bits of generated
1276 stuff. If you install C<perltidy>, pl will use it. Some options get handled
1277 by perl, so they won't show up here:
1278  
1279 PLDUMP=1 \
1280 pl 'say "Hello Perld!"'
1281  
1282 > use feature ':' . substr $^V, 1;
1283 >
1284 > sub pl::prog {
1285 > $pl::last = 1;
1286 > LINE: {
1287 > #line 1 "main program"
1288 > say "Hello Perld!";
1289 > } continue {
1290 > $pl::last = 0;
1291 > }
1292 > if ( $pl::last || eof ) {
1293 > ++$ARGIND;
1294 > if ($pl::last) { my $d = $.; close ARGV; $. = $d }
1295 > exit if $pl::last == 2;
1296 > }
1297 > }
1298  
1299 If you export this with value 2, it will instead show what a template would
1300 compile to:
1301  
1302 PLDUMP=2 \
1303 pl 'template q([%x!for( @$_ ) {%] X: [% : %] [%~!}%]), x => [1..5]'
1304 PLDUMP=2 \
1305 pl 't q([%x!for( @$_ ) {%] X: [% : %] [%~!}%]), x => [1..5]'
1306  
1307 > my @_template = [
1308 > '',
1309 > ' X: ',
1310 > ''
1311 > ] ;
1312 > #line 1 "template"
1313 > sub { my $_template = $_template[0]
1314 > ; for((local $_ = $TEMPLATE{x} // '')?():(), @$_ ) {
1315 > ;$_template .= $_template[1] . (do {{
1316 > ; $_ }} // '');$_template .= $_template[2]
1317 > ; }
1318 > ;
1319 > ; $_template }
1320  
1321 =back
1322  
1323 =head1 COMPATIBILITY
1324  
1325 Even if it's rare nowadays, you can still find Perl 5.16 out in the wild
1326 (e.g., in RHEL 7). Pl accommodates it gracefully, falling back to what works.
1327 It has shims for C<any>, C<all>, C<none>, C<notall>, C<product> & C<sum0>.
1328 (Some Unices maintain even older Perl versions, e.g., AIX or Solaris: you can
1329 go back till Perl 5.10 with L<pl
1330 0.63.2|https://sourceforge.net/projects/perl1liner/files/App-pl-v0.63.2.tgz/download>.)
1331  
1332 =head2 Minor Differences with perl -E
1333  
1334 Known minor differences are:
1335  
1336 =over
1337  
1338 =item *
1339  
1340 by design in an B<-n> loop C<last> is per file instead of behaving like C<exit>
1341  
1342 =item *
1343  
1344 don't C<goto LINE>, but C<next LINE> is fine
1345  
1346 =item *
1347  
1348 using C<pop>, etc. to implicitly modify C<@A(RGV)> works in B<-B> begin code
1349 but not in your main program (which gets compiled to a function)
1350  
1351 =item *
1352  
1353 shenanigans with unbalanced braces won't work
1354  
1355 =back
1356  
1357 =head2 Windows Notes
1358  
1359 I<B<W>ork B<I>s B<N>ever B<D>one B<O>n B<W>indows B<S>ystems ;-)>
1360  
1361 Do yourself a favour and get a real Shell, e.g., from
1362 L<Cygwin|https://cygwin.com/>, L<git|https://gitforwindows.org>,
1363 L<MinGW|https://mingw.osdn.io/>,
1364 L<MSYS|https://sourceforge.net/p/mingw-w64/wiki2/MSYS/>, or
1365 L<WSL|https://docs.microsoft.com/en-us/windows/wsl/about>! If you can't avoid
1366 F<command.com> or F<cmd.exe>, you will have to first convert all inner quotes
1367 to C<qq> or C<\">. Then convert the outer single quotes to double quotes:
1368  
1369 pl "echo qq{${quote}Perl$quote}, \"$Quote@ARGV$Quote\"" one-liner
1370 pl "e qq{${q}Perl$q}, \"$Q@A$Q\"" one-liner
1371  
1372 > Perl one-liner
1373  
1374 PowerShell is weirder. (Did I mention you'd be better off with a real Shell?)
1375 You must use outer single quotes, but you still need to protect double quotes:
1376  
1377 pl 'echo qq{${quote}Perl$quote}, \"$Quote@ARGV$Quote\"' one-liner
1378 pl 'e qq{${q}Perl$q}, \"$Q@A$Q\"' one-liner
1379  
1380 > Perl one-liner
1381  
1382 While the old Windows 10 terminal understands ANSI escape sequences, it makes
1383 it horribly hard to activate them. Therefore, they're off by default, requiring
1384 B<--color> to override that choice.
1385  
1386 =for html <hr>
1387  
1388 Pl is on L<SourceForge|https://perl1liner.sourceforge.io/> and also available
1389 on L<meta::cpan|https://metacpan.org/dist/App-pl>.
↑ Summary