bin/slimpack

filter: Statementstmt 67.2% 310 / 461 Branchbran 53.1% 117 / 220 Conditioncond 36.8% 56 / 152 MC/DCmcdc 19.0% 16 / 84 Subroutinesub 86.3% 38 / 44 Podpod n/a totaltotal 55.8% 537 / 961 CC 172 SCAR 39.2
CC
172
Cov
58%
_bundle_unpacklisted
272.0
cmd_trace
93.4
_decide_bundle_set
79.6
CRAP 50.6
↑ Summary lib/App/SlimPacker/Minify.pm »
1 #!/usr/bin/perl
2 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # App::SlimPacker (and PPI) is loaded lazily: only pack/bundle need it. The
10 # fetch-only subcommands (trace/packlists-for/tree) must run without PPI.
11  
12 # slimpack — build a fatpacked, minified standalone Perl script.
13 #
14 # By default ('pack') drives the whole fatpack pipeline internally by calling
15 # App::FatPacker's methods (no shelling out to the fatpack binary):
16 #
17 # 1. trace — compile the boot script, record every module it loads
18 # 2. core filter — drop core modules via Module::CoreList
19 # 3. packlists — map the surviving modules to their .packlist files
20 # 4. tree — copy module sources into a (temporary) fatlib/
21 # 5. bundle — minify + inline plugins + emit the standalone script
22 #
23 # Each step is also exposed as a subcommand with the same arguments as fatpack:
24 # slimpack [pack] [options] script
25 # slimpack trace [--to=FILE|--to-stderr] [--use=MODULE] script
26 # slimpack packlists-for MODULE...
27 # slimpack tree [PACKLIST ...]
28 # slimpack bundle [options] script (minify+bundle only; uses --lib/--fatlib)
29 # slimpack minify [options] script... (minify only, no bundling)
30  
31 37 my %O; # shared bundler options
32  
33 10 ▶sub _load_fatpacker {
Subroutine _load_fatpacker: called CC=2 SCAR=9.2
34 0 ▶ eval { require App::FatPacker; 1 } or die
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require App::FatPacker; 1 } } or die "slimpack: this needs App::FatPacker; install it from CPAN\n" 0 10 10
35 "slimpack: this needs App::FatPacker; install it from CPAN\n";
36 0 return App::FatPacker->new;
37 }
38  
39 25 ▶sub _load_slimpacker {
Subroutine _load_slimpacker: called CC=1 SCAR=0.0
40 25 require App::SlimPacker;
41 25 App::SlimPacker->import(qw(process process_deps minify_file pack_string perl_switches inline_plugins module_deps plugin_search_paths collect_bundle_globals rename_module_name rename_module_path));
42 }
43  
44 37 my (%resolved, %copying, %SLIMIFIED, %SLIMIFIED_DEPS, %RTEXT);
45  
46 # Lazy dependencies that are only needed when another module is in the
47 # bundle: module => gate. Moo::sification loads Moo::HandleMoose, and with it
48 # all of Moose, only once Moose itself has been loaded.
49 37 my %GATED = ('Moo::HandleMoose' => 'Moose');
50 # Gated modules met by a dependency walk, held back until their gate is
51 # bundled (%DEFERRED), and the ones let through since (%RELEASED).
52 37 my (%DEFERRED, %RELEASED);
53  
54 # Module name for a module path: Foo/Bar.pm -> Foo::Bar.
55 161 ▶sub _path_to_mod {
Subroutine _path_to_mod: called CC=1 SCAR=0.0
56 161 (my $mod = shift) =~ s{/}{::}g;
57 161 $mod =~ s{\.pm$}{};
58 161 return $mod;
59 }
60  
61 # Whether $mod matches one of the --include/--exclude patterns: module names
62 # with * and ? as wildcards.
63 55 ▶sub _matches {
Subroutine _matches: called CC=4 SCAR=18.9
64 55 my ($mod, $patterns) = @_;
65 55 ▶ for my $p (@{ $patterns || [] }) {
Condition50%1/2
$patterns || []
l!l
550
MC/DC0%0/1
$patterns || []
$patterns
Truth table50%1/2
A
A= $patterns
Aresult
11
00
66 0 (my $re = quotemeta $p) =~ s/\\\*/.*/g;
67 0 $re =~ s/\\\?/./g;
68 0 ▶ return 1 if $mod =~ /\A$re\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $mod =~ /\A$re\z/ 0 0 0
69 }
70 55 return 0;
71 }
72  
73 # Whether a dependency walk should leave $mod out: excluded modules always,
74 # gated ones until _release_gated finds their gate in the bundle.
75 13 ▶sub _held_back {
Subroutine _held_back: called CC=5 SCAR=21.7
76 13 my ($mod) = @_;
77 13 ▶ return 1 if _matches($mod, $O{exclude});
Branch: 1 branch
BranchTrueFalseTotal
if _matches($mod, $O{"exclude"}) 0 13 13
78 13 ▶ return 0 if !$GATED{$mod} || $RELEASED{$mod} || _matches($mod, $O{include});
Branch: 1 branch
BranchTrueFalseTotal
if not $GATED{$mod} or $RELEASED{$mod} or _matches($mod, $O{"include"}) 13 0 13
Condition33%2/6
not $GATED{$mod} or $RELEASED{$mod} or _matches($mod, $O{"include"})
l!l&&r!l&&!r
1300
not $GATED{$mod} or $RELEASED{$mod}
l!l&&r!l&&!r
1300
MC/DC0%0/3
not $GATED{$mod} or $RELEASED{$mod} or _matches($mod, $O{"include"})
not $GATED{$mod} $RELEASED{$mod} _matches($mod, $O{"include"})
Truth table25%1/4
not(A) or B or C
A= not $GATED{$mod}B= $RELEASED{$mod}C= _matches($mod, $O{"include"})
ABCresult
1XX1
01X1
0011
0000
79 0 $DEFERRED{$mod} = 1;
80 0 return 1;
81 }
82  
83 # Walk the held-back modules whose gate is bundled, until none is left.
84 12 ▶sub _release_gated {
Subroutine _release_gated: called CC=3 SCAR=16.5
85 12 my ($bundled, $walk) = @_;
86 0 ▶ while (my @ready = grep { $bundled->($GATED{$_}) } sort keys %DEFERRED) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = grep({&$bundled($GATED{$_});} sort(keys %DEFERRED))) 0 12 12
87 0 delete @DEFERRED{@ready};
88 0 @RELEASED{@ready} = (1) x @ready;
89 0 $walk->($_) for @ready;
90 }
91 }
92  
93 # Whether the final bundle should drop $mod: it is excluded, or gated on a
94 # module that is not in the bundle (%$in holds the bundled module names).
95 21 ▶sub _dropped {
Subroutine _dropped: called CC=4 SCAR=16.4
96 21 my ($mod, $in) = @_;
97 21 ▶ return 1 if _matches($mod, $O{exclude});
Branch: 1 branch
BranchTrueFalseTotal
if _matches($mod, $O{"exclude"}) 0 21 21
98 21 my $gate = $GATED{$mod};
99 21 ▶ return $gate && !$in->{$gate} && !_matches($mod, $O{include});
Condition33%2/6
$gate && !$in->{$gate} && !_matches($mod, $O{'include'})
!ll&&!rl&&r
2100
$gate and not $in->{$gate}
!ll&&!rl&&r
2100
MC/DC0%0/3
$gate && !$in->{$gate} && !_matches($mod, $O{'include'})
$gate not $in->{$gate} !_matches($mod, $O{'include'})
Truth table25%1/4
A and not(B) && not(C)
A= $gateB= not $in->{$gate}C= !_matches($mod, $O{'include'})
ABCresult
0XX0
10X0
1100
1111
100 }
101  
102 # Module names for the --include patterns. A pattern with wildcards is
103 # matched against the modules installed under @dirs.
104 12 ▶sub _included_modules {
Subroutine _included_modules: called CC=8 SCAR=37.9
105 12 my (@dirs) = @_;
106 12 my %mods;
107 12 ▶ for my $p (@{ $O{include} || [] }) {
Condition50%1/2
$O{'include'} || []
l!l
120
MC/DC0%0/1
$O{'include'} || []
$O{'include'}
Truth table50%1/2
A
A= $O{'include'}
Aresult
11
00
108 0 ▶ if ($p !~ /[*?]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p =~ /[*?]/) 0 0 0
109 0 $mods{$p} = 1;
110 0 next;
111 }
112 # only scan the namespace in front of the first wildcard
113 0 (my $base = $p) =~ s/[*?].*//s;
114 0 $base =~ s/[^:]*\z//;
115 0 $base =~ s/::\z//;
116 0 $base =~ s{::}{/}g;
117 0 ▶ for my $root (grep { !ref && -d } @dirs) {
Condition0%0/3
!ref($_) && -d $_
!ll&&!rl&&r
000
MC/DC0%0/2
!ref($_) && -d $_
!ref($_) -d $_
Truth table0%0/3
not(A) && B
A= !ref($_)B= -d $_
ABresult
0X0
100
111
118 0 ▶ my $top = length $base ? "$root/$base" : $root;
Branch: 1 branch
BranchTrueFalseTotal
length $base ? : 0 0 0
119 0 ▶ next unless -d $top;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $top 0 0 0
120 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
121 0 ▶ return unless /\.pm\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.pm\z/ 0 0 0
122 0 my $mod = _path_to_mod(substr $File::Find::name, length($root) + 1);
123 0 ▶ $mods{$mod} = 1 if _matches($mod, [$p]);
Branch: 1 branch
BranchTrueFalseTotal
if _matches($mod, [$p]) 0 0 0
124 0 } }, $top);
125 }
126 }
127 12 return sort keys %mods;
128 }
129  
130 # ---------------------------------------------------------------- pack ----
131 4 ▶sub cmd_pack {
Subroutine cmd_pack: called CC=3 SCAR=21.7
132 4 my (@args) = @_;
133 4 my $script = shift @args;
134  
135 4 my $fp = _load_fatpacker;
136 0 _load_slimpacker;
137  
138 # 1. trace (slurp from STDOUT)
139 0 my $trace = $fp->trace(args => [$script, @args]);
140 0 my @modules = grep { length } split /\r?\n/, $trace;
141  
142 # 2. drop core and --exclude modules (both match the "Foo::Bar" form)
143 0 require Module::CoreList;
144 @modules = grep {
145 0 my $name = _path_to_mod($_);
146 0 ▶ !Module::CoreList::is_core($name) && !_matches($name, $O{exclude});
Condition0%0/3
Module::CoreList::is_core($name) || _matches($name, $O{'exclude'})
l!l&&r!l&&!r
000
MC/DC0%0/2
Module::CoreList::is_core($name) || _matches($name, $O{'exclude'})
Module::CoreList::is_core($name) _matches($name, $O{'exclude'})
Truth table0%0/3
A || B
A= Module::CoreList::is_core($name)B= _matches($name, $O{'exclude'})
ABresult
1X1
011
000
147 } @modules;
148  
149 # 3+4. packlists -> tree, into a temp dir we throw away. Debian- and
150 # hand-installed modules have no .packlist, so any noncore dep that fatpack
151 # couldn't resolve is copied from its @INC path instead.
152 0 my $tmp = tempdir(CLEANUP => 1);
153 0 my $fatlib = "$tmp/fatlib";
154 0 my @packlists = $fp->packlists_containing(\@modules);
155 0 $fp->packlists_to_tree($fatlib, \@packlists);
156 # The temp fatlib is a private per-run copy assembled for this script, so
157 # its modules get variable-renamed like any other bundled source; only a
158 # user-supplied --fatlib stays verbatim. Set BEFORE _bundle_unpacklisted:
159 # the tree build stashes minified bodies, and they must be renamed too.
160 0 local $O{fatlib_verbatim} = 0;
161 0 _bundle_unpacklisted($fp, $fatlib, $_) for @modules;
162  
163 # 5. bundle from that temp fatlib
164 0 local $O{fatlib} = $fatlib;
165 0 return cmd_bundle($script);
166 }
167  
168 # Copy modules that fatpack couldn't locate a .packlist for into the fatlib,
169 # resolving each from its real @INC path. Recurses into any dependency of those
170 # modules that is loadable, non-core, and likewise missing a reachable packlist.
171  
172 # Minified output and dependencies stashed per fatlib/lib path while the
173 # Normalize a module name or module-path (e.g. `File::HomeDir`, `File/HomeDir.pm`)
174 # to the filename form used in @INC lookups and as a fatlib-relative target path.
175 0 ▶sub _mod_to_path {
Subroutine _mod_to_path: not called CC=1 SCAR=6.9
176 0 my ($mod) = @_;
177 0 $mod =~ s{::}{/}g;
178 0 $mod =~ s{\.pm$}{};
179 0 return "$mod.pm";
180 }
181  
182 0 ▶sub _bundle_unpacklisted {
Subroutine _bundle_unpacklisted: not called CC=16 SCAR=56.1
183 0 my ($fp, $fatlib, $mod) = @_;
184  
185 0 my $path = _mod_to_path($mod);
186 0 ▶ return if $copying{$path}++; # break cycles
Branch: 1 branch
BranchTrueFalseTotal
if $copying{$path}++ 0 0 0
187 0 my $file;
188 0 ▶ if (exists $resolved{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $resolved{$path}) { } 0 0 0
189 0 $file = $resolved{$path};
190 } else {
191 0 local @INC = ('lib', @INC);
192 0 my $ok = eval { require $path; 1 };
193 0 ▶ $file = $ok ? $INC{$path} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
194 0 ▶ $resolved{$path} = $file || '';
Condition0%0/2
$file || ''
l!l
00
MC/DC0%0/1
$file || ''
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
195 }
196 0 my $target = "$fatlib/$path";
197 0 ▶ if ($file && -f $file && !-f $target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and -f $file and not -f $target) 0 0 0
Condition0%0/6
$file and -f $file and not -f $target
!ll&&!rl&&r
000
$file and -f $file
!ll&&!rl&&r
000
MC/DC0%0/3
$file and -f $file and not -f $target
$file -f $file not -f $target
Truth table0%0/4
A and B and not(C)
A= $fileB= -f $fileC= not -f $target
ABCresult
0XX0
10X0
1100
1111
198 0 require File::Basename; require File::Path; require File::Copy;
199 0 File::Path::mkpath(File::Basename::dirname($target));
200 0 ▶ File::Copy::copy($file, $target)
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($file, $target) or warn "slimpack: cannot copy ${file}: $!\n" 0 0 0
201 or warn "slimpack: cannot copy $file: $!\n";
202 0 ▶ my $src = do { local $/; open my $fh, '<', $file or die "Cannot read $file: $!"; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot read ${file}: $!" 0 0 0
203 # Resolve this module's dependencies and stash its minified body now;
204 # the bundle step reuses both, so the source is parsed only once.
205 0 my ($min, @deps);
206 0 ▶ if ($O{no_minify}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{'no_minify'}) { } 0 0 0
207 0 $min = $src;
208 0 @deps = module_deps($src);
209 } else {
210 ($min, @deps) = process_deps($src,
211 rename_vars => $O{rename_vars} && !$O{fatlib_verbatim},
212 rename_subs => 0,
213 0 ▶ rewrite => $O{rewrite});
Condition0%0/3
$O{'rename_vars'} && !$O{'fatlib_verbatim'}
!ll&&!rl&&r
000
MC/DC0%0/2
$O{'rename_vars'} && !$O{'fatlib_verbatim'}
$O{'rename_vars'} !$O{'fatlib_verbatim'}
Truth table0%0/3
A && not(B)
A= $O{'rename_vars'}B= !$O{'fatlib_verbatim'}
ABresult
0X0
100
111
214 }
215 0 $SLIMIFIED{$target} = $min;
216 0 $SLIMIFIED_DEPS{$target} = \@deps;
217 0 for my $dep (@deps) {
218 0 require Module::CoreList;
219 0 ▶ next if Module::CoreList::is_core($dep) || _held_back($dep);
Branch: 1 branch
BranchTrueFalseTotal
if Module::CoreList::is_core($dep) or _held_back($dep) 0 0 0
Condition0%0/3
Module::CoreList::is_core($dep) or _held_back($dep)
l!l&&r!l&&!r
000
MC/DC0%0/2
Module::CoreList::is_core($dep) or _held_back($dep)
Module::CoreList::is_core($dep) _held_back($dep)
Truth table0%0/3
A or B
A= Module::CoreList::is_core($dep)B= _held_back($dep)
ABresult
1X1
011
000
220 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 my ($dpath) = $fp->packlists_containing([$dep]);
222 0 ▶ _bundle_unpacklisted($fp, $fatlib, $dep) unless $dpath;
Branch: 1 branch
BranchTrueFalseTotal
unless $dpath 0 0 0
223 }
224 }
225 0 delete $copying{$path};
226 }
227  
228 # --------------------------------------------------------------- trace ----
229 1 ▶sub cmd_trace {
Subroutine cmd_trace: called CC=10 SCAR=45.4
230 1 my (@args) = @_;
231  
232 1 my $fp = _load_fatpacker;
233  
234 0 my @use;
235 0 my ($file, $to_stderr);
236 0 Getopt::Long::Configure('no_ignorecase');
237 0 ▶ Getopt::Long::GetOptionsFromArray(\@args,
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray(\@args, "to=s", \$file, "to-stderr", \$to_stderr, "use=s", \@use) or exit 2 0 0 0
238 'to=s' => \$file,
239 'to-stderr' => \$to_stderr,
240 'use=s' => \@use,
241 ) or exit 2;
242 0 ▶ die "slimpack trace: --to and --to-stderr are mutually exclusive\n"
Branch: 1 branch
BranchTrueFalseTotal
if $file and $to_stderr 0 0 0
Condition0%0/3
$file and $to_stderr
!ll&&!rl&&r
000
MC/DC0%0/2
$file and $to_stderr
$file $to_stderr
Truth table0%0/3
A and B
A= $fileB= $to_stderr
ABresult
0X0
100
111
243 if $file && $to_stderr;
244  
245 0 my $script = shift @args;
246 0 ▶ die "slimpack trace: missing boot script\n" unless defined $script;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $script 0 0 0
247  
248 0 ▶ $file ||= 'fatpacker.trace';
Condition0%0/2
$file ||= "fatpacker.trace"
l!l
00
MC/DC0%0/1
$file ||= "fatpacker.trace"
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
249 0 ▶ if (!$to_stderr and -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $to_stderr and -e $file) 0 0 0
Condition0%0/3
not $to_stderr and -e $file
!ll&&!rl&&r
000
MC/DC0%0/2
not $to_stderr and -e $file
not $to_stderr -e $file
Truth table0%0/3
not(A) and B
A= not $to_stderrB= -e $file
ABresult
0X0
100
111
250 0 ▶ unlink $file or die "Couldn't remove old trace file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Couldn't remove old trace file: $!" 0 0 0
251 }
252 0 ▶ my $arg = $to_stderr ? '>&STDERR' : ">>${file}";
Branch: 1 branch
BranchTrueFalseTotal
$to_stderr ? : 0 0 0
253 0 $fp->trace(use => \@use, args => [$script, @args], output => $arg);
254 }
255  
256 # --------------------------------------------------------- packlists ----
257 3 ▶sub cmd_packlists_for {
Subroutine cmd_packlists_for: called CC=2 SCAR=11.6
258 3 my (@args) = @_;
259 3 my $fp = _load_fatpacker;
260 # Normalise module names (Carp, File::HomeDir) to the path form
261 # (Carp.pm, File/HomeDir.pm) that FatPacker's require expects; bare
262 # `require "Carp"` would otherwise fail to locate the module.
263 0 my @paths = map { _mod_to_path($_) } @args;
264 0 my @packlists = $fp->packlists_containing(\@paths);
265 0 print "$_\n" for @packlists;
266 }
267  
268 # ---------------------------------------------------------------- tree ----
269 1 ▶sub cmd_tree {
Subroutine cmd_tree: called CC=1 SCAR=0.4
270 1 my (@args) = @_;
271 1 my $fp = _load_fatpacker;
272 0 $fp->packlists_to_tree($O{fatlib}, \@args);
273 }
274  
275 # -------------------------------------------------------------- minify ----
276 12 ▶sub cmd_minify {
Subroutine cmd_minify: called CC=8 SCAR=20.8
277 12 my ($minout, @files) = @_;
278  
279 12 _load_slimpacker;
280 12 ▶ die "slimpack minify: missing source file\n" unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 1 11 12
281 11 ▶ die "slimpack minify: -o needs exactly one source file\n" if $minout && @files > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $minout and @files > 1 1 10 11
Condition100%3/3
$minout and @files > 1
!ll&&!rl&&r
911
MC/DC100%2/2
$minout and @files > 1
$minout @files > 1
Truth table100%3/3
A and B
A= $minoutB= @files > 1
ABresult
0X0
100
111
282  
283 my @out = map {
284 10 my $m = minify_file($_, rename_vars => $O{rename_vars},
285 rename_subs => $O{rename_subs},
286 rename_exports => $O{rename_exports},
287 11 rewrite => $O{rewrite});
288 10 ▶ $m .= "\n" unless $m =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /\n\z/ 10 0 10
289 10 $m;
290 } @files;
291 9 ▶ if ($minout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minout) { } 1 8 9
292 1 ▶ open my $fh, '>', $minout or die "Cannot write $minout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $minout or die "Cannot write ${minout}: $!" 1 0 1
293 1 print {$fh} $out[0];
294 1 close $fh;
295 } else {
296 8 print for @out;
297 }
298 }
299  
300 # ------------------------------------------------------------- bundle ----
301 # Best-level deflate of a module source.
302 30 ▶sub _deflate {
Subroutine _deflate: called CC=1 SCAR=0.0
303 30 my ($src) = @_;
304 30 require Compress::Raw::Zlib;
305 30 my $d = Compress::Raw::Zlib::Deflate->new(-Level => 9);
306 30 my $out = '';
307 30 $d->deflate(\$src, $out);
308 30 $d->flush(my $tail);
309 30 return $out . $tail;
310 }
311  
312 # Deflate, base64-encoded so installation of the source literal is robust on
313 # any platform / encoding.
314 7 ▶sub _zlib_base64 {
Subroutine _zlib_base64: called CC=1 SCAR=0.0
315 7 my ($src) = @_;
316 7 require MIME::Base64;
317 7 return MIME::Base64::encode_base64(_deflate($src), '');
318 }
319  
320 # Deflate, stored as escaped binary (--binary): instead of base64 only the
321 # bytes a text-mode read or the single-quoted literal could mangle are
322 # escaped -- \x01 (the escape byte itself), LF and CR (so no line-ending
323 # translation ever touches the payload), ^Z (EOF in Windows text mode),
324 # backslash and the quote delimiter. ~2% overhead instead of base64's 33%,
325 # but the bundle is no longer a viewable text file. Returns the complete
326 # single-quoted literal.
327 37 my %BIN_ESC = ("\x01" => "\x01\x01", "\x0a" => "\x01n", "\x0d" => "\x01r",
328 "\x1a" => "\x01z", "\\" => "\x01b", "'" => "\x01q");
329 23 ▶sub _zlib_binary {
Subroutine _zlib_binary: called CC=1 SCAR=0.0
330 23 my ($src) = @_;
331 23 my $out = _deflate($src);
332 23 $out =~ s/([\x01\x0a\x0d\x1a\\'])/$BIN_ESC{$1}/g;
333 23 return "'" . $out . "'";
334 }
335  
336 13 ▶sub cmd_bundle {
Subroutine cmd_bundle: called CC=9 SCAR=23.8
337 13 my ($script) = @_;
338  
339 13 _load_slimpacker;
340 13 my $lib = $O{lib};
341 13 my $fatlib = $O{fatlib};
342 13 ▶ my $have_program = @{$O{e}||[]} || @{$O{E}||[]};
Condition42%3/7
@{$O{'e'} or [];} || @{$O{'E'} or [];}
l!l&&r!l&&!r
0013
$O{'e'} || []
l!l
130
$O{'E'} || []
l!l
130
MC/DC0%0/2
@{$O{'e'} or [];} || @{$O{'E'} or [];}
@{$O{'e'} or [];} @{$O{'E'} or [];}
MC/DC0%0/1
$O{'e'} || []
$O{'e'}
MC/DC0%0/1
$O{'E'} || []
$O{'E'}
Truth table33%1/3
A || B
A= @{$O{'e'} or [];}B= @{$O{'E'} or [];}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $O{'e'}
Aresult
11
00
Truth table50%1/2
A
A= $O{'E'}
Aresult
11
00
343 13 ▶ die "slimpack bundle: missing boot script\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $script or $have_program 0 13 13
Condition33%1/3
defined $script or $have_program
l!l&&r!l&&!r
1300
MC/DC0%0/2
defined $script or $have_program
defined $script $have_program
Truth table33%1/3
A or B
A= defined $scriptB= $have_program
ABresult
1X1
011
000
344 if !defined $script && !$have_program;
345 13 ▶ if (defined $script && $have_program) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script and $have_program) 0 13 13
Condition33%1/3
defined $script and $have_program
!ll&&!rl&&r
0130
MC/DC0%0/2
defined $script and $have_program
defined $script $have_program
Truth table33%1/3
A and B
A= defined $scriptB= $have_program
ABresult
0X0
100
111
346 0 warn "slimpack: ignoring script $script (using -e/-E)\n";
347 }
348  
349 13 my $boot_text = _boot_text($script);
350 13 my $strip_re = qr{^(?:\Q$lib\E|\Q$fatlib\E)/}x;
351 13 my @pm_files = _collect_pm_files($lib, $fatlib);
352 13 my $classes = _lib_class_index(\@pm_files, $strip_re, $lib);
353  
354 # Decide which files to bundle, then re-minify the whole bundle from maps
355 # built on the ORIGINAL sources (see _reminify_bundle), then emit.
356 13 my ($main, @paths_to_bundle) =
357 _decide_bundle_set($boot_text, $lib, $fatlib, \@pm_files, $strip_re, $classes);
358  
359 # path -> meta for rename decision and %INC keys
360 12 my %meta;
361 12 for my $path (@paths_to_bundle) {
362 21 my $is_fatlib = ($path =~ m{^\Q$fatlib\E/});
363 21 (my $inc = $path) =~ s/$strip_re//;
364 21 $meta{$path} = { fatlib => $is_fatlib, inc_key => $inc };
365 }
366  
367 12 my ($use_mods, $gmap, $glex, $gsubs) =
368 _reminify_bundle(\$main, \@paths_to_bundle, \%meta);
369  
370 12 _emit_bundle($main, \@paths_to_bundle, \%meta, $use_mods, $gmap, $glex, $gsubs);
371 }
372  
373 # Collect every .pm under --lib and --fatlib into one file list. A root that
374 # does not exist as a directory is skipped (as find(1) itself does).
375 13 ▶sub _collect_pm_files {
Subroutine _collect_pm_files: called CC=1 SCAR=0.0
376 13 my ($lib, $fatlib) = @_;
377 13 my @pm_files;
378 141 ▶ File::Find::find({ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
379 141 ▶ return unless /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.pm$/ 66 75 141
380 75 push @pm_files, $File::Find::name;
381 13 }, no_chdir => 1 }, grep { -d } ($fatlib, $lib));
382 13 return @pm_files;
383 }
384  
385 # Index every package class under --lib (module name => 1) for the
386 # inline_plugins() class list. Only --lib sources count: the fatlib holds
387 # third-party modules, not the application's plugin classes.
388 13 ▶sub _lib_class_index {
Subroutine _lib_class_index: called CC=3 SCAR=11.0
389 13 my ($pm_files, $strip_re, $lib) = @_;
390 13 my %classes;
391 13 for my $path (@$pm_files) {
392 75 ▶ next unless $path =~ m{^\Q$lib\E/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^\Q$lib\E/] 10 65 75
393 65 (my $rel = $path) =~ s/$strip_re//;
394 65 $classes{ _path_to_mod($rel) } = 1;
395 }
396 13 return \%classes;
397 }
398  
399 # Decide which bundled .pm files to include and process the boot program,
400 # stashing each module's ORIGINAL source in %RTEXT and its minified body in
401 # %SLIMIFIED so the re-minify and emit passes never re-read or re-parse them.
402 #
403 # Two modes:
404 # --bundle-lib-all every --lib .pm goes in; the boot program is minified
405 # on its own (no dependency walk).
406 # default walk the boot program's dependency graph against the
407 # lib/fatlib index, enqueueing each dependency exactly
408 # once and resolving packlist-less modules from @INC.
409 # Returns ($main, @paths_to_bundle).
410 13 ▶sub _decide_bundle_set {
Subroutine _decide_bundle_set: called CC=23 SCAR=43.8
411 13 my ($boot_text, $lib, $fatlib, $pm_files, $strip_re, $classes) = @_;
412 13 my $main;
413 my @paths_to_bundle;
414 13 ▶ if ($O{bundle_lib_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{'bundle_lib_all'}) { } 0 13 13
415 0 $RTEXT{'--boot--'} = $boot_text;
416 0 @paths_to_bundle = sort @$pm_files;
417 0 ▶ $main = $O{no_minify} ? $boot_text : process($boot_text, rewrite => $O{rewrite});
Branch: 1 branch
BranchTrueFalseTotal
$O{'no_minify'} ? : 0 0 0
418 } else {
419 13 my (%lib_path, %fatlib_mod);
420 13 for my $path (@$pm_files) {
421 75 (my $rel = $path) =~ s/$strip_re//;
422 75 my $mod = _path_to_mod($rel);
423 75 ▶ if ($path =~ m{^\Q$fatlib\E/}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[^\Q$fatlib\E/]) { } 10 65 75
elsif ($path =~ m[^\Q$lib\E/]) { } 65 0 65
424 10 $fatlib_mod{$mod} = 1;
425 } elsif ($path =~ m{^\Q$lib\E/}) {
426 65 $lib_path{$mod} = $path;
427 }
428 }
429 13 my %to_bundle;
430 13 for my $path (@$pm_files) {
431 75 ▶ next unless $path =~ m{^\Q$fatlib\E/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^\Q$fatlib\E/] 65 10 75
432 10 $to_bundle{$path} = 1;
433 }
434 13 my %seen;
435 my @unresolved;
436 # When inline_plugins is on, the plugin-finder module (e.g.
437 # Module::Pluggable) is replaced in the boot text by static `require`s of
438 # the actual plugin classes, so we must NOT bundle the finder (nor chase
439 # it through the $INC fallback) — that would bloat the bundle and defeat
440 # the inlining.
441 13 my %inlined_finder;
442 13 ▶ if ($O{inline_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{"inline_plugins"}) 13 0 13
443 13 my $searched = plugin_search_paths($boot_text);
444 13 ▶ if (%$searched) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$searched) 0 13 13
445 0 %inlined_finder = map { $_ => 1 } qw(Module::Pluggable);
446 }
447 }
448 # Inline the plugin-finder's result into the boot text now so the
449 # static class list and its requires flow through the same rename
450 # passes as every other bundle module name (module renaming otherwise
451 # leaves search_path stranded and drops the plugins() list entirely).
452 13 ▶ $boot_text = inline_plugins($boot_text, $classes) if $O{inline_plugins};
Branch: 1 branch
BranchTrueFalseTotal
if $O{"inline_plugins"} 13 0 13
453 13 my ($stash_and_enqueue, $enqueue);
454 # Minify a source once and enqueue its dependencies in that same parse.
455 # The minified body is stashed per path so the emit pass below never
456 # re-parses it.
457 12 ▶ $stash_and_enqueue = sub {
Subroutine __ANON__: called CC=6 SCAR=21.4
458 12 my ($path, $src) = @_;
459 12 $RTEXT{$path} = $src;
460 12 ▶ if ($O{no_minify}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{'no_minify'}) { } 0 12 12
461 0 $SLIMIFIED{$path} = $src;
462 0 $enqueue->($_) for module_deps($src);
463 } else {
464 12 my $is_fat = $path =~ m{^\Q$fatlib\E/};
465 my ($min, @deps) = process_deps($src,
466 rename_vars => $O{rename_vars} && (!$is_fat || !$O{fatlib_verbatim}),
467 rename_subs => 0,
468 12 ▶ rewrite => $O{rewrite});
Condition33%2/6
$O{'rename_vars'} && !($is_fat && $O{'fatlib_verbatim'})
!ll&&!rl&&r
0012
$is_fat && $O{'fatlib_verbatim'}
!ll&&!rl&&r
1200
MC/DC0%0/3
$O{'rename_vars'} && !($is_fat && $O{'fatlib_verbatim'})
$O{'rename_vars'} $is_fat $O{'fatlib_verbatim'}
Truth table25%1/4
A && not(B && C)
A= $O{'rename_vars'}B= $is_fatC= $O{'fatlib_verbatim'}
ABCresult
0XX0
1110
10X1
1101
469 12 $SLIMIFIED{$path} = $min;
470 12 $enqueue->($_) for @deps;
471 }
472 13 };
473 13 ▶ $enqueue = sub {
Subroutine __ANON__: called CC=16 SCAR=40.9
474 13 my ($mod) = @_;
475 13 ▶ return if _held_back($mod);
Branch: 1 branch
BranchTrueFalseTotal
if _held_back($mod) 0 13 13
476 13 ▶ return if $seen{$mod}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$mod}++ 0 13 13
477 13 ▶ return if $inlined_finder{$mod};
Branch: 1 branch
BranchTrueFalseTotal
if $inlined_finder{$mod} 0 13 13
478 13 require Module::CoreList;
479 13 ▶ return if Module::CoreList::is_core($mod);
Branch: 1 branch
BranchTrueFalseTotal
if Module::CoreList::is_core($mod) 0 13 13
480 13 my $path = $lib_path{$mod};
481 13 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 1 12 13
482 1 ▶ if ($fatlib_mod{$mod}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatlib_mod{$mod}) 0 1 1
483 0 return;
484 }
485 # Module has no .packlist (Debian/hand-installed): copy it from
486 # @INC into the fatlib so it still gets bundled.
487 1 my $fp = _load_fatpacker;
488 0 _bundle_unpacklisted($fp, $fatlib, $mod);
489 0 my $copied = "$fatlib/" . _mod_to_path($mod);
490 0 ▶ if (-f $copied) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $copied) { } 0 0 0
491 0 $fatlib_mod{$mod} = 1;
492 0 $to_bundle{$copied} = 1;
493 0 ▶ if (exists $SLIMIFIED_DEPS{$copied}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SLIMIFIED_DEPS{$copied}) { } 0 0 0
494 0 $enqueue->($_) for @{ $SLIMIFIED_DEPS{$copied} };
495 } else {
496 0 ▶ my $src = do { local $/; open my $fh, '<', $copied or die "Cannot read $copied: $!"; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $copied or die "Cannot read ${copied}: $!" 0 0 0
497 0 $stash_and_enqueue->($copied, $src);
498 }
499 } else {
500 0 push @unresolved, $mod;
501 }
502 0 return;
503 }
504 12 ▶ return if $to_bundle{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $to_bundle{$path} 0 12 12
505 12 $to_bundle{$path} = 1;
506 # A pack run already minified this module while copying it into the
507 # fatlib; reuse the stashed body and deps instead of re-parsing.
508 12 ▶ if (exists $SLIMIFIED{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SLIMIFIED{$path}) 0 12 12
509 0 ▶ $enqueue->($_) for @{ $SLIMIFIED_DEPS{$path} || [] };
Condition0%0/2
$SLIMIFIED_DEPS{$path} || []
l!l
00
MC/DC0%0/1
$SLIMIFIED_DEPS{$path} || []
$SLIMIFIED_DEPS{$path}
Truth table0%0/2
A
A= $SLIMIFIED_DEPS{$path}
Aresult
11
00
510 0 return;
511 }
512 12 ▶ my $src = do { local $/; open my $fh, '<', $path or die "Cannot read $path: $!"; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!" 12 0 12
513 12 $stash_and_enqueue->($path, $src);
514 13 };
515 13 ▶ if ($O{no_minify}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{'no_minify'}) { } 0 13 13
516 0 $main = $boot_text;
517 0 $enqueue->($_) for module_deps($boot_text);
518 } else {
519 13 ($main, my @boot_deps) = process_deps($boot_text, rewrite => $O{rewrite});
520 13 $enqueue->($_) for @boot_deps;
521 }
522 12 $RTEXT{'--boot--'} = $boot_text;
523 12 ▶ if ($O{inline_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{"inline_plugins"}) 12 0 12
524 12 my $searched = plugin_search_paths($boot_text);
525 12 ▶ if (%$searched) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$searched) 0 12 12
526 0 for my $ns (sort keys %$searched) {
527 0 for my $cp (sort grep { m{^\Q$ns\E::[^:]+$} } keys %$classes) {
528 0 $enqueue->($cp);
529 }
530 }
531 }
532 }
533 12 $enqueue->($_) for _included_modules($lib, $fatlib, @INC);
534 0 ▶ _release_gated(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
535 0 my ($mod) = @_;
536 return $fatlib_mod{$mod} || -f "$fatlib/" . _mod_to_path($mod)
537 0 ▶ || ($lib_path{$mod} && $to_bundle{$lib_path{$mod}});
Condition0%0/9
$fatlib_mod{$mod} || -f "$fatlib/" . _mod_to_path($mod) || $lib_path{$mod} && $to_bundle{$lib_path{$mod}}
l!l&&r!l&&!r
000
$fatlib_mod{$mod} or -f "$fatlib/" . _mod_to_path($mod)
l!l&&r!l&&!r
000
$lib_path{$mod} and $to_bundle{$lib_path{$mod}}
!ll&&!rl&&r
000
MC/DC0%0/4
$fatlib_mod{$mod} || -f "$fatlib/" . _mod_to_path($mod) || $lib_path{$mod} && $to_bundle{$lib_path{$mod}}
$fatlib_mod{$mod} -f "$fatlib/" . _mod_to_path($mod) $lib_path{$mod} $to_bundle{$lib_path{$mod}}
Truth table0%0/5
A or B || C and D
A= $fatlib_mod{$mod}B= -f "$fatlib/" . _mod_to_path($mod)C= $lib_path{$mod}D= $to_bundle{$lib_path{$mod}}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
538 12 }, $enqueue);
539 12 ▶ if (@unresolved) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unresolved) 0 12 12
540 0 my @genuine;
541 0 require Module::CoreList;
542 0 for my $mod (@unresolved) {
543 0 ▶ push @genuine, $mod unless Module::CoreList::is_core(_path_to_mod($mod));
Branch: 1 branch
BranchTrueFalseTotal
unless Module::CoreList::is_core(_path_to_mod($mod)) 0 0 0
544 }
545 0 ▶ if (@genuine) {
Branch: 1 branch
BranchTrueFalseTotal
if (@genuine) 0 0 0
546 0 warn "slimpack: unresolved dependencies: @genuine\n";
547 }
548 }
549 12 @paths_to_bundle = sort keys %to_bundle;
550 }
551 12 my %mod_of = map { (my $rel = $_) =~ s/$strip_re//; ($_ => _path_to_mod($rel)) } @paths_to_bundle;
552 12 my %in = map { $_ => 1 } grep { !_matches($_, $O{exclude}) } values %mod_of;
553 12 @paths_to_bundle = grep { !_dropped($mod_of{$_}, \%in) } @paths_to_bundle;
554 12 return ($main, @paths_to_bundle);
555 }
556  
557 # Bundle-aware re-minify. --rewrite renames `our` package globals too;
558 # --rename-subs renames sub/method names (and, with --rename-exports, the
559 # export/import protocol strings — on by default). Both need the whole
560 # dependency graph packed together so every module (and the boot program)
561 # agrees on one short name — declaration and all references — which
562 # per-file minification alone never dare to touch. --rename-modules (on
563 # by default) likewise renames the namespace components of every bundled
564 # package consistently, INCLUDING each package's leaf (file) name; the
565 # loader's %INC keys are rewritten with the same component map in the emit
566 # pass. Build the maps from the ORIGINAL sources (the walk may already
567 # have minified them), then re-minify every bundled module and the boot
568 # program so the renames are applied consistently across the whole bundle.
569 # Updates $main_ref in place. Returns ($use_mods, $gmap, $glex, $gsubs).
570 12 ▶sub _reminify_bundle {
Subroutine _reminify_bundle: called CC=25 SCAR=34.3
571 12 my ($main_ref, $paths, $meta) = @_;
572 12 my ($gmap, $glex, $gsubs, $gmods);
573 12 my $use_mods;
574 12 ▶ if (($O{rewrite} || $O{rename_subs} || $O{rename_modules}) && !$O{no_minify}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{"rewrite"} || $O{"rename_subs"} || $O{"rename_modules"} and not $O{"no_minify"}) 12 0 12
Condition33%3/9
$O{"rewrite"} || $O{"rename_subs"} || $O{"rename_modules"} and not $O{"no_minify"}
!ll&&!rl&&r
0012
$O{"rewrite"} or $O{"rename_subs"} or $O{"rename_modules"}
l!l&&r!l&&!r
1200
$O{"rewrite"} or $O{"rename_subs"}
l!l&&r!l&&!r
0120
MC/DC0%0/4
$O{"rewrite"} || $O{"rename_subs"} || $O{"rename_modules"} and not $O{"no_minify"}
$O{"rewrite"} $O{"rename_subs"} $O{"rename_modules"} not $O{"no_minify"}
Truth table14%1/7
A or B or C and not(D)
A= $O{"rewrite"}B= $O{"rename_subs"}C= $O{"rename_modules"}D= not $O{"no_minify"}
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
575 my @srcs = map {
576 12 ▶ defined $RTEXT{$_} ? $RTEXT{$_} : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $RTEXT{$_} ? : 12 9 21
577 9 ▶ open my $fh, '<', $_ or die "Cannot read $_: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $_ or die "Cannot read ${_}: $!" 9 0 9
578 9 my $s = do { local $/; <$fh> }; close $fh; $s;
579 }
580 } @$paths;
581 12 ▶ push @srcs, $RTEXT{'--boot--'} if defined $RTEXT{'--boot--'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $RTEXT{"--boot--"} 12 0 12
582 ($gmap, $glex, $gsubs, $gmods) = collect_bundle_globals(@srcs,
583 12 { rename_exports => $O{rename_exports} });
584 12 ▶ $use_mods = $O{rename_modules} && $gmods && %$gmods ? $gmods : undef;
Branch: 1 branch
BranchTrueFalseTotal
$O{'rename_modules'} && $gmods && %$gmods ? : 1 11 12
Condition83%5/6
$O{"rename_modules"} and $gmods and %$gmods
!ll&&!rl&&r
1011
$O{"rename_modules"} and $gmods
!ll&&!rl&&r
1002
MC/DC66%2/3
$O{"rename_modules"} and $gmods and %$gmods
$O{"rename_modules"} $gmods %$gmods
Truth table75%3/4
A and B and C
A= $O{"rename_modules"}B= $gmodsC= %$gmods
ABCresult
0XX0
10X0
1100
1111
585 # Module renames move package namespaces, so the global map keys follow.
586 12 my $gmap_mod = $gmap;
587 12 ▶ if ($use_mods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mods) 1 11 12
588 1 my %m;
589 1 for my $pkg (keys %$gmap) {
590 0 $m{ rename_module_name($pkg, $use_mods) } = $gmap->{$pkg};
591 }
592 1 $gmap_mod = \%m;
593 }
594 12 ▶ if (%$gmap || %$gsubs || $use_mods) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$gmap or %$gsubs or $use_mods) 10 2 12
Condition66%4/6
%$gmap or %$gsubs or $use_mods
l!l&&r!l&&!r
1002
%$gmap or %$gsubs
l!l&&r!l&&!r
0102
MC/DC33%1/3
%$gmap or %$gsubs or $use_mods
%$gmap %$gsubs $use_mods
Truth table50%2/4
A or B or C
A= %$gmapB= %$gsubsC= $use_mods
ABCresult
1XX1
01X1
0011
0000
595 10 for my $path (@$paths) {
596 21 my $src = $RTEXT{$path};
597 21 ▶ if (!defined $src) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $src) 9 12 21
598 9 ▶ open my $fh, '<', $path or die "Cannot read $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!" 9 0 9
599 9 $src = do { local $/; <$fh> }; close $fh;
600 }
601 $SLIMIFIED{$path} = process($src,
602 rename_vars => $O{rename_vars} && (!$meta->{$path}{fatlib} || !$O{fatlib_verbatim}),
603 rename_subs => $O{rename_subs},
604 rename_exports => $O{rename_exports},
605 rewrite => $O{rewrite},
606 21 ▶ (%$gmap_mod ? (global_rename_vars => $gmap_mod) : ()),
Branch: 2 branches
BranchTrueFalseTotal
%$gmap_mod ? : 0 21 21
$use_mods ? : 1 20 21
Condition66%4/6
$O{'rename_vars'} && !($meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'})
!ll&&!rl&&r
0912
$meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'}
!ll&&!rl&&r
1209
MC/DC33%1/3
$O{'rename_vars'} && !($meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'})
$O{'rename_vars'} $meta->{$path}{'fatlib'} $O{'fatlib_verbatim'}
Truth table50%2/4
A && not(B && C)
A= $O{'rename_vars'}B= $meta->{$path}{'fatlib'}C= $O{'fatlib_verbatim'}
ABCresult
0XX0
1110
10X1
1101
607 global_lexicals => $glex,
608 global_subs => $gsubs,
609 ($use_mods ? (global_modules => $use_mods) : ()));
610 }
611 $$main_ref = process($RTEXT{'--boot--'},
612 rename_vars => $O{rename_vars}, rename_subs => $O{rename_subs},
613 rename_exports => $O{rename_exports},
614 rewrite => $O{rewrite},
615 10 ▶ (%$gmap_mod ? (global_rename_vars => $gmap_mod) : ()),
Branch: 2 branches
BranchTrueFalseTotal
%$gmap_mod ? : 0 10 10
$use_mods ? : 1 9 10
616 global_lexicals => $glex,
617 global_subs => $gsubs,
618 ($use_mods ? (global_modules => $use_mods) : ()));
619 }
620 }
621 12 return ($use_mods, $gmap, $glex, $gsubs);
622 }
623  
624 # Assemble and write the standalone bundle from the final minified bodies:
625 # a lazy @INC hook (mirrors App::FatPacker) that evaluates each bundled
626 # source exactly when `require`d, so intra-module dependency ordering is
627 # preserved instead of emitting everything top-to-bottom eagerly. Module
628 # sources are embedded with pack_string() (single-quoted / q<delim>
629 # literals) instead of B::perlstring's always-double-quoted form, which
630 # escapes every `$`/`@`/`%` and inflates the table by ~15%. By default each
631 # source is also deflate-compressed (best level) and base64-encoded before
632 # embedding; --no-compress keeps the raw source literal. The loader is
633 # written literally below in a single-quoted heredoc so its own sigils never
634 # need escaping; the only dynamic piece is the module table, spliced in at
635 # the __SLPACK_ENTRIES__ placeholder.
636 12 ▶sub _emit_bundle {
Subroutine _emit_bundle: called CC=22 SCAR=34.2
637 12 my ($main, $paths, $meta, $use_mods, $gmap, $glex, $gsubs) = @_;
638  
639 12 my @modules;
640 12 for my $path (@$paths) {
641 # Reuse the minified body produced while walking the graph; only
642 # modules that were never reached that way fall back to reading and
643 # processing here.
644 21 my $out = $SLIMIFIED{$path};
645 21 ▶ if (!defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $out) 0 21 21
646 0 ▶ open my $fh, '<', $path or die "Cannot read $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Cannot read ${path}: $!" 0 0 0
647 0 my $src = do { local $/; <$fh> };
648 0 close $fh;
649 $out = $O{no_minify} ? $src : process($src,
650 rename_vars => $O{rename_vars} && (!$meta->{$path}{fatlib} || !$O{fatlib_verbatim}),
651 rename_subs => $O{rename_subs},
652 rewrite => $O{rewrite},
653 0 ▶ (defined $gmap ? (global_rename_vars => $gmap, global_lexicals => $glex) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$O{'no_minify'} ? : 0 0 0
defined $gmap ? : 0 0 0
defined $gsubs ? : 0 0 0
Condition0%0/6
$O{'rename_vars'} && !($meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'})
!ll&&!rl&&r
000
$meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'}
!ll&&!rl&&r
000
MC/DC0%0/3
$O{'rename_vars'} && !($meta->{$path}{'fatlib'} && $O{'fatlib_verbatim'})
$O{'rename_vars'} $meta->{$path}{'fatlib'} $O{'fatlib_verbatim'}
Truth table0%0/4
A && not(B && C)
A= $O{'rename_vars'}B= $meta->{$path}{'fatlib'}C= $O{'fatlib_verbatim'}
ABCresult
0XX0
1110
10X1
1101
654 (defined $gsubs ? (global_subs => $gsubs) : ()));
655 }
656 21 $out =~ s/^#!\S+//;
657 21 push @modules, $out;
658 }
659  
660 # The bundle keeps the boot script's own shebang line, captured in
661 # _boot_text before minification eats it (--shebang overrides it); a boot
662 # program without one gets the portable env form, since /usr/bin/perl
663 # does not exist everywhere (FreeBSD, custom perls).
664 12 ▶ my $shebang = $O{shebang} // '/usr/bin/env perl';
Condition100%2/2
$O{"shebang"} // "/usr/bin/env perl"
l!l
93
MC/DC100%1/1
$O{"shebang"} // "/usr/bin/env perl"
$O{"shebang"}
Truth table100%2/2
A
A= $O{"shebang"}
Aresult
11
00
665 12 ▶ $shebang = "#!$shebang" if $shebang !~ /^#!/;
Branch: 1 branch
BranchTrueFalseTotal
unless $shebang =~ /^#!/ 4 8 12
666 12 $main =~ s/\A#![^\n]*\n?//;
667 12 $main =~ s{use lib[^;]+;}{}g;
668  
669 # The boot program is compressed the same way as modules. A
670 # __DATA__/__END__ section can't survive string-eval, so it is split off
671 # and bundled as its own '__DATA__' entry; the loader opens an in-memory
672 # handle over it and aliases *main::DATA before the boot program runs
673 # (semantics are the same except fileno(DATA), which has no OS
674 # descriptor, and tell(DATA), which counts from the data, not the file).
675 12 my $main_compressed = $O{compress};
676 12 my $data_section;
677 12 ▶ if ($main_compressed && $main =~ /^__(?:DATA|END)__[^\n]*\n?/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_compressed and $main =~ /^__(?:DATA|END)__[^\n]*\n?/m) 4 8 12
Condition100%3/3
$main_compressed and $main =~ /^__(?:DATA|END)__[^\n]*\n?/m
!ll&&!rl&&r
264
MC/DC100%2/2
$main_compressed and $main =~ /^__(?:DATA|END)__[^\n]*\n?/m
$main_compressed $main =~ /^__(?:DATA|END)__[^\n]*\n?/m
Truth table100%3/3
A and B
A= $main_compressedB= $main =~ /^__(?:DATA|END)__[^\n]*\n?/m
ABresult
0X0
100
111
678 4 $data_section = substr($main, $+[0]);
679 4 $main = substr($main, 0, $-[0]);
680 }
681  
682 12 my @inc_entries = map { $meta->{$_}{inc_key} } @$paths;
683 12 ▶ if ($use_mods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mods) 1 11 12
684 1 @inc_entries = map { rename_module_path($_, $use_mods) } @inc_entries;
685 }
686 12 my %code_by_file;
687 12 @code_by_file{@inc_entries} = @modules;
688  
689 37 ▶ use B qw(perlstring);
Subroutine BEGIN: called CC=1 SCAR=0.0
690 my $literal = $O{binary} ? \&_zlib_binary
691 7 ▶ : $O{compress} ? sub { pack_string(_zlib_base64($_[0])) }
Subroutine __ANON__: called CC=1 SCAR=0.0
692 12 ▶ : \&pack_string;
Branch: 2 branches
BranchTrueFalseTotal
$O{'binary'} ? : 8 4 12
$O{'compress'} ? : 2 2 4
693 12 my @entries = map { perlstring($_) . "=>" . $literal->($code_by_file{$_}) }
694 @inc_entries;
695 12 ▶ push @entries, perlstring('__MAIN__') . '=>' . $literal->($main)
Branch: 1 branch
BranchTrueFalseTotal
if $main_compressed 10 2 12
696 if $main_compressed;
697 12 ▶ push @entries, perlstring('__DATA__') . '=>' . $literal->($data_section)
Branch: 1 branch
BranchTrueFalseTotal
if defined $data_section 4 8 12
698 if defined $data_section;
699 12 my $entries = join ',', @entries;
700  
701 12 ▶ my $loader = $O{compress} ? _loader_text_compressed() : _loader_text_plain();
Branch: 1 branch
BranchTrueFalseTotal
$O{'compress'} ? : 10 2 12
702 12 my ($pre, $post) = split /__SLPACK_ENTRIES__/, $loader, 2;
703 12 my $out = $shebang . "\n" . $pre . $entries . $post;
704 12 ▶ $out .= $main unless $main_compressed;
Branch: 1 branch
BranchTrueFalseTotal
unless $main_compressed 2 10 12
705  
706 12 ▶ if ($O{output} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($O{'output'} eq '-') { } 0 12 12
707 0 print $out;
708 } else {
709 12 ▶ open my $fh, '>', $O{output} or die "Cannot write $O{output}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $O{"output"} or die qq[Cannot write $O{"output"}: $!] 12 0 12
710 12 print $fh $out;
711 12 close $fh;
712 12 chmod 0755, $O{output};
713 }
714 }
715  
716 # Loader for compressed bundles. Only the head differs between base64
717 # (default) and --binary entries: the decoder closure $x, and whether
718 # MIME::Base64 is needed. The shared tail installs the lazy @INC hook,
719 # aliases *main::DATA to the '__DATA__' entry if the boot program had a data
720 # section (leaving an already-open main::DATA alone, with a warning), and
721 # decodes the '__MAIN__' entry into a lexical that the last statement evals
722 # AFTER the BEGIN block, at run phase — inside BEGIN, `perl -c bundle`
723 # would execute the program.
724 10 ▶sub _loader_text_compressed {
Subroutine _loader_text_compressed: called CC=2 SCAR=6.9
725 10 ▶ my $head = $O{binary} ? <<'HEAD_BINARY' : <<'HEAD_BASE64';
Branch: 1 branch
BranchTrueFalseTotal
$O{'binary'} ? : 8 2 10
726 use Compress::Raw::Zlib ();
727 my$m;BEGIN{my%c=(__SLPACK_ENTRIES__);my%u=("\x01"=>"\x01",n=>"\n",r=>"\r",z=>"\x1a",b=>"\\",q=>"'");my$x=sub{my($b)=@_;$b=~s/\x01(.)/$u{$1}/g;my$i=Compress::Raw::Zlib::Inflate->new;my$g="";$i->inflate($b,\$g);$g};
728 HEAD_BINARY
729 use Compress::Raw::Zlib ();
730 use MIME::Base64 qw(decode_base64);
731 my$m;BEGIN{my%c=(__SLPACK_ENTRIES__);my$x=sub{my$i=Compress::Raw::Zlib::Inflate->new;my$g="";$i->inflate(decode_base64($_[0]),\$g);$g};
732 HEAD_BASE64
733 10 chomp $head;
734 10 return $head . <<'TAIL';
735 my$d='SlimPacked::'.(0+%c);no strict 'refs';*{($d).'::INC'}=sub{my($e,$f)=@_;return unless my$b=$c{$f};my$g=$x->($b);open my$h,'<',\$g or die"slimpack load $f: $!";return$h};unshift@INC,bless\%c,$d;if(defined(my$D=$c{'__DATA__'})){if(defined fileno*main::DATA){warn"slimpack: main::DATA already open; leaving it\n"}else{my$v=$x->($D);open my$h,'<',\$v or die"slimpack DATA: $!";*main::DATA=$h}}if(defined(my$j=delete$c{'__MAIN__'})){$m=$x->($j)}};if(defined$m){eval$m;die$@ if$@}
736 TAIL
737 }
738  
739 2 ▶sub _loader_text_plain {
Subroutine _loader_text_plain: called CC=1 SCAR=0.0
740 2 return <<'LOADER';
741 BEGIN{my%c=(__SLPACK_ENTRIES__);my$d='SlimPacked::'.(0+%c);no strict 'refs';*{($d).'::INC'}=sub{my($e,$f)=@_;return unless my$lit=$c{$f};my$g=eval$lit;die"slimpack decode $f: $@"if$@;open my$h,'<',\$g or die"slimpack load $f: $!";return$h};unshift@INC,bless\%c,$d;};
742 LOADER
743 }
744 13 ▶sub _boot_text {
Subroutine _boot_text: called CC=14 SCAR=35.6
745 13 my ($script) = @_;
746 13 ▶ if (@{$O{e}||[]} || @{$O{E}||[]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$O{"e"} or [];} or @{$O{"E"} or [];}) 0 13 13
Condition42%3/7
@{$O{"e"} or [];} or @{$O{"E"} or [];}
l!l&&r!l&&!r
0013
$O{'e'} || []
l!l
130
$O{'E'} || []
l!l
130
MC/DC0%0/2
@{$O{"e"} or [];} or @{$O{"E"} or [];}
@{$O{"e"} or [];} @{$O{"E"} or [];}
MC/DC0%0/1
$O{'e'} || []
$O{'e'}
MC/DC0%0/1
$O{'E'} || []
$O{'E'}
Truth table33%1/3
A or B
A= @{$O{"e"} or [];}B= @{$O{"E"} or [];}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $O{'e'}
Aresult
11
00
Truth table50%1/2
A
A= $O{'E'}
Aresult
11
00
747 0 ▶ return perl_switches($O{m}||[], $O{M}||[], $O{e}||[], $O{E}||[]);
Condition0%0/8
$O{'m'} || []
l!l
00
$O{'M'} || []
l!l
00
$O{'e'} || []
l!l
00
$O{'E'} || []
l!l
00
MC/DC0%0/1
$O{'m'} || []
$O{'m'}
MC/DC0%0/1
$O{'M'} || []
$O{'M'}
MC/DC0%0/1
$O{'e'} || []
$O{'e'}
MC/DC0%0/1
$O{'E'} || []
$O{'E'}
Truth table0%0/2
A
A= $O{'m'}
Aresult
11
00
Truth table0%0/2
A
A= $O{'M'}
Aresult
11
00
Truth table0%0/2
A
A= $O{'e'}
Aresult
11
00
Truth table0%0/2
A
A= $O{'E'}
Aresult
11
00
748 }
749 13 ▶ open my $fh, '<', $script or die "Cannot read boot script $script: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $script or die "Cannot read boot script ${script}: $!" 13 0 13
750 13 my $content = do { local $/; <$fh> };
751 13 close $fh;
752 # Remember the script's shebang for the bundle; the minifier strips it.
753 13 ▶ $O{shebang} //= $1 if $content =~ /\A(#![^\n]*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /\A(#![^\n]*)/ 8 5 13
Condition33%1/3
$O{"shebang"} //= $1
l!l&&r!l&&!r
080
MC/DC0%0/2
$O{"shebang"} //= $1
$O{"shebang"} $1
Truth table33%1/3
A //= B
A= $O{"shebang"}B= $1
ABresult
1X1
011
000
754 13 ▶ return perl_switches($O{m}||[], $O{M}||[], []) . $content;
Condition50%2/4
$O{'m'} || []
l!l
130
$O{'M'} || []
l!l
130
MC/DC0%0/1
$O{'m'} || []
$O{'m'}
MC/DC0%0/1
$O{'M'} || []
$O{'M'}
Truth table50%1/2
A
A= $O{'m'}
Aresult
11
00
Truth table50%1/2
A
A= $O{'M'}
Aresult
11
00
755 }
756  
757 2 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
758 2 return <<'USAGE';
759 slimpack — build a fatpacked, minified standalone Perl script.
760  
761 By default ('pack') runs the whole pipeline by calling App::FatPacker:
762 trace the script, drop core modules, resolve packlists, copy to a temp
763 fatlib, then minify + inline plugins and emit the standalone script.
764  
765 Usage: slimpack [OPTIONS] [COMMAND]
766  
767 Commands:
768 (default) [OPTIONS] script full pipeline: trace, drop core, packlists,
769 tree into a temp fatlib, bundle
770 trace [--to=FILE|--to-stderr] [--use=MODULE] script
771 packlists-for MODULE...
772 tree [PACKLIST ...]
773 bundle [OPTIONS] script minify + bundle only (uses --lib/--fatlib)
774 minify [OPTIONS] script... minify only, no bundling (print to
775 STDOUT, or -o FILE)
776 --minify script... same as the minify subcommand
777  
778 Options (pack and bundle):
779 -o, --output FILE write the bundled script to FILE (default: a.out)
780 --lib DIR project .pm sources (default: lib)
781 --fatlib DIR fatpacked module tree (default: fatlib; pack uses a temp one)
782 --no-minify bundle verbatim, skipping the PPI minification pass
783 --rename-vars / --no-rename-vars rename my variables (on by default)
784 --rename-subs / --no-rename-subs rename sub/method names (on by default)
785 --rename-modules / --no-rename-modules rename bundled module/class names
786 (on by default; keeps each package's leaf component)
787 --rename-exports / --no-rename-exports also rename names in qw() import /
788 export lists and can()/import() strings (on by default)
789 --rewrite / --no-rewrite additionally shorten keywords/operators to
790 equivalent forms (foreach->for, m/->, +=1->++, etc.);
791 experimental, off by default
792 --compress / --no-compress deflate module sources and the boot program,
793 including its __DATA__/__END__ section (on by default;
794 --no-compress embeds them verbatim)
795 --binary / --no-binary store the compressed sources as escaped binary
796 (default, smallest) or base64 with --no-binary, which
797 keeps the bundle a plain text file (~33% bigger payload)
798 that survives git, diff, mail and copy-paste; neither
799 is readable -- for that, use --no-compress
800 --shebang LINE shebang for the bundle (default: the boot script's
801 own; without one, #!/usr/bin/env perl)
802 --no-inline-plugins keep Module::Pluggable as a runtime dependency
803 --bundle-lib-all include every .pm under --lib, even if unreferenced
804 --include MODULE also bundle MODULE (repeatable; * and ? are wildcards)
805 --exclude MODULE never bundle MODULE (repeatable; * and ? are wildcards)
806 -m/-M/-e/-E build the boot program from perl-binary switches
807  
808 Options (minify):
809 -o, --output FILE write minified output to FILE (needs one script)
810 --rename-vars / --no-rename-vars rename my variables (on by default)
811 --rename-subs / --no-rename-subs rename sub/method names (on by default)
812 --rename-exports / --no-rename-exports also rename names in qw() import /
813 export lists and can()/import() strings (on by default)
814 --rewrite / --no-rewrite additionally shorten keywords/operators to
815 equivalent forms (foreach->for, m/->, +=1->++, etc.);
816 experimental, off by default
817 USAGE
818 }
819  
820 # ---------------------------------------------------------------------------
821 # Dispatch
822 # ---------------------------------------------------------------------------
823 37 my $subcmd = 'pack';
824 37 ▶if (@ARGV && $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/) { } 32 5 37
elsif (@ARGV and $ARGV[0] eq '--minify') { } 1 5 6
Condition100%3/3
@ARGV and $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/
!ll&&!rl&&r
1432
MC/DC100%2/2
@ARGV and $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/
@ARGV $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/
Truth table100%3/3
A and B
A= @ARGVB= $ARGV[0] =~ /^(?:pack|trace|packlists-for|tree|bundle|minify)$/
ABresult
0X0
100
111
825 32 $subcmd = $ARGV[0];
826 32 shift @ARGV;
827 ▶} elsif (@ARGV && $ARGV[0] eq '--minify') {
Condition100%3/3
@ARGV and $ARGV[0] eq "--minify"
!ll&&!rl&&r
131
MC/DC100%2/2
@ARGV and $ARGV[0] eq "--minify"
@ARGV $ARGV[0] eq "--minify"
Truth table100%3/3
A and B
A= @ARGVB= $ARGV[0] eq "--minify"
ABresult
0X0
100
111
828 # Minify-only flag: PPI-minify the given files, no bundling.
829 1 $subcmd = 'minify';
830 1 shift @ARGV;
831 }
832  
833 # Global bundler options are parsed here, before dispatch, so they work on
834 # pack and bundle. trace / packlists-for / tree / minify parse their own.
835 37 %O = (lib => 'lib', fatlib => 'fatlib', output => 'a.out',
836 inline_plugins => 1, no_minify => 0, bundle_lib_all => 0,
837 rename_vars => 1, rename_subs => 1, rewrite => 0, compress => 1,
838 binary => undef, # default: follow compress; --no-binary forces base64
839 shebang => undef, # default: the boot script's own, else env perl
840 rename_modules => 1, rename_exports => 1,
841 fatlib_verbatim => 1,
842 m => [], M => [], e => [], E => [], include => [], exclude => []);
843  
844 37 ▶if ($subcmd eq 'pack' or $subcmd eq 'bundle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd eq "pack" or $subcmd eq "bundle") 19 18 37
Condition100%3/3
$subcmd eq "pack" or $subcmd eq "bundle"
l!l&&r!l&&!r
51418
MC/DC100%2/2
$subcmd eq "pack" or $subcmd eq "bundle"
$subcmd eq "pack" $subcmd eq "bundle"
Truth table100%3/3
A or B
A= $subcmd eq "pack"B= $subcmd eq "bundle"
ABresult
1X1
011
000
845 19 Getopt::Long::Configure('no_ignorecase');
846 my $ok = Getopt::Long::GetOptionsFromArray(\@ARGV,
847 'lib=s' => \$O{lib},
848 'fatlib=s' => \$O{fatlib},
849 'o|output=s' => \$O{output},
850 'no-minify' => \$O{no_minify},
851 'rename!' => \$O{rename_vars}, # legacy alias
852 'rename-vars!' => \$O{rename_vars},
853 'rename-subs!' => \$O{rename_subs},
854 'rename-modules!' => \$O{rename_modules},
855 'rename-exports!' => \$O{rename_exports},
856 'rewrite!' => \$O{rewrite},
857 'compress!' => \$O{compress},
858 'binary!' => \$O{binary},
859 'shebang=s' => \$O{shebang},
860 0 ▶ 'no-inline-plugins' => sub { $O{inline_plugins} = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
861 'bundle-lib-all' => \$O{bundle_lib_all},
862 'include=s@' => $O{include},
863 'exclude=s@' => $O{exclude},
864 'm=s@' => $O{m},
865 'M=s@' => $O{M},
866 'e=s@' => $O{e},
867 'E=s@' => $O{E},
868 19 ▶ 'help' => \my $help,
Branch: 1 branch
BranchTrueFalseTotal
my $ok = &Getopt::Long::GetOptionsFromArray(\@ARGV, "lib=s", \$O{"lib"}, "fatlib=s", \$O{"fatlib"}, "o|output=s", \$O{"output"}, "no-minify", \$O{"no_minify"}, "rename!", \$O{"rename_vars"}, "rename-vars!", \$O{"rename_vars"}, "rename-subs!", \$O{"rename_subs"}, "rename-modules!", \$O{"rename_modules"}, "rename-exports!", \$O{"rename_exports"}, "rewrite!", \$O{"rewrite"}, "compress!", \$O{"compress"}, "binary!", \$O{"binary"}, "shebang=s", \$O{"shebang"}, "no-inline-plugins", sub { $O{"inline_plugins"} = 0; } , "bundle-lib-all", \$O{"bundle_lib_all"}, "include=s\@", $O{"include"}, "exclude=s\@", $O{"exclude"}, "m=s\@", $O{"m"}, "M=s\@", $O{"M"}, "e=s\@", $O{"e"}, "E=s\@", $O{"E"}, "help", \my $help) or exit 2 19 0 19
869 ) or exit 2;
870 19 ▶ if ($help) { print _usage(); exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 18 19
871 die "slimpack: --binary needs compression; drop --no-compress\n"
872 18 ▶ if $O{binary} && !$O{compress};
Branch: 1 branch
BranchTrueFalseTotal
if $O{"binary"} and not $O{"compress"} 1 17 18
Condition66%2/3
$O{"binary"} and not $O{"compress"}
!ll&&!rl&&r
1701
MC/DC50%1/2
$O{"binary"} and not $O{"compress"}
$O{"binary"} not $O{"compress"}
Truth table66%2/3
A and not(B)
A= $O{"binary"}B= not $O{"compress"}
ABresult
0X0
100
111
873 17 ▶ $O{binary} //= $O{compress};
Condition66%2/3
$O{"binary"} //= $O{"compress"}
l!l&&r!l&&!r
2150
MC/DC0%0/2
$O{"binary"} //= $O{"compress"}
$O{"binary"} $O{"compress"}
Truth table66%2/3
A //= B
A= $O{"binary"}B= $O{"compress"}
ABresult
1X1
011
000
874 }
875  
876 35 my $minify_out;
877 35 ▶if ($subcmd eq 'minify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcmd eq "minify") 13 22 35
878 13 Getopt::Long::Configure('no_ignorecase');
879 Getopt::Long::GetOptionsFromArray(\@ARGV,
880 'o|output=s' => \$minify_out,
881 'rename!' => \$O{rename_vars}, # legacy alias
882 'rename-vars!' => \$O{rename_vars},
883 'rename-subs!' => \$O{rename_subs},
884 'rename-exports!' => \$O{rename_exports},
885 'rewrite!' => \$O{rewrite},
886 13 ▶ 'help' => \my $help,
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray(\@ARGV, "o|output=s", \$minify_out, "rename!", \$O{"rename_vars"}, "rename-vars!", \$O{"rename_vars"}, "rename-subs!", \$O{"rename_subs"}, "rename-exports!", \$O{"rename_exports"}, "rewrite!", \$O{"rewrite"}, "help", \my $help) or exit 2 13 0 13
887 ) or exit 2;
888 13 ▶ if ($help) { print _usage(); exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 12 13
889 }
890  
891 34 ▶if ($subcmd eq 'pack') {
Branch: 6 branches
BranchTrueFalseTotal
if ($subcmd eq 'pack') { } 4 30 34
elsif ($subcmd eq 'trace') { } 1 29 30
elsif ($subcmd eq 'packlists-for') { } 3 26 29
elsif ($subcmd eq 'tree') { } 1 25 26
elsif ($subcmd eq 'bundle') { } 13 12 25
elsif ($subcmd eq 'minify') { } 12 0 12
892 4 cmd_pack(@ARGV);
893 } elsif ($subcmd eq 'trace') {
894 1 cmd_trace(@ARGV);
895 } elsif ($subcmd eq 'packlists-for') {
896 3 cmd_packlists_for(@ARGV);
897 } elsif ($subcmd eq 'tree') {
898 1 cmd_tree(@ARGV);
899 } elsif ($subcmd eq 'bundle') {
900 13 cmd_bundle(@ARGV);
901 } elsif ($subcmd eq 'minify') {
902 12 cmd_minify($minify_out, @ARGV);
903 }
904  
905 =head1 NAME
906  
907 slimpack - build a fatpacked, minified standalone Perl script
908  
909 =head1 SYNOPSIS
910  
911 slimpack [options] script
912 slimpack [options] -e/-E 'code' [-m/-M module ...]
913 slimpack trace [--to=FILE|--to-stderr] [--use=MODULE] script
914 slimpack packlists-for MODULE...
915 slimpack tree [PACKLIST ...]
916 slimpack bundle [options] script
917 slimpack minify [options] script... # minify only, no bundling
918 slimpack --minify script... # same, as a flag
919  
920 =head1 DESCRIPTION
921  
922 By default (C<pack>) slimpack drives the whole fatpack pipeline internally by
923 calling App::FatPacker's methods: it traces the boot script, drops core modules
924 with Module::CoreList, resolves their packlists, copies the module sources into
925 a temporary fatlib, then minifies and inlines plugins and emits a single
926 standalone C<#!/usr/bin/perl> script.
927  
928 Each pipeline step is also exposed as its own subcommand with the same arguments
929 as fatpack, so the individual steps can be run by hand.
930  
931 C<minify> (or the C<--minify> switch) runs only the minification pass: each
932 file is read, PPI-minified, and printed to STDOUT (or written with C<-o>).
933 Nothing is traced or bundled. The standalone C<minify> script installed
934 alongside C<slimpack> offers the same mode.
935  
936 =head1 OPTIONS
937  
938 =over 8
939  
940 =item -o, --output FILE
941  
942 Write the bundled script to FILE (default a.out; use C<-> for stdout).
943 C<minify> prints to STDOUT unless C<-o> is given.
944  
945 =item --lib DIR
946  
947 Project C<.pm> sources (default C<lib>).
948  
949 =item --fatlib DIR
950  
951 Fatpacked module tree (default C<fatlib>; C<pack> uses a temporary one).
952  
953 =item --no-minify
954  
955 Bundle modules and boot program verbatim, skipping the PPI minification pass.
956  
957 =item --rename-vars, --no-rename-vars
958  
959 Minify and rename C<my> variables (default), or with C<--no-rename-vars>
960 leave variable names untouched (perl C<process(..., rename_vars => 0)>).
961 Applies to bundling and to the C<minify> mode. The legacy spelling
962 C<--rename>/C<--no-rename> is still accepted as an alias.
963  
964 =item --rename-subs, --no-rename-subs
965  
966 Also rename sub and method names to short names (default): definitions,
967 plain calls, C<< ->method >> chains, C<&name>/C<\&name> code-references and
968 fully-qualified C<< Pkg::name(...) >> calls are all shortened in step, so the
969 bundle shrinks further than variable renaming alone reaches. Names are kept
970 verbatim when Perl dispatches or reads them by name: keywords and builtins,
971 magic methods (C<new>, C<DESTROY>, C<AUTOLOAD>, C<import>, C<can>, C<isa>,
972 C<tie> methods, serialization hooks), string arguments to C<< ->can(...) >>
973 and friends, hashed-deref keys and label/goto targets.
974 Variable-method dispatch C<< $obj->$name >> is unchanged, but a method that is
975 only invoked through C<$obj->can('method')> plus a string would stop matching
976 after the rename; such modules should be packed with C<--no-rename-subs>.
977  
978 When C<bundle>ing, the whole dependency graph is renamed consistently with
979 one shared name map (the C<minify> mode renames each file independently).
980  
981 =item --rename-modules, --no-rename-modules
982  
983 On by default when bundling: rename the namespace components of every bundled
984 package to short names, keeping each package's leaf (final) component verbatim.
985 A bundle controls its own dependency graph, so C<< package App::Thing >>,
986 C<< use App::Thing >>, C<< App::Thing->new >>, qualified C<< App::Thing::sub >>
987 and module/namespace strings (C<"App/Thing.pm">) can all agree on one short
988 name, shrinking the file further than sub renaming alone. The leaf is kept so
989 that filename-based plugin discovery (C<< Module::Pluggable >> search paths
990 built from a namespace string plus a C<.pm> leaf) and loader keys C<App/Thing.pm>
991 stay coherent. Only names that are statically visible are rewritten; a module
992 name assembled at runtime (from user config, directory globbing or
993 C<string-eval>) cannot be followed and must be handled by the app author, so
994 bundles that do that should be packed with C<--no-rename-modules>.
995  
996 =item --rename-exports, --no-rename-exports
997  
998 On by default. With C<--rename-subs>, names in the export/import protocol are
999 renamed in step with the sub they refer to: C<use ... qw(foo)>, C<< @EXPORT_OK
1000 = qw(foo) >>, C<< $o->can('foo') >>, C<< $o->import('foo') >> and the
1001 corresponding bareword forms. Pragma import arguments (C<use lib 'path'>,
1002 C<use parent>, C<use constant>) and any string that is not a single word are
1003 left alone. A program that builds method names from other strings at runtime
1004 should be packed with C<--no-rename-exports>.
1005  
1006 =item --rewrite, --no-rewrite
1007  
1008 Additionally shorten keywords and operators into equivalent forms
1009 (C<foreach> -> C<for>, C<m/.../> -> C</.../> after C<=~>/C<!~>, trailing C<;>
1010 before C<}>, C<$x += 1> -> C<$x++>, C<$x = $x OP $y> -> C<$x OP= $y>,
1011 C<print($x)> -> C<print $x>). Opt-in and experimental; C<--rewrite> enables
1012 it, C<--no-rewrite> restores the default (off).
1013  
1014 When C<bundle>ing, C<--rewrite> also renames C<our>-declared package globals
1015 across the whole bundle (declaration and all references, fully-qualified
1016 C<$PKG::name> or not), since the bundle controls its entire dependency graph.
1017 Postfix declarations (C<return if our $flag>) count too. C<$VERSION>/
1018 C<$AUTOLOAD>/C<@ISA>, ALL_CAPS, control-variable (C<$^...>) and single-character
1019 names are never renamed, and a global is skipped if its name is a C<my>/
1020 C<state> lexical anywhere in the bundle -- so an unqualified reference can
1021 never become ambiguous. Interpolating strings follow the rename while
1022 quoted: C<"$name">, C<qq{_${name}}>, C<< <<"EOF" >> heredoc bodies, C<< <$fh> >>
1023 readlines and backticks are written with the short name (escaped dollars and
1024 literal-only tokens stay as they are), so names quoted in templates shrink
1025 too.
1026  
1027 =item --compress, --no-compress
1028  
1029 Deflate the minified source of each bundled module -- and the boot program
1030 (default), or embed them verbatim with C<--no-compress>. Compression happens
1031 at install time at the best level with the core C<Compress::Raw::Zlib>
1032 module, and the embedded loader (also core-only) decompresses lazily exactly
1033 when a module is C<require>d (or just before the boot program is C<eval>d).
1034 How the compressed bytes are stored is picked with L</--binary, --no-binary>.
1035 Raw C<--no-compress> bundles keep the sources and boot program visible and
1036 readable, at the cost of a larger file.
1037  
1038 A boot-program C<__DATA__>/C<__END__> section is compressed too: it is split
1039 off, bundled on its own, and served through an in-memory handle the loader
1040 installs as C<*main::DATA> before the boot program runs (a C<main::DATA>
1041 that something already opened is left alone, with a warning). Reading,
1042 C<seek>, C<tell> and C<eof> work as usual; the differences are that
1043 C<fileno(DATA)> is -1, as there is no OS file descriptor behind it, and that
1044 C<tell> counts from the start of the data rather than of the script file.
1045  
1046 =item --binary, --no-binary
1047  
1048 Store the compressed sources as escaped binary (the default), or
1049 base64-encoded with C<--no-binary>. In binary form only the bytes that could
1050 be mangled in transit or by the Perl parser are escaped (CR, LF, ctrl-Z,
1051 backslash, the quote), so the payload is about 2% larger than the raw deflate
1052 output instead of base64's 33%, making the bundle roughly 25% smaller; the
1053 loader also drops the C<MIME::Base64> dependency. The escaping keeps the
1054 payload free of CR, LF and ctrl-Z bytes, so running the bundle is safe on
1055 every platform, including Windows text-mode reads.
1056  
1057 A binary bundle still contains NUL and high-bit bytes, so treat it as an
1058 opaque artifact and move it byte-for-byte: an editor that re-saves it as
1059 UTF-8, a copy-paste, or a transfer in text mode corrupts it silently. Use
1060 C<--no-binary> when the bundle must stay a plain text file that survives
1061 those channels -- checked into a repository and diffed, sent by mail, pasted
1062 into a terminal. Neither form makes the sources readable; to inspect or
1063 debug what a bundle runs, use C<--no-compress> (and C<--no-minify> for the
1064 verbatim sources).
1065  
1066 =item --shebang LINE
1067  
1068 The shebang line for the bundle. The default is the boot script's own
1069 shebang; a boot script without one (or a C<-e>/C<-E> program) gets
1070 C<#!/usr/bin/env perl>, which also works on systems without
1071 C</usr/bin/perl>, such as FreeBSD. The leading C<#!> may be left out.
1072  
1073 =item --no-inline-plugins
1074  
1075 Keep Module::Pluggable as a runtime dependency instead of inlining plugins.
1076  
1077 =item --bundle-lib-all
1078  
1079 Include every C<.pm> under --lib, even if not referenced. By default only
1080 statically-reachable lib modules are bundled; fatlib is always fully included.
1081  
1082 =item --include MODULE
1083  
1084 Also bundle MODULE and what it loads, for modules the script loads in a way
1085 slimpack can't see, such as a plugin chosen at runtime. Repeat it for more
1086 modules. C<*> and C<?> work as wildcards, so C<--include 'DateTime::TimeZone::*'>
1087 takes all of them.
1088  
1089 =item --exclude MODULE
1090  
1091 Never bundle MODULE, even when something loads it. Takes the same patterns
1092 as C<--include>. Use it to leave out optional parts of a dependency the
1093 script never uses.
1094  
1095 Moo's Moose support is left out without this: C<Moo::HandleMoose>, which
1096 loads Moose, is only bundled when Moose is in the bundle anyway.
1097  
1098 =item -m MODULE
1099  
1100 Use MODULE with no imports (like perl C<-m>).
1101  
1102 =item -M MODULE[=list]
1103  
1104 Use MODULE, optional import list or version (like perl C<-M>).
1105  
1106 =item -e CODE
1107  
1108 Code to bundle; may be repeated (like perl C<-e>).
1109  
1110 =item -E CODE
1111  
1112 Same as C<-e>, but enables all features (like perl C<-E>).
1113  
1114 =back
1115  
1116 =head1 AUTHOR
1117  
1118 Nicolas Mendoza E<lt>mendoza@pvv.ntnu.noE<gt>
1119  
1120 =head1 LICENSE
1121  
1122 Artistic License 2.0.
1123  
1124 =head1 SEE ALSO
1125  
1126 App::FatPacker, fatpack, App::SlimPacker
↑ Summary lib/App/SlimPacker/Minify.pm »