NAME
Perl::Minify - Minify Perl source with the minify.pl command or from Perl code
SYNOPSIS
# Primary use: the minify.pl command
minify.pl script.pl > script.min.pl
minify.pl script.pl --width 80 > script.min.pl
minify.pl script.pl --sub color > color.min.pl
# Programmatic use
use Perl::Minify qw(minify minify_sub);
my $min = minify($source_str, { wrap => 100 });
DESCRIPTION
Perl::Minify shrinks Perl source by stripping comments and POD, collapsing non-essential whitespace, and optionally shortening lexical variable names and wrapping lines.
Most users want the minify.pl command, which is installed with this distribution. Give it a file and it prints the minified code to STDOUT. The module API is only needed when your own Perl program has to minify source.
COMMAND LINE USAGE
minify.pl FILE [--width N] [--sub NAME]
Reads FILE and prints the minified code to STDOUT. The input file is never modified, so redirect STDOUT to save the result.
# Minify a whole file
minify.pl script.pl > script.min.pl
# Minify and word-wrap to 80 columns
minify.pl script.pl --width 80 > script.min.pl
# Emit only one named subroutine
minify.pl script.pl --sub color > color.min.pl
Arguments
FILE-
Path to the Perl source file (required). If the file is missing or unreadable, a usage message is printed and the script exits with an error.
--width N-
Word-wrap the output to at most
Ncolumns. Without it, the output is a single unwrapped line. --sub NAME-
Minify only the subroutine
NAME. It is printed as bare source with its name kept (seeminify_subunder MODULE API). If no such sub exists, the error is written to STDERR and the script exits 1.
Behavior
minify.pl always enables shorten_vars, and it always uses the default stripping behavior (comments, POD, whitespace, and semicolons are removed). Those settings and optimize are not available as command line flags. If you need different options, use the module API below.
If the source cannot be parsed, the error is written to STDERR and the script exits 1.
MODULE API
Use the module when your own code needs to minify Perl source. Nothing is exported by default; import the functions you need.
minify($code, \%opts?)
Minifies a string of Perl source. Returns the minified string, or undef on PPI parse failure.
minify_sub($code, $sub_name, \%opts?)
Locates a named subroutine, and runs the minification pipeline on it alone. Returns the sub as bare source text (no surrounding use header or file-scope code), or undef on parse failure or if no sub named $sub_name is found. The sub keeps its name by default; pass keep_name => 0 to emit an anonymous sub { ... } instead. A bare sub that depends on file-scope lexicals or state variables will not compile on its own.
minify_error()
Returns the last error message set by a failed minify or minify_sub call (also available as $Perl::Minify::ERROR). Returns an empty string when the most recent call succeeded.
OPTIONS
Options are passed to the module functions as a hashref. All are optional, and unknown keys are ignored. Defaults:
strip_comments 1 remove C<#> line comments
strip_pod 1 remove POD blocks
strip_whitespace 1 collapse non-significant whitespace
strip_semicolons 1 remove safe terminators before C<}> block delimiters
shorten_vars 0 rename lexicals to short names ($c, $d, ...)
wrap 0 if >0, word-wrap output to <=N columns
keep_name 1 preserve the sub name in C<minify_sub> output
escape_newlines 1 write LF/TAB/CR in strings as C<\n \t \r> escapes
optimize 0 EXPERIMENTAL: fold simple Number +/-/* chains
(e.g. C<2 + 3 + 4> becomes C<9>)
LIMITATIONS
shorten_vars
Renaming is skipped for symbols that appear inside string quotes, regex literals, s///e replacement blocks, heredocs, and interpolations. Symbolic references are not renamed. The renamer uses one global per-document map, so two distinct my declarations with the same name (in different scopes) share a single short name.
wrap
An overlong statement with no break point that is outside a string/regex literal is emitted as a single line that may exceed the requested width. This never breaks a string literal mid-character.
INSTALLATION
perl Makefile.PL
make
make test
make install
Requires Perl v5.16 or later, and the CPAN module PPI (cpan PPI).
AUTHOR
Scottchiefbaker - https://github.com/scottchiefbaker/
LICENSE
Same terms as Perl itself.