5.2. Perl Functions in Alphabetical Order
Returns information
about the stack of current subroutine calls. Without an argument, it
returns the package name in a scalar context; in a list context, it
returns the package name, filename, and line number from which the
currently executing subroutine was called:
($package, $filename, $line) = caller;
With an argument it evaluates n as the
number of stack frames to go back before the current one. It also
reports some additional information that the debugger uses to print a
stack trace:
$i = 0;
while (($pack, $file, $line, $subname, $hasargs,
$wantarray, $evaltext, $is_require) = caller($i++)) {
...
}
Furthermore, when called from within the DB package,
caller returns more detailed information: it sets
the list variable @DB::args as the argument passed
in the given stack frame.
dbmopen %hash, dbname, mode
| | Binds a DBM file
(dbname) to a hash
(%hash). dbnameis the name of the database without the .dir or
.pag extension. Note that while not deprecated,
dbmopen has basically been superseded by
tie( ). If the database does not exist, and a
valid mode is specified, the database is created with the permissions
specified by mode (as modified by the
umask). To prevent creation of the database if it
doesn't exist, you may specify a
mode of undef, and the
function will return a false value if it can't find
an existing database. If your system supports only the older DBM
functions, you may have only one dbmopen in your
program.
Values assigned to the hash prior to the dbmopen
are not accessible. If you don't have write access
to the DBM file, you can only read the hash variables, not set them.
This function is actually just a call to tie with
the proper arguments, but is provided for backward compatibility with
older versions of Perl.
Returns a Boolean
value saying whether the scalar value resulting from
expr has a real value. If no argument is
given, defined checks $_.
A scalar that contains no valid string, numeric, or reference value
is known as the undefined value, or undef. Many
operations return the undefined value under exceptional conditions,
such as end-of-file, uninitialized variable, system error, and so on.
This function allows you to distinguish between an undefined null
string and a defined null string when you're using
operators that might return a real null string.
You can use defined to see if a subroutine exists,
that is, if the subroutine definition has been successfully parsed.
However, using defined on an array or a hash is
not guaranteed to produce intuitive results and should be avoided.
delete $hash{key}
delete @hash{@keys}
| | Deletes the specified
key or keys and
associated values from the specified hash.
(It doesn't delete a file. See unlink for that.) Deleting from
$ENV{} modifies the environment. Deleting from a
hash that is bound to a (writable) DBM file deletes the entry from
the DBM file.
For normal hashes, the delete function returns the
value (not the key) that was deleted, but this behavior is not
guaranteed for tied hashes, such as those bound to DBM files. To test
whether a hash element has been deleted, use
exists.
Evaluates the
expression or code in its argument at runtime as a separate Perl
program within the context of the larger script. Any variable
settings remain afterward, as do any subroutine or format
definitions. The code of the eval is treated as a
block, so any locally scoped variables declared within the
eval last only until the eval
is done. (See also local and
my.) The value returned from an
eval is the value of the last expression
evaluated. Like subroutines, you may also use the
return function to return a value and exit the
eval.
With eval string, the
contents of string are compiled and
executed at runtime. For example:
$a = 3, $b = 4;
$c = '$a * $b';
print (eval "$c"); # Prints 12
The string form of eval is useful for executing
strings produced at runtime from standard or other dynamic input
sources. If the string produces an error, either from syntax or at
runtime, the eval exits with the undefined value
and places the error in $@. If
string is omitted, the operator evaluates
$_.
The block form of eval is used in Perl programs to
handle runtime errors (exceptions). The code in
block is compiled only once during the
compilation of the main program. If there is a syntax error in the
block, it will produce an error at compile time. If the code in
block produces a runtime error (or if a
die statement is encountered), the
eval exits, and the error is placed in
$@. For example, the following code can be used to
trap a divide-by-zero error at runtime:
eval {
$a = 10; $b = 0;
$c = $a / $b; # Causes runtime error
# Trapped by eval
};
print $@; # Prints "Illegal division by 0 at
# try.pl line 3"
As with any code in a block, a final semicolon is not required.
formline picture, variables
| | Internal function
used by formats, although you may also call it. It formats a list of
values (variables) according to the
contents of picture, placing the output
into the format output accumulator, $^A. When a
write is done, the contents of
$^A are written to a filehandle, but you can also
read $^A yourself and set $^A
back to "". Note that a format
typically does one formline per line of form, but
the formline function itself
doesn't care how many newlines are embedded in the
picture. This means that the
~ and ~~ tokens will treat the
entire picture as a single line. Thus, you
may need to use multiple formlines to implement a single-record
format, such as the format compiler.
Be careful if you put double quotes around the picture, since an
@ character may be taken to mean the beginning of
an array name. formline always returns true. See
Chapter 4, "The Perl Language" for more information.
gethostbyaddr address, [addrtype]
| | Retrieves
the hostname (and alternate addresses) of a packed binary network
address.
(addrtype indicates the type of address
given. Since gethostbyaddr is used almost solely
for Internet IP addresses, addrtype is not
needed.) The return value in list context is:
($name, $aliases, $addrtype, $length, @addrs)
in which @addrs is a list of packed binary
addresses. In the Internet domain, each address is four bytes long
and can be unpacked by something like:
($a, $b, $c, $d) = unpack('C4', $addrs[0]);
In scalar context, gethostbyaddr returns only the
hostname.
Retrieves the
next line from the /etc/passwd file (or its
equivalent coming from another server). Returns null at the end of
the file. The return value in list context is:
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)
Some machines may use the quota and comment fields for other
purposes, but the remaining fields will always be the same. To set up
a hash for translating login names to uids, do this:
while (($name, $passwd, $uid) = getpwent) {
$uid{$name} = $uid;
}
In scalar context, getpwent returns only the
username.
Retrieves the
passwd file entry of a user
name. The return value in list context is:
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)
If you want to do this repeatedly, consider caching the data in a
hash using getpwent.
In scalar context, getpwnam returns only the
numeric user ID.
Retrieves the
passwd file entry with the user ID
uid. The return value in list context is:
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell)
If you want to do this repeatedly, consider slurping the data into a
hash using getpwent.
In scalar context, getpwuid returns the username.
Converts a time
string as returned by the time function to a
nine-element list with the time correct for Greenwich Mean Time zone
(a.k.a. GMT, UTC, etc.). Typically used as follows:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
gmtime(time);
All list elements are numeric and come straight out of a C language
struct tm. In particular, this means that
$mon has the range 0..11,
$wday has the range 0..6, and
the year has had 1,900 subtracted from it. (You can remember which
elements are 0-based because
you're always using these as subscripts into
0-based arrays containing month and day names.) If
expr is omitted, it does
gmtime(time). For example, to print the current
month in London:
$london_month = (qw(Jan Feb Mar Apr May Jun
Jul Aug Sep Oct Nov Dec))[(gmtime)[4]];
The Perl library module Time::Local contains a subroutine,
timegm( ), that can convert in the opposite
direction.
In scalar context, gmtime returns a
ctime(3)-like string based on the GMT time value.
Declares one or more
global variables vars to have temporary
values within the innermost enclosing block, subroutine,
eval, or file. The new value is initially
undef for scalars and ( ) for
arrays and hashes. If more than one variable is listed, the list must
be placed in parentheses, because the operator binds more tightly
than a comma. All the listed variables must be legal lvalues, that
is, something you can assign to. This operator works by saving the
current values of those variables on a hidden stack and restoring
them upon exiting the block, subroutine, eval, or
file.
Subroutines called within the scope of a local variable will see the
localized inner value of the variable. The technical term for this
process is "dynamic scoping." Use
my for true private variables.
Converts the
value returned by time to a nine-element list with
the time corrected for the local time zone. It's
typically used as follows:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
All list elements are numeric. The element $mon
(month) has the range 0..11, and
$wday (weekday) has the range
0..6. The year has had 1,900 subtracted from it.
(You can remember which elements are 0-based
because you're always using them as subscripts into
0-based arrays containing month and day names.) If
val is omitted, it does
localtime(time). For example, to get the name of
the current day of the week:
$thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime)[6]];
The Perl library module Time::Local contains a subroutine,
timelocal( ), that can convert in the opposite
direction.
In scalar context, localtime returns a
ctime(3)-like string based on the localtime value.
Declares one or more
private variables to exist only within the innermost enclosing block,
subroutine, eval, or file. The new value is
initially undef for scalars and (
) for arrays and hashes. If more than one variable is
listed, the list must be placed in parentheses, because the operator
binds more tightly than a comma. Only simple scalars or complete
arrays and hashes may be declared this way. The variable name may not
be package-qualified, because package variables are all global, and
private variables are not related to any package.
Unlike local, this operator has nothing to do with
global variables, other than hiding any other variable of the same
name from view within its scope. (A global variable can always be
accessed through its package-qualified form or a symbolic reference,
however.) A private variable is not visible until the statement after
its declaration. Subroutines called from within the scope of such a
private variable cannot see the private variable unless the
subroutine is also textually declared within the scope of the
variable.
open filehandle, filename
open filehandle, mode, filename
open filehandle, mode, expr, list (new in 5.8)
open filehandle, mode, reference (new in 5.8)
| | Opens
the file given by filename and associates
it with filehandle. If
filehandle is omitted, the scalar variable
of the same name as the filehandle must
contain the filename. (And you must also be careful to use
or die after the statement rather than
|| die, because the precedence
of || is higher than list operators such as
open.)
If filename is preceded by either
< or nothing, the file is opened for input
(read-only). If filename is preceded by
>, the file is opened for output. If the file
doesn't exist, it will be created; if the file
exists, it will be overwritten with output using
>. Preceding the filename with
>> opens an output file for appending. For
both read and write access, use a + before
< or >.
If you choose to use the three-or-more-arguments form of
open, you can use separate
mode and
filename arguments, such as
open($fh,
$mode,
$filename),
in which $moderepresents an open mode or pipe. For example:
my $mode = '+<';
my $filename = 'whatever.txt';
open(FH, $mode, $filename)
or die("can't open $filename: $!");
As covered in Chapter 4, "The Perl Language", you can build Perl 5.8
and newer with PerlIO support, which offers additional features for
your system's I/O (STDIO). This allows you to do
neat things, such as specify utf-8 as your default
encoding for all of your I/O, or set your default line endings with
'crlf'. In addition, you can select piping to or
extracting information from an external program with
'|-' and '-|', respectively.
For example:
my $prog = "webster overworked";
open($fh, '-|', $prog)
or die("can't open pipe to '$prog': $!");
or, similarly: my @prog_info = qw(/usr/contrib/bin/webster overworked);
open($fh, '-|', @prog_info)
or die(...);
A filehandle may also be attached to a process by using a piped
command. If the filename begins with |, the
filename is interpreted as a command to which output is to be piped.
If the filename ends with a |, the filename is
interpreted as a command that pipes input to you. You may not have an
open command that pipes both in and out.
Any pipe command containing shell metacharacters is passed to the
shell for execution; otherwise, it is executed directly by Perl. The
filename - refers to STDIN, and
> refers to STDOUT. open
returns nonzero upon success, the undefined value otherwise. If the
open involved a pipe, the return value happens to
be the process ID of the subprocess.
Takes a
list of values and packs it into a binary structure, returning the
string containing the structure. templateis a sequence of characters that give the order and type of values,
as follows:
Character
|
Meaning
|
@
|
Null-fill to absolute position.
|
(
|
Start of a ( ) group.
|
a
|
An ASCII string, will be null padded.
|
A
|
An ASCII string, will be space padded.
|
b
|
A bit string, low-to-high order (such as vec( )).
|
B
|
A bit string, high-to-low order.
|
c
|
A signed char value.
|
C
|
An unsigned char value.
|
d
|
A double-precision float in the native format.
|
D
|
A long double-precision float in the native format. Long doubles are
avai able only if your system supports long double values and if Perl
has been compiled to support these values. Causes a fatal error
otherwise. New in 5.8.
|
f
|
A single-precision float in the native format.
|
F
|
A floating-point value in the native format, i.e., a Perl internal
floating-point value (NV). New in 5.8.
|
h
|
A hexadecimal string, low nybble first.
|
H
|
A hexadecimal string, high nybble first.
|
i
|
A signed integer value.
|
I
|
An unsigned integer value.
|
l
|
A signed long value.
|
j
|
A signed integer value, i.e., a Perl internal integer (IV). New in
5.8.
|
J
|
An unsigned integer value, i.e., a Perl internal unsigned integer
(UV). New in 5.8.
|
L
|
An unsigned long value.
|
n
|
A short in "network" (big-endian)
order.
|
N
|
A long in "network" (big-endian)
order.
|
p
|
A pointer to a string.
|
P
|
A pointer to a structure (fixed-length string).
|
q
|
A signed quad (64-bit) value. New in 5.8.
|
Q
|
An unsigned quad value. Quads are available only if your system
supports 64-bit integer values and if Perl has been compiled to
support these values. Causes a fatal error otherwise. New in 5.8.
|
s
|
A signed short value.
|
S
|
An unsigned short value.
|
v
|
A short in "VAX" (little-endian)
order.
|
V
|
A long in "VAX" (little-endian)
order.
|
u
|
A uuencoded string.
|
U
|
A Unicode character number. Encodes to UTF-8 internally (or
UTF-EBCDIC in EBCDIC platforms). New in 5.8.
|
w
|
A BER compressed integer.
|
x
|
A null byte.
|
X
|
Back up a byte.
|
Z
|
A null terminated (ASCII) string. Will be null padded. New in 5.8.
|
Each character may optionally be followed by a number that gives a
repeat count. Together the character and the repeat count make a
field specifier. Field specifiers may be separated by whitespace,
which will be ignored. With all types except a and
A, the pack function will
gobble up that many values from the list.
Specifying * for the repeat count means to use
however many items are left. The a and
A types gobble just one value, but pack it as a
string of length count, padding with nulls
or spaces as necessary. (When unpacking, A strips
trailing spaces and nulls, but a does not.) Real
numbers (floats and doubles) are in the native machine format only;
due to the multiplicity of floating formats, and the lack of a
standard network representation, no facility for interchange has been
made.
Generally, the same template may also be used in the
unpack function. If you want to join variable
length fields with a delimiter, use the join
function.
Declares that the
rest of the innermost enclosing block, subroutine,
eval, or file belongs to the indicated
namespace. (The scope of a
package declaration is thus the same as the scope
of a local or my declaration.)
All subsequent references to unqualified global identifiers will be
resolved by looking them up in the declared packages symbol table. A
package declaration affects only global
variables—including those you've used
local on—but not lexical variables created
with my.
Using package without an argument is possible, but
since its semantics are unclear, package; has been
depracated in Perl 5.8. If you intend to disallow variables that
aren't fully qualified, use
strict; instead.
Typically, you put a package declaration as the
first thing in a file that will be included by the
require or use operator, but
you can put one anywhere that a statement would be legal. When
defining a class or a module file, it is customary to name the
package the same name as the file, to avoid confusion.
(It's also customary to name such packages beginning
with a capital letter, because lowercase modules are, by convention,
interpreted as pragmas.)
printf [filehandle] format, list
| | Prints a formatted
string of the elements in list to
filehandle or, if omitted, the currently
selected output filehandle. This is similar to the C
library's printf and
fprintf functions, except that the
* field width specifier is not supported. The
function is exactly equivalent to:
print filehandle sprintf(format, list);
printf and sprintf use the same
format syntax, but sprintf returns only a string;
it doesn't print to a filehandle. The
format string contains text with embedded
field specifiers into which the elements of
list are substituted in order, one per
field. Field specifiers follow the form:
%m.nx
A percent sign begins each field, and x is
the type of field. The optional m gives
the minimum field width for appropriate field types (negative
m left-justifies). The
.n gives the precision for a specific
field type, such as the number of digits after a decimal point for
floating-point numbers, the maximum length for a string, and the
minimum length for an integer.
Field specifiers (x) may be the following:
Code
|
Meaning
|
%
|
Percent sign
|
c
|
Character
|
d
|
Decimal integer
|
e
|
Exponential format floating-point number
|
E
|
Exponential format floating-point number with uppercase E
|
f
|
Fixed-point format floating-point number
|
g
|
Floating-point number, in either exponential or fixed decimal notation
|
G
|
Like g with uppercase E (if applicable)
|
ld
|
Long decimal integer
|
lo
|
Long octal integer
|
lu
|
Long unsigned decimal integer
|
lx
|
Long hexadecimal integer
|
o
|
Octal integer
|
s
|
String
|
u
|
Unsigned decimal integer
|
x
|
Hexadecimal integer
|
X
|
Hexadecimal integer with uppercase letters
|
p
|
The Perl value's address in hexadecimal
|
n
|
Special value that stores the number of characters output so far into
the next variable in the parameter list
|
read filehandle, $var, length, [offset]
| | Attempts to read
length bytes of data into variable
var from the specified
filehandle. The function returns the
number of bytes actually read, or 0 at
end-of-file. It returns the undefined value on error.
var will grow or shrink to the length
actually read. The offset, if specified,
says where in the variable to start putting bytes, so that you can do
a read into the middle of a string.
To copy data from the filehandle FROM into the filehandle TO, you
could say:
while (read FROM, $buf, 16384) {
print TO $buf;
}
Note that the opposite of read is simply
print, which already knows the length of the
string you want to write and can write a string of any length.
Perl's read function is actually
implemented in terms of standard I/O's
fread function, so the actual
read system call may read more than
length bytes to fill the input buffer, and
fread may do more than one system
read to fill the buffer. To gain greater control,
specify the real system call using sysread.
require filename
require num
require package
| | Asserts a dependency
of some kind depending on its argument. (If an argument is not
supplied, $_ is used.)
If the argument is a string filename, this
function includes and executes the Perl code found in the separate
file of that name. This is similar to performing an
eval on the contents of the file, except that
require checks to see that the library file has
not been included already. The function also knows how to search the
include path stored in the @INC array.
If requires argument is a number
num, the version number of the currently
executing Perl binary (as known by $]) is compared
to num. If it is smaller, execution is
immediately aborted. Thus, a script that requires Perl version 5.003
can have as its first line:
require 5.003;
and earlier versions of Perl will abort. If require's argument is a
package name, require assumes an automatic
.pm suffix, making it easy to load standard
modules. This is like use, except that it happens
at runtime, not compile time, and the import
routine is not called.
seek filehandle, offset, whence
| | Positions the file pointer
for filehandle, just like the
fseek(3) call of standard I/O. The first position
in a file is at offset 0, not offset
1, and offsets refer to byte positions, not line
numbers. The function returns 1 upon success,
0 otherwise. For handiness, the function can
calculate offsets from various file positions for you. The value of
whence specifies which of three file
positions your offset is relative to:
0, the beginning of the file;
1, the current position in the file; or
2, the end of the file.
offset may be negative for a
whence of 1 or
2.
select rbits, wbits, ebits, timeout
| |
The four-argument select operator is totally
unrelated to the previously described select
operator. This operator is for discovering which (if any) of your
file descriptors are ready to do input or output, or to report an
exceptional condition. It calls the select(2)
system call with the bitmasks you've specified,
which you can construct using fileno and
vec, like this:
$rbits = $wbits = $ebits = "";
vec($rbits, fileno(STDIN), 1) = 1;
vec($wbits, fileno(STDOUT), 1) = 1;
$ein = $rin | $win;
The select call blocks until one or more file
descriptors is ready for reading, writing, or reporting an error
condition. timeout is given in seconds and
tells select how long to wait.
setpriority which, who, priority
| | Sets the
current priority for a process, a process
group, or a user. which must indicate one
of these types: PRIO_PROCESS,
PRIO_PGRP, or PRIO_USER.
who, therefore, identifies the specific
process, process group, or user with its ID.
priority is an integer number that will be
added to or subtracted from the current priority; the lower the
number, the higher the priority. The interpretation of a given
priority may vary from one operating system to the next. See setpriority on your system. Invoking
setpriority will produce a fatal error if used on
a machine that doesn't implement
setpriority.
Sorts a
list and returns the sorted list value. By
default (without a code argument), it
sorts in standard string comparison order (undefined values sorting
before defined null strings, which sort before everything else).
code, if given, may be the name of a
subroutine or a code block (anonymous subroutine) that defines its
own comparison mechanism for sorting elements of
list. The routine must return to the
sort function an integer less than, equal to, or
greater than 0, depending on how the elements of the list will be
ordered. (The handy <=> and
cmp operators can be used to perform three-way
numeric and string comparisons.)
The normal calling code for subroutines is bypassed, with the
following effects: the subroutine may not be a recursive subroutine,
and the two elements to be compared are passed into the subroutine as
$a and $b, not via
@_. The variables $a and
$b are passed by reference, so
don't modify them in the subroutine.
Do not declare $a and $b as
lexical variables (with my). They are package
globals (though they're exempt from the usual
restrictions on globals when you're using
use strict). However, you do
need to make sure your sort routine is in the same
package, or else you must qualify $a and
$b with the package name of the caller.
In versions preceding 5.005, Perl's
sort is implemented in terms of
C's qsort(3) function. Some
qsort(3) versions will dump core if your sort
subroutine provides inconsistent ordering of values. As of 5.005,
however, this is no longer true.
split /pattern/, string, [limit]
| | Scans a
string for delimiters that match
pattern and splits the string into a list
of substrings, returning the resulting list value in list context, or
the count of substrings in scalar context. The delimiters are
determined by repeated pattern matching, using the regular expression
given in pattern, so the delimiters may be
of any size and need not be the same string on every match. If the
pattern doesn't match at
all, split returns the original string as a single
substring. If it matches once, you get two substrings, and so on.
If limit is specified and is not negative,
the function splits into no more than that many fields. If
limit is negative, it is treated as if an
arbitrarily large limit has been
specified. If limit is omitted, trailing
null fields are stripped from the result (which potential users of
pop would do well to remember). If
string is omitted, the function splits the
$_ string. If patternis also omitted, the function splits on whitespace,
/\s+/, after skipping any leading whitespace.
If the pattern contains parentheses, then
the substring matched by each pair of parentheses is included in the
resulting list, interspersed with the fields that are ordinarily
returned. Here's a simple case:
split /([-,])/, "1-10,20";
This produces the list value: (1, '-', 10, ',', 20)
Returns a string
formatted by the printf conventions. The
format string contains text with embedded
field specifiers into which the elements of
list are substituted, one per field. Field
specifiers are roughly of the form:
%m.nx
in which m and
n are optional sizes with interpretation
that depends on the type of field, and xis one of the following:
Code
|
Meaning
|
%
|
Percent sign
|
c
|
Character
|
d
|
Decimal integer
|
e
|
Exponential format floating-point number
|
E
|
Exponential format floating-point number with uppercase E
|
f
|
Fixed-point format floating-point number
|
g
|
Floating-point number, in either exponential or fixed decimal notation
|
G
|
Like g with uppercase E (if applicable)
|
ld
|
Long decimal integer
|
lo
|
Long octal integer
|
lu
|
Long unsigned decimal integer
|
lx
|
Long hexadecimal integer
|
o
|
Octal integer
|
s
|
String
|
u
|
Unsigned decimal integer
|
x
|
Hexadecimal integer
|
X
|
Hexadecimal integer with uppercase letters
|
p
|
The Perl value's address in hexadecimal
|
n
|
Special value that stores the number of characters output so far into
the next variable in the parameter list.
|
m is typically the minimum length of the
field (negative for left-justified), and nis precision for exponential formats and the maximum length for other
formats. Padding is typically done with spaces for strings and zeroes
for numbers. The * character as a length specifier
is not supported.
Returns
a 13-element list giving the statistics for a
file, indicated by either a filehandle or
an expression that gives its name. It's typically
used as follows:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat $filename;
Not all fields are supported on all filesystem types. Here are the
meanings of the fields:
Field
|
Meaning
|
dev
|
Device number of filesystem
|
ino
|
Inode number
|
mode
|
File mode (type and permissions)
|
nlink
|
Number of (hard) links to the file
|
uid
|
Numeric user ID of file's owner
|
gid
|
Numeric group ID of file's owner
|
rdev
|
The device identifier (special files only)
|
size
|
Total size of file, in bytes
|
atime
|
Last access time since the epoch
|
mtime
|
Last modification time since the epoch
|
ctime
|
Inode change time (not creation time!) since the epoch
|
blksize
|
Preferred blocksize for file system I/O
|
blocks
|
Actual number of blocks allocated
|
$dev and $ino, taken together,
uniquely identify a file. The $blksize and
$blocks are likely defined only on BSD-derived
filesystems. The $blocks field (if defined) is
reported in 512-byte blocks. Note that $blocks*512
can differ greatly from $size for files containing
unallocated blocks, or "holes,"
which aren't counted in $blocks.
If stat is passed the special filehandle
consisting of an underline, no actual stat is
done, but the current contents of the stat
structure from the last stat or
stat-based file test (the -x
operators) is returned.
sysopen filehandle, filename, mode, [perms]
| | Opens the file
given by filename and associates it with
filehandle. This function calls
open(2) with the parameters
filename, mode,
and perms.
The possible values and flag bits of the
mode parameter are system-dependent; they
are available via the Fcntl library module. However, for historical
reasons, some values are universal: 0 means
read-only, 1 means write-only, and
2 means read/write.
If the file named by filename does not
exist, and sysopen creates it (typically because
mode includes the
O_CREAT flag), then the value of
perms specifies the permissions of the
newly created file. If perms is omitted,
the default value is 0666, which allows read and
write for all. This default is reasonable. See
umask.
The FileHandle module provides a more object-oriented approach to
sysopen. See also open.
syswrite filehandle, scalar, length, [offset]
| | Writes
length bytes of data from variable
scalar to the specified
filehandle. The function returns the
number of bytes actually written, or the undefined value on error.
You should be prepared to handle the problems that standard I/O
normally handles for you, such as partial writes. The
offset, if specified, says where in the
string to start writing from, in case you're using
the string as a buffer, for instance, or need to recover from a
partial write.
Do not mix calls to print (or
write) and syswrite on the same
filehandle unless you really know what you're doing.
tie variable, classname, list
| | Binds a
variable to a package class,
classname, that will provide the
implementation for the variable. Any additional arguments
(list) are passed to the
"new" method of the class (meaning
TIESCALAR, TIEARRAY, or
TIEHASH). Typically, these are arguments that
might be passed to the dbm_open(3) function of C,
but this is package-dependent. The object returned by the
"new" method is also returned by
the tie function, which can be useful if you want
to access other methods in classname. (The
object can also be accessed through the tied
function.)
A class implementing a hash should provide the following methods: TIEHASH $class, LIST
DESTROY $self
FETCH $self, $key
STORE $self, $key, $value
DELETE $self, $key
EXISTS $self, $key
FIRSTKEY $self
NEXTKEY $self, $lastkey
A class implementing an ordinary array should provide the following
methods:
TIEARRAY $classname, LIST
DESTROY $self
FETCH $self, $subscript
STORE $self, $subscript, $value
A class implementing a scalar should provide the following methods: TIESCALAR $classname, LIST
DESTROY $self
FETCH $self,
STORE $self, $value
Unlike dbmopen, the tie
function will not use or
require a module for you—you need to do that
explicitly yourself.
Undefines the value
of expr, which must be an lvalue. Use only
on a scalar value, an entire array or hash, or a subroutine name
(using the & prefix). Any storage associated
with the object will be recovered for reuse (though not returned to
the system, for most versions of Unix). The undef
function will probably not do what you expect on most special
variables. The function always returns the undefined value. This is
useful because you can omit the expr, in
which case nothing gets undefined, but you still get an undefined
value that you could, for instance, return from a subroutine to
indicate an error.
You may use undef as a placeholder on the left
side of a list assignment, in which case the corresponding value from
the right side is simply discarded. Apart from that, you may not use
undef as an lvalue.
use Module list
use version
use Module version list
| | If the
first argument is a number, it is treated as a version number. If the
version of Perl is less than version, an
error message is printed and Perl exits. This provides a way to check
the Perl version at compilation time instead of waiting for runtime.
If version appears between
Module and
list, then use calls
the version method in class
Module with
version as an argument.
Otherwise, use imports some semantics into the
current package from the named Module,
generally by aliasing certain subroutine or variable names into your
package. It is exactly equivalent to the following:
BEGIN { require Module; import Module list; }
The BEGIN forces the require
and import to happen at compile time. The
require makes sure that the module is loaded into
memory if it hasn't been yet. The
import is not a built-in
function—it's just an ordinary static method
call into the package named by Module that
tells the module to import the list of features back into the current
package. The module can implement its import method any way it likes,
though most modules just choose to derive their import method via
inheritance from the Exporter class defined in the Exporter module.
If you don't want your namespace altered, explicitly
supply an empty list:
use Module ( );
This is exactly equivalent to the following: BEGIN { require Module; }
Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way. (See Chapter 8, "Standard Modules" for
descriptions of the currently implemented pragmas.) These
pseudomodules typically import semantics into the current block
scope, unlike ordinary modules, which import symbols into the current
package. (The latter are effective through the end of the file.)
There's a corresponding declaration,
no, that
"unimports" any meanings originally
imported by use but have since become less
important:
no integer;
no strict 'refs';
Returns a list
consisting of all the values of the named hash. The values are
returned in an apparently random order, but it is the same order that
the keys or each function would
produce on the same hash. To sort the hash by its values, see the
example under keys. Note that using
values on a hash bound to a very large DBM file
will produce a very large list, causing you to have a very large
process, and leaving you in a bind. You might prefer to use the
each function, which will iterate over the hash
entries one by one without reading them all into a single list.
Treats a
string as a vector of unsigned integers
and returns the value of the element specified by
offset and
bits. The function may also be assigned
to, which causes the element to be modified. The purpose of the
function is to provide compact storage of lists of small integers.
The integers may be very small—vectors can hold numbers that
are as small as one bit, resulting in a bitstring.
The offset specifies the number of
elements to skip over to find the one you want.
bits is the number of bits per element in
the vector, so each element can contain an unsigned integer in the
range
0..(2**bits)-1.
bits must be one of 1,
2, 4, 8,
16, or 32. As many elements as
possible are packed into each byte, and the ordering is such that
vec($vectorstring,0,1) is guaranteed to go into
the lowest bit of the first byte of the string. To find the position
of the byte in which an element will be placed, you have to multiply
the offset by the number of elements per
byte. When bits is 1,
there are eight elements per byte. When
bits is 2, there are
four elements per byte. When bits is
4, there are two elements (called nybbles) per
byte. And so on.
Regardless of whether your system is big-endian or little-endian,
vec($foo, 0, 8) always refers to the first byte of
string $foo. See select for
examples of bitmaps generated with vec.
Vectors created with vec can also be manipulated
with the logical operators |,
&, ^, and
~, which will assume a bit vector operation is
desired when the operands are strings. A bit vector
(bits == 1) can be
translated to or from a string of 1s and
0s by supplying a b* template
to unpack or pack. Similarly, a
vector of nybbles (bits ==
4) can be translated with an h*
template.
Writes a formatted record
(possibly multiline) to the specified
filehandle, using the format associated
with that filehandle (see Section 4.11, "Unicode"). By default,
the format for a filehandle is the one having the same name as the
filehandle.
If filehandle is unspecified, output goes
to the current default output filehandle, which starts as STDOUT but
may be changed by the select operator. If the
filehandle is an expression, then the
expression is evaluated to determine the actual
filehandle at runtime.
Note that write is not the opposite of
read. Use print for simple
string output. If you want to bypass standard I/O, see
syswrite.
 |  |  | 5. Function Reference |  | 6. Debugging |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|