Section 17.9.  The Standard Library

Table of Contents

17.9. The Standard Library

Use core modules wherever possible.

It's definitely best practice to avoid unnecessary work, and code reuse is a primary example of that. Perl has two main software libraries of reusable code: the standard Perl library and the CPAN. It's almost always a serious mistake to start hacking on a solution without at least exploring whether your problem has already been solved.

The library of modules that come standard with every Perl distribution is the ideal place to start. There are no issues of availability: if a core module solves your problem, then that solution will already have been installed anywhere that Perl itself is available. There are no issues of authorization either: if Perl has been approved for production use in your organization, the library modules will almost certainly be acceptable too.

Another major advantage is that the standard library contains some of the most heavily used Perl modules available. Frequent use means they're also some of the most strenuously stress-testedand therefore more likely to be both reliable and efficient.

Perl's standard library contains modules for creating declaration attributes; optimizing the loading of modules; using arbitrary precision numbers, complex numbers, and a full range of trigonometric functions; adding I/O layers to the standard streams; interfacing with flat-file and relational databases; verifying and debugging Perl code; benchmarking and profiling program performance; CGI scripting; accessing the CPAN; serializing and deserializing data structures; calculating message digests; dealing with different character encodings; accessing system error constants; imposing exception semantics on failure-returning functions and subroutines; processing filenames in a filesystem-independent manner; searching for, comparing, and copying files; filtering source code; command-line argument processing; performing common operations on scalars, arrays, and hashes; internationalizing and localizing programs; setting up and using pipes and sockets; interacting with network protocols (including FTP, NNTP, ping, POP3, and SMTP); encoding and decoding MIME; data caching and subroutine memoization; accessing the complete POSIX function library; processing POD documentation; building software test suites; text processing; thread programming; acquiring and manipulating time and date information; and using Unicode.

There is rarely a good reason to roll your own code for any of those common tasks. For example, if you need a temporary filename, it may be tempting to throw the necessary code together yourself:

    # Range of acceptable replacements for placeholders in template...
    my @letter = ('A'..'Z');

    # Given a template, fill it in randomly, making sure the file doesn't exist...
    sub tempfile {
        my ($template) = @_;
        my $filename;

        while (1) {
            $filename = $template;
            $filename =~ s{ X }{$letter[rand @letter]}gexms;
            last ATTEMPT if ! -e $filename;

        return $filename;

    my $filename = tempfile('.myapp_XXXXXX');
    open my $fh, '>', $filename
        or croak "Couldn't open temp file: $filename";

But that's a waste of time and effort, when the necessary functionality is better implemented, more thoroughly tested, and already sitting there waiting on your system:

    use File::Temp qw( tempfile );

    my ($fh, $filename) = tempfile('.myapp_XXXXXX');

The perlmodlib documentation is a good place to start exploring the Perl Standard Library.

    Table of Contents
    © 2000- NIV