Chapter 8 Standard Modules8.65 Fatal Provides a way to replace functions that return false on failure with equivalents that die instead, so youcan use the functions without having to tes
Trang 1Chapter 8 Standard Modules
8.64 ExtUtils::testlib
Used for testing a module after it's been built and before it's been installed, when you don't want to (or
can't) run make test Adding:
use ExtUtils::testlib;
to your test program causes the intermediate directories used by make to be added to @INC, allowing you
to run the test
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.64 ExtUtils::testlib
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_64.htm [2/7/2001 10:32:37 PM]
Trang 2Chapter 8 Standard Modules
8.65 Fatal
Provides a way to replace functions that return false on failure with equivalents that die instead, so youcan use the functions without having to test the return values explicitly Fatal reports errors via die; youcan trap them with $SIG{ DIE } if you want to take some action before the program exits You canuse Fatal for both user-defined functions and Perl core operators except exec and system
import
●
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.65 Fatal
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_65.htm [2/7/2001 10:32:38 PM]
Trang 3Chapter 8 Standard Modules
8.66 Fcntl
A translation of the C fcntl.h program that uses h2xs (which builds a Perl extension from a C header file)
and the C compiler to translate #define symbols and make them available as functions Exports thefollowing routines by default; each routine returns the value of the #define that is the same as theroutine name:
FD_CLOEXEC F_DUPFD F_GETFD F_GETFL F_GETLK
F_RDLCK F_SETFD F_SETFL F_SETLK F_SETLKW
F_UNLCK F_WRLCK O_APPEND O_CREAT O_EXCL
O_NDELAY O_NOCTTY O_NONBLOCK O_RDONLY O_RDWR
O_TRUNC O_WRONLY
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.66 Fcntl
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_66.htm [2/7/2001 10:32:42 PM]
Trang 4Chapter 8 Standard Modules
8.67 fields
Pragma that provides the ability to define class fields at compile time, by updating the %FIELDS hash inthe calling package The %FIELDS hash is set up at compile time as well; use the base pragma to copyfields from base classes, and then fields to add new fields Lets you have objects with named fields
that are as compact and fast as arrays to access For more details, see the fields manpage and the
pseudo-hash section of the perlref manpage.
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.67 fields
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_67.htm [2/7/2001 10:32:44 PM]
Trang 5Chapter 8 Standard Modules
8.68 File::Basename
Parses a file's path, returning its three component pieces: the path to the file, the file's basename, and thefile's extension For example, for:
/usr/local/lib/perl5/SelectSaver.pm
the path is /usr/local/lib/perl5, the basename is SelectSaver, and the extension is pm File::Basename
exports the following functions:
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.68 File::Basename
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_68.htm [2/7/2001 10:32:45 PM]
Trang 6Chapter 8 Standard Modules
8.69 File::CheckTree
Runs file tests on a set of files Exports one function, validate, which takes a single multi-line string
as input Each line of the string contains a filename plus a test to run on the file The test can be followedwith || die to make it a fatal error if it fails The default is || warn Prepending ! to the test
reverses the sense of the test You can group tests (e.g., -rwx); only the first failed test of the groupproduces a warning For example:
Available tests include all the standard Perl file-test operators except -t, -M, -A, and -C Unless it dies,
validate returns the number of warnings issued
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.69 File::CheckTree
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_69.htm [2/7/2001 10:32:46 PM]
Trang 7Chapter 8 Standard Modules
Trang 8Chapter 8 Standard Modules
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.71 File::Copy
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_71.htm [2/7/2001 10:32:49 PM]
Trang 9Chapter 8 Standard Modules
8.72 File::DosGlob
Provides a portable enhanced DOS-like globbing for the standard Perl distribution DosGlob lets you usewildcards in directory paths, is case-insensitive, and accepts both backslashes and forward slashes
(although you may have to double the backslashes) Can be run three ways:
From a Perl script:
require 5.004use File::DosGlob 'glob';
@perlfiles = glob " \pe?l/*.p?";
print < \pe?l/*.p?>;
●
With the perl command, on the command line:
# from the command line (overrides only in main::)
% perl -MFile::DosGlob=glob -e "print < /pe*/*p?>"
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.72 File::DosGlob
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_72.htm [2/7/2001 10:32:50 PM]
Trang 10Chapter 8 Standard Modules
Trang 11Chapter 8 Standard Modules
Trang 12Chapter 8 Standard Modules
8.75 File::Spec
Performs common operations on file specifications in a portable way To do that, it automatically loadsthe appropriate operating-system-specific module, which is one of File::Spec::Mac, File::Spec::OS2,File::Spec::Unix, File::Spec::VMS, or File::Spec::Win32 The complete reference of available functions
is given in File::Spec::Unix; the functions are inherited by the other modules and overridden as
necessary Subroutines should be called as class methods, rather than directly
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.75 File::Spec
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_75.htm [2/7/2001 10:32:59 PM]
Trang 13Chapter 8 Standard Modules
Trang 14Chapter 8 Standard Modules
8.77 File::Spec::OS2
File::Spec for OS/2 Overrides the implementation of the File::Spec::Unix methods, but not the interface
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.77 File::Spec::OS2
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_77.htm [2/7/2001 10:33:02 PM]
Trang 15Chapter 8 Standard Modules
Trang 16Chapter 8 Standard Modules
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.79 File::Spec::VMS
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_79.htm [2/7/2001 10:33:05 PM]
Trang 17Chapter 8 Standard Modules
Trang 18Chapter 8 Standard Modules
nlink Number of links to the file
atime Last access time
mtime Last modified time
ctime Inode change time
blksize Preferred blocksize for filesystem I/O
blocks Number of blocks allocated
You can access the status fields either with the methods or by importing the fields into your namespacewith the :FIELDS import tag and then accessing them by prepending st_ to the field name (e.g.,
$st_mode) Here are examples of doing it both ways:
Trang 20Chapter 8 Standard Modules
Trang 21Chapter 8 Standard Modules
The following additional FileHandle methods act as front-ends for the corresponding built-in Perl
functions (see the O'Reilly book Programming Perl or the perlfunc manpage for more detail):
Trang 22Finally, the following methods are useful:
Works like Perl's <FILEHANDLE> construct, except that it can be safely called in an array
context (but it still returns just one line)
$fh->getlines
Works like Perl's <FILEHANDLE> construct when called in an array context to read all remaininglines in a file
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.83 FileHandle
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_83.htm (2 of 2) [2/7/2001 10:33:11 PM]
Trang 23Chapter 8 Standard Modules
8.84 FindBin
Finds the full path to a script's bin directory, letting you use relative paths to that directory without
needing to know the actual location:
use FindBin;
use lib "$FindBin::Bin/ /lib";
or:
use FindBin qw($Bin);
use lib "$Bin/ /lib";
FindBin exports the following variables:
$Script with all links resolved
If you invoke Perl with the -e option or read the Perl script from STDIN, then FindBin sets both $Bin
and $RealBin to the current directory
Trang 24Chapter 8 Standard Modules
8.85 GDBM_File
Allows Perl programs to make use of the facilities provided by the GNU gdbm library Most of the
libgdbm.a functions are available as methods of the GDBM_File interface See the gdbm(3) manpage and
the description of DB_File in this chapter The following is an example of the use of GDBM_File:
use GDBM_File;
tie %hash, "GDBM_File", $filename, &GDBM_WRCREAT, 0644);
# read/writes of %hash are now read/writes of $filename
untie %hash;
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.85 GDBM_File
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_85.htm [2/7/2001 10:33:14 PM]
Trang 25Chapter 8 Standard Modules
8.86 Getopt::Long
Lets your program accept command-line options with long names, introduced by Standard
single-character options are also accepted Options that start with may have an argument appended,following a space or an equals sign (=):
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.86 Getopt::Long
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_86.htm [2/7/2001 10:33:15 PM]
Trang 26Chapter 8 Standard Modules
8.87 Getopt::Std
Processes single-character command-line options with option clustering Exports two functions, whichanalyze @ARGV, extract information about the options, and return this information to your program in aset of variables Processing of @ARGV stops when an argument without a leading - is encountered, if thatargument is not associated with a preceding option Otherwise, @ARGV is processed to the end and leftempty
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.87 Getopt::Std
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_87.htm [2/7/2001 10:33:16 PM]
Trang 27Chapter 8 Standard Modules
8.88 I18N::Collate
The use of I18N::Collate is now deprecated, although it is still provided for compatibility with olderprograms Don't use it in new programs, as its functionality was integrated into the core Perl in Perl5.004 See the locale pragma
Trang 28Chapter 8 Standard Modules
Trang 29Chapter 8 Standard Modules
8.90 IO
Loads all of the following IO modules with a single use statement: IO::Handle, IO::Seekable, IO::File,IO::Pipe, and IO::Socket
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.90 IO
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_90.htm [2/7/2001 10:33:19 PM]
Trang 30Chapter 8 Standard Modules
Trang 31Chapter 8 Standard Modules
Method Description
close Closes file or pipe
eof Returns 1 if next read will return end-of-file
fileno Returns file descriptor for a filehandle
print Prints a string or comma-separated list of strings
printf Prints a formatted string
[Chapter 8] 8.92 IO::Handle
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_92.htm (1 of 2) [2/7/2001 10:33:22 PM]
Trang 32read Reads data from a filehandle
stat Returns an array of status information for a file
sysread Reads data from a filehandle with system call read(2)
syswrite Writes data to a filehandle with system call write(2)
truncate Truncates a file to a specified length
Finally, the following methods act on the equivalent Perl variables See Chapter 4 for more information
Method Description
autoflush If nonzero, forces a flush now and after each write or print
(default 0)
format_page_number Current page number
format_lines_per_page Current page length (default 60)
format_lines_left Number of lines left on page
format_name Name of current report format
format_top_name Name of current top-of-page format
format_line_break_characters Current set of linebreak chars for a format
format_formfeed Formfeed char used by formats (default \f)
format_write Wrapper for write function
input_record_separator Input record separator (default newline)
input_line_number Current input line number for last filehandle accessed
output_field_separator Output field separator for print
output_record_separator Output record separator for print
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.92 IO::Handle
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_92.htm (2 of 2) [2/7/2001 10:33:22 PM]
Trang 33Chapter 8 Standard Modules
Trang 34Chapter 8 Standard Modules
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.94 IO::Seekable
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_94.htm [2/7/2001 10:33:24 PM]
Trang 35Chapter 8 Standard Modules
Trang 36Chapter 8 Standard Modules
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.96 IO::Socket
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_96.htm [2/7/2001 10:33:26 PM]
Trang 37Chapter 8 Standard Modules
Trang 38Chapter 8 Standard Modules
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.98 IPC::Open2
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_98.htm [2/7/2001 10:33:28 PM]
Trang 39Chapter 8 Standard Modules
8.99 IPC::Open3
IPC::Open3 works like IPC::Open2
use IPC::Open3;
$pid = open3($wtr, $rdr, $err, $cmd_with_args);
$pid = open3($wtr, $rdr, $err, $cmd, "arg1", "arg2", );
The following differences apply:
The first two arguments ($wtr and $rdr) are passed in the opposite order
●
A third filehandle can be passed, for standard error If this argument is given as "", then STDERRand STDOUT for $cmd are on the same filehandle
●
If $wtr begins with <, then the leading < is stripped from the name and the remainder is assumed
to be a regular filehandle for an open file, rather than a reference to a typeglob open3 opens thisfile as STDIN for $cmd and closes it in the parent Likewise, if $rdr or $err begins with >,then $cmd directs STDOUT or STDERR directly to that file rather than to the parent
●
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.99 IPC::Open3
http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/perl/perlnut/ch08_99.htm [2/7/2001 10:33:29 PM]