第 15 章

函數列表


CONTENTS


Perl has a large number of functions that come as standard with most implementations, and an even wider range of additional modules, each with its own additional functions. This chapter lists all the standard functions alphabetically for reference.

Each function is assigned a category. There are two main categories; list operators, which can take more than one argument, and named unary operators, which can only take one argument. A secondary category is noted in parentheses so you can see, at a glance, the type of operation the function performs. This is a very rough categorization, as many functions might overlap in any category scheme.

For each function the form of the arguments is listed. If there are multiple forms of calling the function, there will be multiple lines describing each form. The meanings of the arguments are described in the text.

The type of value returned by the function is listed. This is usually specified in more detail in the function description.

Two categories of functions, those dealing with sockets and those dealing with System V inter-process communications, are not dealt with in great detail. Both of these categories of functions are direct counterparts of UNIX system functions.

-A

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  integer (age of file in days since last

              access relative to $BASETIME)

Definition

The file test operator takes one file handle or filename as an argument. It returns age of file in days since last access relative to $BASETIME. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


print "-A ", -A "/etc/fstab", "\n";

-B

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is binary. It returns '' (false) if the file is not binary. The first characters of the file are checked to see if the high bit is set and if a suitable number do have the high bit set the file is assumed to be binary. If the file is empty it is returned as binary. Because this test involves reading the file itself, it is best to test to learn if the file exists as a plain file (-f), first. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-B "/etc/fstab") ? print("-B fstab is binary\n") :

 

	print("-B fstab is not binary\n");

-b

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is a block special file (that is, a UNIX /dev device file). It returns '' (false) if the file is not a block special file. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-b "/dev/hda1") ? print("-b hda1 is block\n") :

 

	print("-b hda1 is not block\n");

-C

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  integer (age of file in days since last

              inode change relative to $BASETIME)

Definition

The file test operator takes one file handle or filename as an argument. It returns age of file in days since last inode change relative to $BASETIME. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


print "-C ", -C "/etc/fstab", "\n";

-c

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is a character special file. It returns '' (false) if the file is not a character special file. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is suppligª, $_ is used.

Example


(-c "/dev/tty0") ? print("-c tty0 is char\n") : 

	print("-c tty0 is not char\n");

-d

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is a directory. It returns '' (false) if the file is not a directory. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-d "/") ? print("-d / is dir\n") : print("-d / is not dir\n");

-e

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if file exists. It returns '' (false) if the file does not exist. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-e "/") ? print("-e / exists\n") : print("-e / exists\n");

-f

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is a plain file. It returns '' (false) if the file is not a plain file. A plain file is any file that is not a special block device (-b), a special character device (-c), a directory (-d), a symbolic link (-l), a pipe (-p), a named socket (-S), or a direct link to an I/O terminal (-t). All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-f "/") ? print("-f / is plain\n") : print("-f / is not plain\n");

-g

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file has the setgid bit set. It returns '' (false) if the file does not have the setgid bit set. In UNIX, setgid allows an executable to run as if it was being run by the group, which owns the executable itself while executing (for example, if a binary is owned by the group wwwstat, and the binary has the getgid bit set, then that binary has access to all files that the wwwstat group can access while the binary is running, even when the binary is run by someone who is not actually a member of the wwwstat group). All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-g "/vmlinuz") ? print("-g /vmlinuz has setgid\n") :

	 print("-g /vmlinuz has not setgid\n");

-k

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the sticky bit is set. It returns '' (false) if the sticky bit is not set. In UNIX, the sticky bit can mark an executable file to be held in memory when exited (for example, if the binary ls is marked as sticky, when the first person runs it, it is loaded from disk to memory and executed, but when the execution finishes, the binary stays in memory so that when the next person runs ls it does not need to be loaded into memory again because it is already there). This is normally set for frequently used commands to optimize execution speed. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-k "/vmlinuz") ? print("-k /vmlinuz is sticky\n") : 

	print("-k /vmlinuz is not sticky\n");

-l

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is a symbolic link. It returns '' (false) if the file is not a symbolic link. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


(-l "/vmlinuz") ? print("-l /vmlinuz is symlink\n") : 

	print("-l /vmlinuz is not symlink\n");

-M

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  integer (age of file in days relative to $BASETIME)

Definition

The file test operator takes one file handle or filename as an argument. It returns the age of the file in days relative to $BASETIME. All file test operators can take a special argument underscore, which means that the test is carried out on the same file handle as the last file test, stat(), or lstat() call. If no argument is supplied, $_ is used.

Example


print "-M ", -M "/etc/fstab", "\n";

-O

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  1 (true) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns 1 (true) if the file is owned by the real UID/GID and it returns '' (false) otherwise. For the superuser it always returns true. All