pl2pm
Convert Perl library (.pl) files to modules (.pm)
SYNOPSIS
pl2pm [file.pl]
PARAMETERS
file.pl
The path to the Perl 4 library file to be converted. If omitted, pl2pm reads from standard input (STDIN) and writes the converted module to standard output (STDOUT).
DESCRIPTION
The pl2pm command is a utility designed to assist in the migration of Perl 4 library files (typically with a .pl extension) into Perl 5 modules (with a .pm extension). Prior to Perl 5, code reusability often involved simple require statements that loaded code into the global namespace. Perl 5 introduced a more robust and organized module system, utilizing the package keyword for defining namespaces and the Exporter module for explicitly declaring subroutines or variables that can be used by other scripts.
pl2pm automates the basic steps of this conversion: it wraps the existing Perl 4 code within a package declaration, ensures a true value is returned at the end of the file (a requirement for Perl 5 modules), and can optionally set up basic Exporter functionality. This tool is particularly useful for modernizing legacy Perl codebases, providing a starting point for converting old procedural libraries into well-structured Perl 5 modules. However, it's a best-effort tool and almost always requires manual review and further adjustments to ensure full compatibility and optimal Perl 5 practices.
CAVEATS
pl2pm provides a rudimentary conversion and does not handle complex Perl 4 idioms, advanced module concepts, or ensure complete compatibility without manual intervention. It's a starting point, not a full migration solution. Users must review the generated .pm file for correctness, add `use strict; use warnings;` if not automatically included, and implement proper exporting or object-oriented structures as needed. It may not be installed by default on all systems, typically being part of the `perl-doc` or `perl-tools` package within Perl distributions.
USAGE WITH OUTPUT REDIRECTION
Since pl2pm often writes its output to standard output (STDOUT) by default, you typically redirect its output to a new .pm file:
pl2pm my_old_script.pl > MyNewModule.pm
This allows you to then inspect and modify MyNewModule.pm. It is crucial to remember that the generated .pm file will almost certainly require manual editing to ensure proper functionality and adhere to modern Perl coding standards.
PERL 4 VS. PERL 5 MODULE SYSTEM
In Perl 4, a 'library' was often just a file that could be `require`d, and its subroutines and variables would implicitly be in the global namespace. Perl 5 introduced explicit packages to create distinct namespaces and the Exporter module to explicitly declare which subroutines or variables are made available to users of the module. pl2pm attempts to bridge this by wrapping the code in a package and adding basic Exporter setup, but sophisticated conversions require a deeper understanding of Perl 5 module best practices for proper encapsulation and API design.
HISTORY
With the significant evolution from Perl 4 to Perl 5 in the mid-1990s, the way Perl code was organized and reused underwent a major change. Perl 5 introduced true modules, `package` declarations, and the `Exporter` mechanism, offering much cleaner namespaces and better encapsulation than Perl 4's simpler `require` system. pl2pm was developed as part of the Perl distribution to help developers bridge this gap, offering a utility to automate the most straightforward aspects of converting older Perl 4 library scripts into the new Perl 5 module format. Its primary utility was during this transition period, making it a less frequently used but historically significant tool for maintaining backward compatibility and aiding modernization.