Tải bản đầy đủ (.pdf) (72 trang)

Perl in a Nutshell phần 6 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.46 MB, 72 trang )

Chapter 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.
8.63 ExtUtils::Packlist 8.65 Fatal
[ 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
[2/7/2001 10:32:37 PM]
Chapter 8
Standard Modules

8.65 Fatal
Provides a way to replace functions that return false on failure with equivalents that die instead, so you
can use the functions without having to test the return values explicitly. Fatal reports errors via die; you
can trap them with $SIG{__DIE__} if you want to take some action before the program exits. You can
use Fatal for both user-defined functions and Perl core operators except exec and system.
import●
8.64 ExtUtils::testlib 8.66 Fcntl
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.65 Fatal
[2/7/2001 10:32:38 PM]
Chapter 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 the
following routines by default; each routine returns the value of the #define that is the same as the
routine 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
8.65 Fatal 8.67 fields
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.66 Fcntl
[2/7/2001 10:32:42 PM]
Chapter 8
Standard Modules

8.67 fields
Pragma that provides the ability to define class fields at compile time, by updating the %FIELDS hash in
the calling package. The %FIELDS hash is set up at compile time as well; use the base pragma to copy
fields 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.
8.66 Fcntl 8.68 File::Basename
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.67 fields
[2/7/2001 10:32:44 PM]
Chapter 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 the
file'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:
basename●
dirname●
fileparse●
fileparse_set_fstype●
8.67 fields 8.69 File::CheckTree
[ 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
[2/7/2001 10:32:45 PM]
Chapter 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 followed
with || 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 group
produces a warning. For example:
use File::CheckTree;
$warnings += validate( q{
/vmunix -e || die
/bin cd

csh !-ug
sh -ex
/usr -d || warn "What happened to $file?\n"
});
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.
8.68 File::Basename 8.70 File::Compare
[ 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
[2/7/2001 10:32:46 PM]
Chapter 8
Standard Modules

8.70 File::Compare
Compares the contents of two sources, each of which can be a file or a filehandle. Returns 0 if the
sources are equal, 1 if they are unequal, and -1 on error. File::Compare provides two functions:
compare●
cmp●
8.69 File::CheckTree 8.71 File::Copy
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.70 File::Compare
[2/7/2001 10:32:48 PM]
Chapter 8
Standard Modules

8.71 File::Copy
Copies or moves files or filehandles from one location to another. Returns 1 on success, 0 on failure, or
sets $! on error.

copy●
cp●
move●
mv●
8.70 File::Compare 8.72 File::DosGlob
[ 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
[2/7/2001 10:32:49 PM]
Chapter 8
Standard Modules

8.72 File::DosGlob
Provides a portable enhanced DOS-like globbing for the standard Perl distribution. DosGlob lets you use
wildcards 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.004
use 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?>"

With the perlglob.bat program on the DOS command line:
% perlglob /pe*/*p?

When invoked as a program from the command line, File::DosGlob prints null-separated filenames to

STDOUT.
8.71 File::Copy 8.73 File::Find
[ 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
[2/7/2001 10:32:50 PM]
Chapter 8
Standard Modules

8.73 File::Find
Looks for files that match a particular expression. Exports two functions:
find●
finddepth●
8.72 File::DosGlob 8.74 File::Path
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.73 File::Find
[2/7/2001 10:32:51 PM]
Chapter 8
Standard Modules

8.74 File::Path
Creates and deletes multiple directories with specified permissions. Exports two methods:
mkpath●
rmtree●
8.73 File::Find 8.75 File::Spec
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.74 File::Path
[2/7/2001 10:32:58 PM]

Chapter 8
Standard Modules

8.75 File::Spec
Performs common operations on file specifications in a portable way. To do that, it automatically loads
the 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.
8.74 File::Path 8.76 File::Spec::Mac
[ 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
[2/7/2001 10:32:59 PM]
Chapter 8
Standard Modules

8.76 File::Spec::Mac
File::Spec for MacOS.
canonpath●
catdir●
catfile●
curdir●
file_name_is_absolute●
path●
rootdir●
updir●
8.75 File::Spec 8.77 File::Spec::OS2
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]

[Chapter 8] 8.76 File::Spec::Mac
[2/7/2001 10:33:00 PM]
Chapter 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.
8.76 File::Spec::Mac 8.78 File::Spec::Unix
[ 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
[2/7/2001 10:33:02 PM]
Chapter 8
Standard Modules

8.78 File::Spec::Unix
File::Spec for Unix. Provides methods for File::Spec to use in manipulating file specifications.
canonpath●
catdir●
catfile●
curdir●
file_name_is_absolute●
join●
no_upwards●
path●
rootdir●
updir●
8.77 File::Spec::OS2 8.79 File::Spec::VMS
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]

[Chapter 8] 8.78 File::Spec::Unix
[2/7/2001 10:33:04 PM]
Chapter 8
Standard Modules

8.79 File::Spec::VMS
File::Spec for VMS. Overrides the implementation of the File::Spec::Unix methods, but not the interface.
catdir●
catfile●
curdir●
file_name_is_absolute●
path●
rootdir●
updir●
8.78 File::Spec::Unix 8.80 File::Spec::Win32
[ 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
[2/7/2001 10:33:05 PM]
Chapter 8
Standard Modules

8.80 File::Spec::Win32
File::Spec for Win32. Overrides the implementation of the File::Spec::Unix methods, but not the
interface.
canonpath●
catfile●
8.79 File::Spec::VMS 8.81 File::stat
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]

[Chapter 8] 8.80 File::Spec::Win32
[2/7/2001 10:33:06 PM]
Chapter 8
Standard Modules

8.81 File::stat
Provides the same file status information as the Perl functions stat and lstat. Exports two functions
that return File::stat objects. The objects have methods that return the equivalent fields from the Unix
stat(2) call:
Field Meaning
dev
Device number of filesystem
ino
Inode number
mode
File mode
nlink
Number of links to the file
uid
Numeric user ID of owner
gid
Numeric group ID of owner
rdev
Device identifier
size
Size of file, in bytes
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 namespace
with 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:
use File::stat;
$stats = stat($file);
print $stats->uid;
print $st_uid;
[Chapter 8] 8.81 File::stat
(1 of 2) [2/7/2001 10:33:08 PM]
stat●
lstat●
8.80 File::Spec::Win32 8.82 FileCache
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.81 File::stat
(2 of 2) [2/7/2001 10:33:08 PM]
Chapter 8
Standard Modules

8.82 FileCache
Closes and re-opens files as necessary so you can always write to a file even if you already have the
maximum number of files open. Exports one function:
cacheout●
8.81 File::stat 8.83 FileHandle

[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.82 FileCache
[2/7/2001 10:33:09 PM]
Chapter 8
Standard Modules

8.83 FileHandle
Provides object methods for working with filehandles. Provides the following methods:
new●
new_from_fd●
fdopen●
getpos●
open●
setpos●
setvbuf●
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):
clearerr getc
close gets
eof seek
fileno tell
The next group of FileHandle methods correspond to Perl special variables (see Programming Perl or the
perlvar manpage):
autoflush format_page_number
format_formfeed format_top_name
format_line_break_characters input_line_number
format_lines_left input_record_separator
format_lines_per_page output_field_separator
format_name output_record_separator

[Chapter 8] 8.83 FileHandle
(1 of 2) [2/7/2001 10:33:11 PM]
Finally, the following methods are useful:
$fh->print
See Perl's built-in print function.
$fh->printf
See Perl's built-in printf function.
$fh->getline
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 remaining
lines in a file.
8.82 FileCache 8.84 FindBin
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.83 FileHandle
(2 of 2) [2/7/2001 10:33:11 PM]
Chapter 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:

$Bin
Path to bin directory from which script was invoked.
$Script
Basename of script from which Perl was invoked.
$RealBin
$Bin with all links resolved.
$RealScript
$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.
8.83 FileHandle 8.85 GDBM_File
[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl
Programming | Perl Cookbook ]
[Chapter 8] 8.84 FindBin
[2/7/2001 10:33:13 PM]
Chapter 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;
8.84 FindBin 8.86 Getopt::Long
[ 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

[2/7/2001 10:33:14 PM]
Chapter 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 (=):
foo=bar
foo bar
Provides two functions: GetOptions and config.
GetOptions●
config●
8.85 GDBM_File 8.87 Getopt::Std
[ 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
[2/7/2001 10:33:15 PM]

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×