NAME

App::SlimPacker - fatpack-style bundling helpers with PPI-based minification for standalone Perl scripts

VERSION

Version 0.08

SYNOPSIS

use App::SlimPacker qw(minify_file pack_string);

# minify a module and turn it into a Perl string literal for embedding
my $min     = minify_file('lib/My/Module.pm');
my $literal = pack_string($min);    # 'package My::Module;...' or q^...^

DESCRIPTION

App::SlimPacker packs a Perl script and the non-core modules it uses into a single file that runs on a plain perl, with nothing to install. It minifies what it packs: comments, POD and whitespace are removed, variables, subs and package names are shortened consistently across the whole bundle, and the result is compressed.

It finds the modules by compiling the script to see what it loads, and by following the use and require statements in what it finds. It leaves out core modules and copies the rest from their installed locations. Modules found with Module::Pluggable are listed in the bundle directly, since there is no directory to scan at runtime.

Most users want the slimpack command. This module holds the helpers it is built from, and re-exports the minifier from App::SlimPacker::Minify.

EXPORTS

Nothing by default. Import what you need:

use App::SlimPacker qw(pack_string inline_plugins);

FUNCTIONS

pack_string($literal)

Returns the shortest exact Perl string literal for an arbitrary byte string. Used by the bundler to embed minified module sources, where B::perlstring would double-quote and escape every sigil ("\$x", "\@_"), inflating the bundle by ~15%. pack_string never uses perlstring; content is escaped in place inside quote literals, keeping $ @ % " raw.

Two literal families are priced so the cheaper wins:

  • '...' -- cost 2 (delimiters) + backslash occurrences + single-quote occurrences, since both ' and \ must be escaped;

  • a q<delim> literal -- delimiter chosen from ^ ~ | ? , ! # & - + * / % :, the candidate occurring fewest times in the content. Cost 3 + backslash occurrences + delimiter occurrences, since backslashes and the delimiter character must be escaped. = is excluded (a q=...= literal breaks when the content contains a comparison like = >), as are < and > (paired delimiters already close on >, so q<< ... > > cannot nest); every candidate above is validated safe with escaping.

' occurs far more often in code than ^ or any other candidate, so the q<delim> form wins as soon as the content holds a quote, and wins ties for the same reason. No candidate needs to be absent: its occurrences are simply escaped (\^). The returned literal always evals back to the exact input bytes.

plugin_search_paths($program)

Extracts Module::Pluggable search paths from a program's use Module::Pluggable (...) statement. Returns a hashref of namespace => 1.

inline_plugins($program, \%classes)

Inlines a plugin class list into plugins() calls based on the Module::Pluggable search paths in $program and the available classes (hashref of Class::Name => 1). Classes are matched one level deep (Module::Pluggable's default), sorted. The use Module::Pluggable statement is removed so it never loads at runtime.

Returns the modified program text. Programs without Module::Pluggable or without a search_path are returned unchanged.

perl_switches(\@m, \@M, \@e, \@E)

Builds a Perl program string from -m/-M/-e/-E switch arguments (perl-binary style). Returns the program text with appropriate use statements prepended.

AUTHOR

Nicolas Mendoza, <mendoza at pvv.ntnu.no>

LICENSE AND COPYRIGHT

This software is licensed under the Artistic License 2.0. See the LICENSE file in this distribution for the full text.

SEE ALSO

App::SlimPacker::Minify, slimpack, PPI, App::FatPacker