Previous Page Next Page

12.2. The Standard Perl Library

The Perl distribution comes with a number of standard Perl library functions and packages. The Perl 4 library routines are procedural programs and their names end in a .pl extension. The Perl 5 modules end with a .pm extension. In Perl 5, the .pm files are called modules. The .pm files are modules written in two programming styles: procedural and object oriented. The module filenames start with a capital letter. The .pm filenames starting with a lowercase letter are a special type of module, called a pragma. A pragma is a module that tells the compiler that certain conditions must be checked in a Perl program before it can run. Files that have no extension at all are subdirectories. They contain common modules that are divided into several .pm files; for example, the Math subdirectory contains BigFloat.pm, BigInt.pm, Complex.pm, and Trig.pm.

Here is a sample listing from the standard Perl 5 library:[2]

[2] The pathname to the standard Perl library is determined at the time Perl is installed. This can be assigned either a default value or a pathname designated by the person installing Perl.

AnyDBM_File.pm

AutoLoader.pm

AutoSplit.pm

B

B.pm

Benchmark.pm

ByteLoader.pm

CGI

CGI.pm

CORE

CPAN

CPAN.pm

Carp

Carp.pm

Class

Config.pm

Config.pm~

Cwd.pm

DB.pm

Data

Devel

DirHandle.pm

Dumpvalue.pm

DynaLoader.pm

English.pm

Env.pm

Errno.pm

Exporter

Exporter.pm

ExtUtils

Fatal.pm

Fcntl.pm

File

FileCache.pm

FileHandle.pm

FindBin.pm

Getopt

I18N

IO

IO.pm

IPC

Math

Net

O.pm

Opcode.pm

POSIX.pm

POSIX.pod

Pod

SDBM_File.pm

Safe.pm

Search

SelectSaver.pm

SelfLoader.pm

Shell.pm

Socket.pm

Symbol.pm

Sys

Term

Test

Test.pm

Text

Thread

Thread.pm

Tie

Time

UNIVERSAL.pm

User

XSLoader.pm

abbrev.pl

assert.pl

attributes.pm

attrs.pm

auto

autouse.pm

base.pm

bigfloat.pl

bigint.pl

bigrat.pl

blib.pm

bytes.pm

bytes_heavy.pl

cacheout.pl

charnames.pm

chat2.pl

complete.pl

constant.pm

ctime.pl

diagnostics.pm

dotsh.pl

dumpvar.pl

exceptions.pl

fastcwd.pl

fields.pm

filetest.pm

find.pl

finddepth.pl

flush.pl

ftp.pl

getcwd.pl

getopt.pl

getopts.pl

hostname.pl

importenv.pl

integer.pm

less.pm

lib.pm

locale.pm

look.pl

network.pl

newgetopt.pl

open.pm

open2.pl

open3.pl

ops.pm

overload.pm

perl5db.pl

perllocal.pod

ppm.pm

pwd.pl

re.pm

shellwords.pl

sigtrap.pm

stat.pl

strict.pm

subs.pm

syslog.pl

tainted.pl

termcap.pl

timelocal.pl

unicode

utf8.pm

utf8_heavy.pl

validate.pl

vars.pm

warnings

warnings.pm

12.2.1. The @INC Array

The special array @INC contains the directory path to where the library routines are located. To include directories not in the @INC array, you can use the -I switch[3] at the command line, or set the PERL5LIB environment variable to the full pathname. Normally, this variable is set in one of your shell initialization files, either .login or .profile if using UNIX. See Figure 12.4 if using Windows.

[3] See Table A.18 in Appendix A for a description of the -I switch.

Figure 12.4. Setting the PERL5LIB environment variable in Windows.


Example 12.4.

1   $ perl -V  (Windows Command Line)
    Summary of my perl5 (revision 5 version 8 subversion 8)
    configuration:
  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
       hint=recommended, useposix=true, d_sigaction=undef
     usethreads=define use5005threads=undef useithreads=define
     usemultiplicity=define
     useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
     use64bitint=undef use64bitall=undef uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='cl', ccflags ='-nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32
         -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DNO_HASH_SEED
         -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS
         -DUSE_PERLIO -DPERL_MSVCRT_READFIX',
     optimize='-MD -Zi -DNDEBUG -O1',
       ... output continues here
2   @INC:

------------------------------------------------

3   $ perl -e 'print "@INC\n"'     (Windows)
     @INC:
     C:/perl/site/lib
     C:/perl/lib
     .

----------------------------------------------------------
4  $ perl -V  (Linux)
     @INC:
     /usr/local/lib/perl5/5.8.6/i686-linux
     /usr/local/lib/perl5/5.8.6
     /usr/local/lib/perl5/site_perl/5.8.6/i686-linux
     /usr/local/lib/perl5/site_perl/5.8.6
     /usr/local/lib/perl5/site_perl/5.8.5/i686-linux
     /usr/local/lib/perl5/site_perl/5.8.5
     /usr/local/lib/perl5/site_perl
      .

					  

Explanation

  1. Perl with the -V option displays version, configuration, and library information.

  2. The @INC array contains the directories where the Perl libraries are located.

  3. At the Windows command-line prompt, the @INC array is printed. The first element in the colon-separated path is C:/Perl/site/lib, where Windows-specific library functions are stored, and the second element is C:/Perl/lib, the standard Perl library. The final dot is important. Any libraries stored in the current working directory (folder) will be included when Perl searches for these files.

  4. The pathnames containing the word site are site-specific and contain library routines that have been downloaded for this particular site or architecture. Some of the site-specific libraries come with the standard distribution. Those pathnames with the word site or linux (solaris if you are using Solaris UNIX) are for Linux-specific library routines and modules. /usr/local/lib/perl5/5.8.6 is where the standard Perl library resides, and the final dot represents the current working directory.

Setting the PERL5LIB Environment Variable

If you are using UNIX/Linux operating systems, to add new path elements to the @INC array, the PERL5LIB environment variable can be set in your startup initialization files.

In .login for the C and TC shells:

setenv PERL5LIB "directory path"

In .profile for the Bourne, Korn, and Bash shells:

PERL5LIB="directory path"; export PERL5LIB

If you are using Windows, go to the Start menu, then to Settings, then System Properties, then Environment Variables, and finally New. Refer back to Figure 12.4.

To give your own library routines precedence over those in listed in the @INC array, you can put the following line in your program:

unshift(@INC,".");

Unshift causes the . to be prepended to the @INC array, making your present working directory the first element in the search path. If your library is in a different directory, use its full pathname rather than the dot.

12.2.2. Packages and .pl Files

Most of the library routines found in the standard Perl library ending in .pl were written in the Perl 4 days. They consisted of subroutines contained within a package. The library files are still available but have largely been replaced with modules or .pm files.

The require Function

In order to include and execute routines from the standard Perl library (specifically .pl files) or Perl code from any other file, the require function can be used, which is similar to the C #include statement. The require function checks to see if the library has already been included, unlike the eval and the do functions, which are older methods for including files. Without an argument, the value of $_ is included. If the @INC array does not have the correct path to the library, the require will fail with a message such as the following:

Can't locate pwd.pl in @INC at package line 3.

The require function loads files into the program during runtime. The @INC array is also updated at runtime.

Format

require (Expr)
require Expr
require


Including Standard Library Routines

To see an example of a library routine in Perl's library, the pwd.pl routine (written by Larry Wall) is shown in Example 12.5. The pwd package consists of two subroutines that update the PWD environment variable after the chdir function has been executed. The value of $ENV{PWD} is the present working directory.

Notice that in the following Perl library function, the package pwd is declared before the subroutines are defined, thus placing the rest of the file within the pwd package. All variables belong to the package. The names of the subroutines are explicitly switched to package main. They may be called from your main package, but the variables remain local to the pwd package. (The apostrophe is used here instead of the two colons to switch namespaces. The apostrophe is the Perl 4 symbol for switching namespaces.)

The following example is a sample .pl routine from Perl's standard library.

Example 12.5.

(The pwd package)
    #
1   # Usage:
2   #    require "pwd.pl";
    #    &initpwd;
    #    ...
    #    &chdir($newdir);
3   package pwd;
4   sub main'initpwd {
        if ($ENV{'PWD'}) {
        local($dd,$di) = stat('.');
        local($pd,$pi) = stat($ENV{'PWD'});
        return if $di == $pi && $dd == $pd;
    }
    chop($ENV{'PWD'} = 'pwd');
    }
5   sub main'chdir {
        local($newdir) = shift;
        if (chdir $newdir) {
           if ($newdir =~ m#^/#) {
               $ENV{'PWD'} = $newdir;
           }
           else {
               local(@curdir) = split(m#/#,$ENV{'PWD'});
               @curdir = '' unless @curdir;
               foreach $component (split(m#/#, $newdir)) {
                   next if $component eq '.';
                   pop(@curdir),next if $component eq '..';
               }
               $ENV{'PWD'} = join('/',@curdir) || '/';
           }
        }
        else {
            0;
        }          # Return value
    }

6   1;    <---IMPORTANT

					  

Explanation

  1. The usage message tells you how you're supposed to use this package.

  2. In the usage message, you are being told to be sure to require the file pwd.pl. The two subroutines that will be called are initpwd and chdir. The chdir function requires an argument, which is a directory name.

  3. The package pwd is declared. This is the only package in the file.

  4. The subroutine initpwd is defined. Notice that its name is qualified as a symbol for the main package. This means that when you call initpwd from your main package, you won't have to mention the package pwd at all.

  5. The chdir subroutine is defined.

  6. The 1 is required at the end of this package for the require function. If the last expression evaluated in this file is not true, the require function will not load the file into your program.

The following example uses a library routine in the script.

Example 12.6.

(The Script)
    #!/bin/perl
1   require "ctime.pl";
2   require "pwd.pl";
3   &initpwd;         # Call the subroutine
4   printf "The present working directory is %s\n", $ENV{PWD};
5   &chdir ("../..");
6   printf "The present working directory is %s\n", $ENV{PWD};
7   $today=&ctime(time);
8   print "$today";

(Output)
4   The present working directory is /home/jody/ellie/perl
6   The present working directory is /home/jody
8   Wed  Mar 14 11:51:59 2007

Explanation

  1. The ctime.pl Perl standard library function is included here.

  2. The pwd.pl Perl standard library function is included.

  3. The initpwd subroutine is called for the pwd.pl function. It initializes the value of PWD.

  4. The present value of the environment variable PWD is printed.

  5. A call to chdir changes the present working directory.

  6. The present updated value of the environment variable PWD is printed.

  7. Today's date is set in a human-readable format by the subroutine ctime, from ctime.pl.

  8. Today's date is printed in its new format.

Using Perl to Include Your Own Library

The following example shows you how to create your own library functions and include them into a Perl script with the require function. When including user-defined routines or adding routines to a library, make sure to include 1; (a nonzero value) as the last line of the routine. If you do not return a true value as the last line of your library file, this is the type of error you will get from the require function:

average.pl did not return a true value at user.plx line 3.

Example 12.7.

(The midterms Script)
    #!/bin/perl
    # Program name: midterms
    # This program will call a subroutine from another file
1   unshift(@INC, "/home/jody/ellie/perl/mylib");
2   require "average.pl";
    print "Enter your midterm scores.\n";
    @scores=split(' ', <STDIN>);
3   printf "The average is %.1f.\n", average::ave(@scores);
    # The ave subroutine is found in a file called average.pl
-----------------------------------------------------------
4   $ cd mylib         # Directory where library is located
-----------------------------------------------------------

(The Script)
5   $ cat average.pl   # File where subroutine is defined
6   package average; # Declare a package
     # Average a list of grades
7   sub ave {
8       my(@grades)=@_;
        my($num_of_grades)=$#grades + 1;
        foreach $grade ( @grades ){
             $total += $grade;
        }
9       $total/$num_of_grades;   # What gets returned
    }
10  1; # Make sure the file returns true or require will not succeed!

					  

Explanation

  1. The unshift function prepends the @INC array with the pathname to your personal directory, mylib.

  2. The require function first checks the @INC array to get a listing of all directories in which it will search for the .pl file. The require function includes the Perl function average.pl.

  3. The ave function is called and returns a value to be stored in the scalar $average. Since the subroutine ave() is defined in a package called average in the library file, the package name (and two colons) must precede the call to ave(). If not, Perl will try to find ave() in main, the current package.

  4. We will change directories to mylib from the command line. The dollar sign is the shell prompt.

  5. Now we look at the contents of the file average.pl.

  6. A package called average is declared.

  7. A subroutine called ave() is defined.

  8. It will accept a list of grades as parameters. The list is made local with the my function.

  9. The expression is evaluated and returned.

  10. This statement evaluates to true and is located at the end of the file. The require function needs a true return value in order to load this file when asked.

12.2.3. Modules and .pm Files

When using one of the modules (those files ending in .pm) provided in the standard Perl library, you must first make sure the @INC array contains the full pathname to your library distribution and that you include the use function with the module name.

If you are trying to find out how a particular library module works, you can use the perldoc command to get the documentation. (The perldoc command does not work for .pl files from the library.) For example, if you want to know about the CGI.pm module, type at the command line

perldoc CGI

and the documentation for the CGI.pm module will be displayed. If you type

perldoc English

the documentation for the English.pm module will be displayed.

The use Function (Modules and Pragmas)

The use function allows Perl modules and pragmas to be imported into your program at compile time. The use function will not import a module if the module's filename does not have the .pm extension. The require function does the same thing but does not do imports and loads the module at runtime.

A module is a file in a library that behaves according to certain set of conventions. The modules in the standard Perl library are suffixed with the .pm extension. They can also be found in subdirectories. For example, the module Bigfloat.pm is found in a subdirectory called Math. To use a module found in a subdirectory, the directory name is followed by two colons and the name of the module, such as Math::Bigfloat.pm. (Although using the two colons makes it appear that Bigfloat.pm is in a package called "Math," in this context the two colons are used to separate the Math directory/folder from the module. The colon then will be translated to a forward slash for UNIX-type operating systems and a backslash for Windows sytems.)

A pragma, spelled in lowercase letters, is a directive to the compiler that your program should behave in a certain way and, if it doesn't, the program will abort. Some common pragmas are lib, strict, subs, and diagnostics. For a list of modules and pragmas, see Tables A.4 and A.5 in Appendix A.

In object-oriented terminology, subroutines are called methods. If you receive diagnostics using the term method, for now just think of methods as glorified subroutines. Many of the modules in the library use object-oriented Perl. The modules discussed in this chapter do not require any understanding of objects. For a complete discussion on how to use the object-oriented modules, see Chapter 14, "Bless Those Things! (Object-Oriented Perl)."

Format

use Module;
use Module ( list );
use Directory::Module;
use pragma (list);
no pragma;


12.2.4. Exporting and Importing

In the export/import business, someone exports his goods and the someone who imports them is waiting on the other side. Let's say a wine maker in California has four great-tasting wines in his cellar, and he decides to export three of the wines to buyers but keep the best ones for himself. So he creates an export list and tacks it to the wall of his cellar, and, when the buyer comes, the buyer selects only those wines on the export list. The buyer is the importer. There's nothing preventing the importer from taking all four of the wines, but if he follows the guidelines of the export list, he will take only those listed.

When you use a Perl module, you are like the buyer. You import symbols (subroutines, variables, etc.) from the export list provided by the module. You take symbols from another package and add them to your own symbol table. You can take what's on the export list by default, you can ask for specific symbols from the list, or you can even exclude some or all of the symbols on the list. The business of exporting and importing is really just a way of getting symbols into the namespace of your program package so you don't have to fully qualify all the imported names with the module package name and two colons, such as Module::fun1. What you import can be listed after the use directive, such as use Module qw(fun1 fun2);.

The Exporter Module

The exporting module sends symbols to the user of the module. The Exporter.pm module found in the standard Perl library supplies the necessary semantics for modules to be able to export symbols. It implements an import method that allows a module to export functions and variables to its users' namespaces. As we discussed earlier, symbols are stored as a hash of typeglobs on the symbol table for the package. The import routine creates an alias for the symbol that is being taken from one package and used in another. Consider the statement:

*Package_mine::somefunction = \&Package_exporter::somefunction

The package called "Package_mine" is importing a symbol called "somefunction" from "Package_exporter". The symbol is the name of a subroutine called "somefunction". A reference to "somefunction" in the exporting package is assigned to a typeglob in "Package_mine"; i.e., put on the symbol table of "Package_mine".

The Exporter module implements an import routine that uses similar semantics as shown previously. Although you can write your own import function, many modules use Exporter because it provides a highly flexible interface and is easy to use.

Perl automatically calls the import method when processing a use statement for a module. Modules and use are documented in perlfunc and perlmod. Understanding the concept of modules and how the use statement operates is important to understanding the Exporter. The Exporter.pm module is an object-oriented module that functions as a class. Other modules inherit from the Exporter class the capability to export symbols. (See Chapter 14 for more on object-oriented programs.) Inherited classes must be listed in the @ISA array.

require Exporter;
our @ISA=qw(Exporter);[4]

[4] Note that the Exporter module is not enclosed in double quotes when used as an argument to require and that .pm is missing. This tells the compiler two things: if, for example, the module is Math::BigFloat, that will be translated to Math/BigFloat, and if there are indirect method calls within the module, they will be treated as object-oriented method calls, not ordinary subroutine calls.

The names listed in the @EXPORT array are by default switched into the namespace of the program using the module; the names on the @EXPORT_OK array are added to the user's namespace only if requested. The @EXPORT_FAIL array lists those symbols that cannot be exported. The %EXPORT_TAGS hash contains groups of symbols. If the module is imported with use and parentheses are added to the module name, as in use Module(), none of the symbols is exported to the module. Table 12.1 describes the exporting modules and the users of the modules.

Table 12.1. Exporting Symbols
The Exporting ModuleWhat It Means
package Testmodule;Package declaration.
require Exporter;Use the Exporter.pm module to export symbols from package to package.
our @ISA = qw(Exporter);@ISA contains the names of base classes needed to do the exporting.
our @EXPORT = qw($x @y z);Symbols in this list are automatically exported to the user of this module.
our @EXPORT_OK = qw(fun b c);Symbols in this list are exported only if requested by the user of this module.
our @EXPORT_FAIL=qw(fun3 e);These symbols are not to be exported.[a]
our %EXPORT_TAGS= ( ':group1' => [ qw( a b c ) ], ':group2' => [qw( $x @y %c ) ] );The key ':group1' represents the symbols a, b, and c (function names), collectively; ':group2' represents the symbols $x, @y, and %c, collectively.
The Importing ModuleWhat It Means
use Testmodule;Testmodule is loaded.
use Testmodule qw( fun2 );Testmodule is loaded; fun2 is imported.
use Testmodule();Testmodule is loaded, no symbols imported.
use Testmodule qw(:group1 !:group2 );Testmodule imports symbols from group1 (See:%EXPORT_TAGS hash, above) but not symbols from group2.
use Testmodule qw(:group1 !fun2);Testmodule imports symbols from group1, not the symbol fun2.
use Testmodule qw(/^fu/);Testmodule imports symbols whose names start with fu.
SomeModule.pmUser of the Module
package SomeModule.pm;use SomeModule;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT=qw(a b c);

sub a { }

sub b { }
sub c { }
sub d { }
1;
&a;
&b;
&c;                                     # functions and variables automatically
                                          # imported from the @EXPORT array
&SomeModule::d;            # d isn't on the @EXPORT list;
                                         # its name must be fully qualified
package SomeModule.pm;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK=qw(a b c);
sub a {}
sub b {}
sub c {}
sub d {}
1;
use SomeModule qw(a c);     # must ask for symbols
&a;                                            # or they won't be imported
&c;
&SomeModule::c;                  # must fully qualify names of symbols
SomeModule::d;                     # not asked for from @EXPORT_OK list


[a] Variables have the funny symbol preceding their name; subroutines don't have a funny symbol. a, b, and c refer to subroutines with those names.

Using perldoc to Get Documentation for a Perl Module

When you are ready to start using Perl modules, you can use the built-in perldoc command to retrieve the documentation from Perl 5 modules that were formatted with special pod directives (see Chapter 15 for details on this type of formatting). The following excerpt was taken from the CGI.pm documentation.

Note: If you are interested in testing the code displayed in the CGI.pm module, cut and paste the following text, highlighted in bold, and put it into a file. Save the file in the cgi-bin directory under your Web server's root. If using UNIX, turn on execute permission with chmod. Then execute the script in your browser. If you are unfamiliar with how to execute CGI programs, go to Chapter 16 in this book for detailed instructions.

Example 12.8.

(At the Command Line)
1  perldoc CGI
(Output)
NAME
    CGI - Simple Common Gateway Interface Class

SYNOPSIS
      # CGI script that creates a fill-out form
      # and echoes back its values.

2     use CGI qw/:standard/;
3     print header,
           start_html('A Simple Example'),
           h1('A Simple Example'),
           start_form,
           "What's your name? ",textfield('name'),p,
           "What's the combination?", p,
           checkbox_group(-name=>'words',
                          -values=>['eenie','meenie','minie','moe'],
                          -defaults=>['eenie','minie']), p,
           "What's your favorite color? ",
                popup_menu(-name=>'color',
                -values=>['red','green','blue','chartreuse']),p,
          submit,
           end_form,
           hr;

      if (param()) {
          my $name      = param('name');
          my $keywords  = join ', ',param('words');
          my $color     = param('color');
          print "Your name is",em(escapeHTML($name)),p,
                "The keywords are: ",em(escapeHTML($keywords)),p,
                "Your favorite color is ",em(escapeHTML($color)),
                hr;
      }

ABSTRACT
    This perl library uses perl5 objects to make it easy to create Web
    fill-out forms and parse their contents. This package defines CGI
-- More  --

					  

Explanation

  1. The perldoc command produces documentation for modules using Perl's POD directives. (See Chapter 15 for details on how to use Plain Ole Documentation, POD.)

  2. This line is used to load the CGI module, function-oriented style, and import all of its standard symbols for creating CGI scripts containing HTML and Perl functions. In the CGI.pm module, itself, the list of symbols is defined using the %EXPORT_TAGS hash, where "standard" is the key, followed by list of values (also tags to include a huge array of function names): ':standard' => [qw/:html2 :html3 :html4 :form :cgi/]. These symbols will automatically be imported by the user of the module.

  3. This section of the documentation shows you how to use some of the features of CGI.pm. For more on CGI, see Chapter 16.

Using a Perl 5 Module from the Standard Perl Library

The following module, English.pm, provides aliases for built-in variables, such as $_ and $/. For any variables that are also part of the awk programming language, there are both long and short English names for the variable. For example, the number of the current record is represented as $. in Perl and NR in awk. The English names are either $RS (awk) or $INPUT_RECORD_SEPARATOR (Perl).

Example 12.9.

(The Script)
    #!/usr/bin/perl
1   use English;    # Use English words to replace
                    # special Perl variables
2   print "The pid is $PROCESS_ID.\n";
3   print "The pid is $PID.\n";
4   print "The real uid $REAL_USER_ID.\n";
5   print "This version of perl is $PERL_VERSION.\n";

(Output)
2   The pid is 948.
3   The pid is 948.
4   The real uid 9496.
5   5.6.0.

Explanation

  1. The English.pm module is loaded in at compile time with the use directive.

  2. The process ID number of this process is printed.

  3. The $PID variable is the same as $PROCESS.ID.

  4. The real user ID for the user of this program is printed.

  5. The version of Perl is 5.6.0.

The following example is a sample .pm file from Perl's standard library.

Example 12.10.

(A Module from the Standard Perl Library)[a]
1   package Carp;
    # This package implements handy routines
    # for modules that wish to throw
    # exceptions outside of the current package
2   require Exporter;
3   @ISA = Exporter;
4   @EXPORT = qw(confess croak carp);
5   sub longmess {
6       my $error = shift;
        my $mess = "";
        my $i = 2;
    my ($pack,$file,$line,$sub);
    while (($pack,$file,$line,$sub) = caller($i++)) {
        $mess .= "\t$sub " if $error eq "called";
        $mess .= "$error at $file line $line\n";
        $error = "called";
    }
    $mess || $error;
    }
    sub shortmess {
        my $error = shift;
        my ($curpack) = caller(1);
        my $i = 2;
        my ($pack,$file,$line,$sub);
        while (($pack,$file,$line,$sub) = caller($i++)) {
           return "$error at $file line $line\n"
                       if $pack ne $curpack;
    }
    longmess $error;
    }
7   sub confess { die longmess @_; }
8   sub croak { die shortmess @_; }
9   sub carp { warn shortmess @_; }

					  

Explanation

  1. This is the package declaration. The package is named after the file it resides in, Carp.pm. The functions carp, croak, and confess generate error messages, such as die and warn. The difference is that with carp and croak, the error is reported at the line in the calling routine where the error was invoked, whereas confess prints out the stack backtrace showing the chain of subroutines that was involved in generating the error. It prints its message at the line where it was invoked.

  2. The Exporter module is required so that subroutines and variables can be made available to other programs.

  3. The @ISA array contains the names of packages this module will use. Perl implements inheritance by listing other modules this package will use in the @ISA array.

  4. The @EXPORT array lists the subroutines from this module that will be exported by default to any program using this module. The subroutines confess, croak, and carp are now available to you if you want to use this module. Since longmess and shortmess are not on the list to be exported, you cannot directly use these subroutines.

  5. This is a subroutine definition for this module.

  6. The error message provided as an argument to confess is passed here and shifted into the $error scalar.

  7. The definition for subroutine confess is to call die with the return value of longmess.

  8. The definition for subroutine croak is to call die with the return value of shortmess.

  9. The definition for subroutine carp is to call warn with the return value of shortmess.

[a] The Carp.pm module has been rewritten and is much larger with an additional function, called cluck, but this version is used here because it is easier to see how the exporting of subroutines works.

12.2.5. How to "use" a Module from the Standard Perl Library

The following example demonstrates how to use the Carp module from the standard Perl library. The first step in using a module is to read the documentation. This can be done with the perldoc command.

$ perldoc Carp

NAME
    carp - warn of errors (from perspective of caller)

    cluck - warn of errors with stack backtrace (not exported by default)

    croak - die of errors (from perspective of caller)

    confess - die of errors with stack backtrace

    shortmess - return the message that carp and croak produce

    longmess - return the message that cluck and confess produce
SYNOPSIS
    use Carp;
    croak "We're outta here!";

    use Carp qw(cluck);
    cluck "This is how we got here!";

    print FH Carp::shortmess("This will have caller's details added");
    print FH Carp::longmess("This will have stack backtrace added");

DESCRIPTION
    The Carp routines are useful in your own modules because they act
    like die() or warn(), but with a message which is more likely to be
    useful to a user of your module. In the case of cluck, confess, and
    longmess that context is a summary of every call in the call-stack.
    For a shorter message you can use carp, croak or shortmess which
    report the error as being from where your module was called. There
    is no guarantee that that is where the error was, but it is a good
    educated guess.
         <continues here>

					  

The "use" directive makes sure the requested module is loaded at compile time. If there is a list following the module name, that list represents symbols that will be exported from the module to be used (imported) in your program. In the Carp.pm module, one of the functions is called croak. If requested, the user can call the croak function without fully qualifying the symbol name with the :: syntax (Carp::croak).

Example 12.11.

Explanation

  1. The Carp module is used (loaded into) in the current package, main. Because croak is listed after the module name, it is the only function name that can be used in this script without fully qualifying its name. If, for example, you try to use confess rather that Carp::confess, the program will be aborted with an error message such as: String found where operator expected at cluck.plx line 9, near "confess "Illegal value:""       (Do you need to predeclare confess?)...

  2. The user is asked for input.

  3. The subroutine try is called and passed the scalar $grade.

  4. The if subroutine try is defined.

  5. The argument passed in is assigned to $number.

  6. The croak function is called with an error message. The croak function was exported by the Carp module. The program will die if the value of $number is not in the range between 0 and 100. The error message reports the line where the program died, as well as the name of the package, subroutine name, and the number of the line where the subroutine was invoked.

Example 12.12.

(Using a Module from the Standard Perl Library in a Script)
   #!/bin/perl
1  use Carp qw(cluck);  # cluck not exported by default

  print "Give me a grade: ";
  $grade = <STDIN>;
2  try($grade);     # Call subroutine

   sub try{
      my($number)=@_;
      cluck "Illegal value: " if $number < 0 || $number > 100;
   }
  print "That was just a warning. Program continues here.\n";

(Output)
2   Give me a grade: 200
6   Give me a grade: 200
    Illegal value:  at cluck.plx line 9
        main::try('200\x{a}') called at cluck.plx line 5
    That was just a warning. Program continues here.

Explanation

  1. The Carp module is used (loaded into) in the current package, main. The cluck function will be used in this script. If you look at the documentation for cluck, that symbol is not exported by default into the user's namespace. This program explicitly imports it.

  2. The user is asked for input.

  3. The subroutine try is called and passed the scalar $grade.

  4. The if subroutine try is defined.

  5. The argument passed in is assigned to $number.

  6. The cluck function is called with an error message. The cluck function was requested and imported from the Carp module. The program will send a warning if the value of $number is not in the range between 0 and 100. Now if you try to use any of the other Carp functions, the program will die. You will have to explicitly import any of those functions or use the fully qualified name; e.g., Carp::confess.

12.2.6. Using Perl to Create Your Own Module

The following example illustrates how to create a module in a separate .pm file and use the module in another program. Although this module itself looks like any other package, it must additionally include the Exporter module, the @ISA array, and the @EXPORT array for it to really behave as a module. The ability to export specific symbols to other programs and to import other modules is what differentiates a module from a .pl file. To see a skeletal module for creating modules for CPAN, see "Creating Extensions and Modules for CPAN with the h2xs Tool" on page 395.

Example 12.13.

(The Me.pm Module)
1   package Me;
2   use strict;  use warnings;
3   require 5.6;      # Make sure we're a version of Perl no
                      # older than 5.6
4   require Exporter; # Exporter.pm allows symbols to be imported
                      # by others
5   our @ISA=qw(Exporter); # ISA is a list of base packages needed
                           # by this module
6   our @EXPORT_OK=qw(hello goodbye );  # List of your subroutines
                                        # to export
7   sub hello { my($name)=shift;
        print "Hi there, $name.\n" };
8   sub goodbye { my($name)=shift;
        print "Good-bye $name.\n";}
9   sub do_nothing { print "Didn't print anything.
                            Not in EXPORT list\n";}
    1;

–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
    #!/usr/bin/perl
    # Program name: main.perl
10  use lib ("/home/ellie/Modules");   # A pragma to update @INC.
11  use Me  qw(hello goodbye);         # Import package
12  &hello ("Daniel");
13  &goodbye ("Steve");
14  &do_nothing;       # This was not on the Export list
                       # in Me.pm so cannot be imported unless
                       # explicitly with &Me::do_nothing
(Output)
12  Hi there, Daniel.
13  Good-bye Steve.
14  Undefined subroutine &main::do_nothing

					  

Explanation

  1. The file is called Me.pm. It contains a package of the same name without the extension. The Me package is declared.

  2. The strict pragma bars global variables, and the warnings pragma issues the appropriate warning messages if there are variables used only once, undefined values, etc.

  3. The require is used to make sure the Perl version being used is not less than 5.6. If it is, the script will abort.

  4. The Exporter module is a special Perl module that allows the use function to import subroutines (called "methods") from a particular module.

  5. The @ISA array lists any packages containing subroutines (methods) that will be used by this package. This is how Perl implements inheritance from one module to another. (See Chapter 14, "Bless Those Things! (Object-Oriented Perl).")

  6. The @EXPORT array lists all subroutines (methods) that can be exported by default. The @EXPORT_OK array lists subroutines (methods) that can be exported if the user of the module requests them in his use statement. If he doesn't ask, he won't get them. Those subroutines on the export list are hello and goodbye, defined below.

  7. The subroutine hello is defined.

  8. The subroutine goodbye is defined.

  9. The subroutine do_nothing is defined. Note: the name of this subroutine is not on the export list; that is, it is not in the @EXPORT_OK arrray. If not on the export list, Perl will look in the current package, main, for it.

  10. The lib pragma tells the compiler to update the @INC array at compile time. It is the same as saying: BEGIN{ require "/home/ellie/Module"; import Module;}.

  11. The use function causes Perl to include the Me.pm module into this package.

  12. The subroutine hello is called with an argument. This subroutine name was imported.

  13. The subroutine goodbye is called with an argument. It was also imported.

  14. This subroutine was not imported. It was not on the export list (@EXPORT) in Me.pm. An error message is printed indicating that package main does not recognize this subroutine. If the explicit package name, &Me::goodbye, is given, the subroutine can be used from package Me.

Previous Page Next Page