Chapter 15

Function List


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 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


(-o "/vmlinuz") ? print("-o /vmlinuz is owned by real uid/gid\n") : 

	print("-o /vmlinuz is not owned by real uid/gid\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. This function returns 1 (true) if the file is owned by the effective UID/GID and it returns '' (false) otherwise. For the superuser it always returns true. 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


(-O "/vmlinuz") ? print("-O /vmlinuz is owned by effective uid/gid\n") : 

	print("-o /vmlinuz is not owned by effective uid/gid\n");

-p

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 named pipe. It returns '' (false) if the file is not a named pipe. 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


(-p "/vmlinuz") ? print("-p /vmlinuz is named pipe\n") : 

	print("-p /vmlinuz is not named pipe\n");

-R

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 readable by the effective UID/GID and it returns '' (false) otherwise. For the superuser it always returns true. 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


(-R "/vmlinuz") ? print("-R /vmlinuz is readable by effective uid/gid\n") : 

	print("-R /vmlinuz is not readable by effective uid/gid\n");

-r

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 readable by the real UID/GID and it returns '' (false) otherwise. For the superuser it always returns true. 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


(-r "/vmlinuz") ? print("-r /vmlinuz is readable by real uid/gid\n") : 

	print("-r /vmlinuz is not readable by real uid/gid\n");

-S

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


(-S "/vmlinuz") ? print("-S /vmlinuz is socket\n") : 

	print("-S /vmlinuz is not socket\n");

-s

Compliance

Syntax


Category  named unary operator (file test)

Arguments  handle

Arguments  filename

Arguments  none

Return Value  integer (size) '' (false)

Definition

The file test operator takes one file handle or filename as an argument. It returns size in bytes as an integer if the file has a non-zero size. It returns '' (false) if the file has zero size. 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


(-s "/vmlinuz") ? print("-s /vmlinuz has non-zero size\n") : 

	print("-s /vmlinuz does not have non-zero size\n");

-T

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 text file. It returns '' (false) if the file is not a text file. The first characters of the file are checked to see if the high bit is set, and if a suitable number is not set the file is assumed to be text. If the file is empty, true is returned. 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


(-T "/vmlinuz") ? print("-T /vmlinuz is text file\n") : 

	print("-T /vmlinuz is not text file\n");

-t

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 terminal tty device. It returns '' (false) if the file is not. 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, STDIN is used.

Example


(-t "/vmlinuz") ? print("-t /vmlinuz is tty\n") : 

	print("-t /vmlinuz is not tty\n");

-u

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 setuid bit set. It returns '' (false) if the files does not have the setuid bit set. In UNIX, setuid allows an executable to take on the uid of the user ownership of the executable itself while executing. 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


(-u "/vmlinuz") ? print("-u /vmlinuz has suid set\n") : 

	print("-u /vmlinuz does not have suid set\n");

-W

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 writable by the real uid/gid. It returns '' (false) otherwise. For the superuser it always returns true. 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


(-W "/vmlinuz") ? print("-W /vmlinuz is writable by real uid/gid\n") : 

	print("-W /vmlinuz is not writable by real UID/GID\n");

-w

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 writable by the effective uid/gid. It returns '' (false) otherwise. For the superuser it always returns true. 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


(-w "/vmlinuz") ? print("-w /vmlinuz is writable by effective uid/gid\n") :

	 print("-l /vmlinuz is not writable by effective uid/gid\n");

-X

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 executable by the real uid/gid. It returns '' (false) otherwise. For the superuser it always returns true. 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


(-X _) ? print("-X /bin/ls is executable by real uid/gid\n") : 

	print("-X /bin/ls is not executable by real uid/gid\n");

-x

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 executable by the effective uid/gid. It returns '' (false) otherwise. For the superuser it always returns true. 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


(-x "/bin/ls") ? print("-x /bin/ls is executable by effective uid/gid\n") : 

	print("-x /bin/ls is not executable by effective uid/gid\n");

-z

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 zero size. It returns '' (false) otherwise. 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


(-z "/vmlinuz") ? print("-z /vmlinuz has zero size\n") : 

	print("-z /vmlinuz does not have zero size\n");

abs

Compliance

Syntax


Category  named unary operator (numeric)

Arguments  numeric value

Return Value  numeric

Definition

This function returns the absolute value of its argument (it ignores any sign).

Example


print("abs(-10) = ",abs(-10),"\n");

accept

Compliance

Syntax


Category  list operator (socket)

Arguments  newsocket, genericsocket

Return Value  integer (address of socket), '' (false)

Definition

This function performs the low-level UNIX socket call accept().

alarm

Compliance

Syntax


Category  named unary operator (process)

Arguments  integer (seconds)

Return Value  integer (seconds to previous alarm)

Definition

This function sets up a UNIX SIGALRM signal to be generated in the number of seconds specified. It is possible for Perl to trap such signals by calling specific signal handling subroutines, such as trap(). Subseqent calls reset the alarm() time, retaining the number of seconds which were needed before the previous SIGALRM would have been generated. A call with zero seconds as an argument cancels the current alarm().

Example


print("alarm(10) ",alarm(10),

" (to illustrate it needs to trapped c.f. trap)\n");

atan2

Compliance

Syntax


Category  list operator (numeric)

Arguments  numeric, numeric

Return Value  numeric

Definition

The atan2 function returns the arctangent of the arguments.

Example


print("atan2(60,2) = ",atan2(60,2),"\n");

bind

Compliance

Syntax


Category  list operator (socket)

Arguments  sockethandle, numeric (network address)

Return Value  1 (true) '' (false)

Definition

This function binds a network address to the socket handle, see the UNIX bind() call.

binmode

Compliance

Syntax


Category  named unary operator (i/o)

Arguments  handle

Return Value  1 (success) or undefined (error)

Definition

On systems that distinguish between text and binary files, this function forces binary mode treatment of the given file handle. In systems which do make the distinction, text files have the end of line characters (Carriage Return, Linefeed) automatically translated to the UNIX end of line character (Linefeed) when reading from the file (and vice versa when writing to the file); binary mode files do not have this automatic transformation.

Example


open(FIL,"file.dat");

binmode(FIL);

bless

Compliance

Syntax


Category  list operator (class)

Arguments  variable

Arguments  variable, classname

Return Value  reference

Definition

This function assigns a class to the referenced object. This class is either explicitly stated in the call, or the name of the current package is used if a second argument is not used in the call. The reference is returned.

TIP
Explictly state the class (use the two-argument version of the call) if the code can be inherited by other classes, because the class in the single-argument call would not return the required value.

Example


$tmp = {};

bless $tmp, ATMPCLASS;

print "bless() \$tmp is now in class ",ref($tmp),"\n";

caller

Compliance

Syntax


Category  named unary operator (scope)

Arguments  expression

Arguments  none

Return Value  1 (true) '' (false)

Return Value  (package, filename, line)

Definition

This function is used to test the current scope of a subroutine call. If evaluated in a scalar context, it returns 1 or '' depending on if the current code has been called as a subroutine (this includes code which is included using a require() or an eval() call). In an array context it supplies details of the calling context in a list comprising the package name, filename, and line of the call.

Example


sub testcaller {

    ($package, $file, $line) = caller;

}

&testcaller;

print "caller() Package=$package File=$file Line=$line \n";

chdir

Compliance

Syntax


Category  named unary operator (files)

Arguments  expression

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function changes the current directory to the directory specified. If no argument is given this call changes the current directory to be the home directory of the current user. It returns 1 upon success and '' otherwise.

Example


chdir("/") ? print("It worked.\n") : print("It didn't work.\n");

chmod

Compliance

Syntax


Category  list operator (files)

Arguments  list

Return Value  numeric

Definition

The first element in the list is the UNIX octal number representing the file permission. This function applies the mode specified by the octal number to all the files in the list that follows. It returns the number of files successfully modified.

Example


print "chmod() changed ",

chmod(0744,"/tmp/test1.txt","/tmp/test2.txt")," files.\n";

chomp

Compliance

Syntax


Category  list operator (string)

Arguments  list

Arguments  variable

Arguments  none

Return Value  numeric

Definition

This is an alternative to the chop() function. It removes characters at the end of strings corresponding to the $INPUT_LINE_SEPARATOR ($/). It returns the number of characters removed. It can be given a list of strings upon which to perform this operation. When given no arguments, the operation is performed on $_.

Example


$tmp="Aaagh!\n";

$ret = chomp $tmp;

print("chomp() ", $tmp, " returned ", $ret, "\n");

chop

Compliance

Syntax


Category  list operator (string)

Arguments  list

Arguments  variable

Arguments  none

Return Value  character

Definition

This function removes the last character of a string and returns that character. If given a list of arguments, the operation is performed on each one and the last character chopped is returned.

Example


$tmp = "1234";

$ret = chop $tmp;

print("chop() ", $tmp, " returned ", $ret, "\n");


TIP
Use chomp() (with $/ set to "\n") rather than chop() if you are unsure that the string has a trailing newline because chop() will remove the last character regardless, but chomp()only removes it if it is a newline.

chown

Compliance

Syntax


Category  list operator (files)

Arguments  list

Return Value  numeric

Definition

This function changes the ownership of the specified files. The first two elements of the list define the user ID and the group ID to set this ownership; the subsequent items in the list are the file names that are changed. The return value is the number of files successfully changed.

Example


print("chown() "); 

chown(1,1,"/tmp/test1.txt") ? print("Worked\n") : print("Didn't work\n");

chr

Compliance

Syntax


Category  named unary operator (string)

Arguments  numeric

Return Value  character

Definition

This function returns the character indicated by the numeric argument.

Example


$E = chr(69);

print("chr() $E \n");

chroot

Compliance

Syntax


Category  named unary operator (files)

Arguments  directoryname

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function is equivalent to the UNIX chroot() function. Given a directory name, this directory is treated as the root directory by all subseqent file system references, thus effectively hiding the rest of the file system outside the specified directory. This restriction applies to all subprocesses of the current process as well.

TIP
Normal UNIX security limits this function to the superuser, and it is normally used to make processes safer by only allowing them access to the subdirectory tree relevant to their purpose.

Example


print("chroot() ");

chroot("/") ? print("Worked.\n") : print("Didn't work.\n");

close

Compliance

Syntax


Category  named unary operator (files)

Arguments  handle

Return Value  1 (true) '' (false)

Definition

This function closes the file opened with the file handle. This operation flushes all buffered output. If the file handle refers to a pipe, the Perl program waits until the process being piped has finished.

Example


open(INF,"/tmp/test1.txt");

$ret = close(INF);

print("close() Returned ",$ret," on success\n");

closedir

Compliance

Syntax


Category  named unary operator (file)

Arguments  handle

Return Value  1 (true) '' (false)

Definition

This function closes the directory opened by opendir() by specifying the relevant directory handle.

Example


opendir(IND,"/tmp");

$ret = closedir(IND);

print("closedir() Returned ",$ret," on success\n");

connect

Compliance

Syntax


Category  list operator (socket)

Arguments  socket, name

Return Value  1 (true) '' (false)

Definition

This function is equivalent to the UNIX function call, which initiates a connection with a process, assuming that the process that is connected is waiting to accept.

continue

Compliance

Syntax


Category  flow control

Arguments  block

Return Value  n/a

Definition

A continue block is a syntax structure that allows a condition to be attached to another block (normally a while block). Any statements in the continue block are evaluated before the attached block is repeated.

Example


$i=0;

print "continue() ";

while ($i<10) {

      if ($i % 2)

         { print "${i}o "; next; }

      else

         {print "${i}e ";}

} continue {$i++}

print "\n";

cos

Compliance

Syntax


Category  named unary operator (numeric)

Arguments  expression

Return Value  numeric

Definition

This function returns the cosine value of the numeric expression supplied as an argument.

Example


print "cos() ",cos(60),"\n";

crypt

Compliance

Syntax


Category  list operator

Arguments  string, string

Return Value  string

Definition

This function is equivalent to the crypt() UNIX call (where available). It encrypts a string (the first argument) using a key (usually the first two letters of the first string itself) and returns the encrypted string.

Example


print "crypt() Password PA: ",crypt("Password","PA"),"\n";

dbmclose

Compliance

Syntax


Category  named unary operator (i/o)

Arguments  arrayname

Return Value  1 (true) '' (false)

Definition

This function undoes the linking of an associative array to a dbm file (see dbmopen()).

NOTE
This is depreciated in Perl 5; use untie() instead.

dbmopen

Compliance

Syntax


Category  list operator (i/o)

Arguments  arrayname, dbname, mode

Return Value  fatal error if dbm not supported (Perl 4)

Definition

This function links the associative array referred to by arrayname, to the dbm database (or equivalent) referred to by dbname (this name should not include the suffix). If the database does not exist, a new one with the specified mode will be opened (the mode being an octal chmod() style file protection).

This is depreciated in Perl 5; use tie() instead.

defined

Compliance

Syntax


Category  named unary operator (misc)

Arguments  expression

Return Value  1 (true) '' (false)

Definition

This function returns a Boolean value depending on whether the argument is defined or not. There is a subtle distinction between an undefined and a defined null value. Some functions return an undefined null to indicate errors, while others return a defined null to indicate a particular result (use a comparison with the null string to test for this, rather than using defined())

Example


@iexist = (1,2,3);

print("defined() The array \@iexist ");

defined @iexist ? print("exists.\n") : print("does not exist.\n");

delete

Compliance

Syntax


Category  named unary operator (hash)

Arguments  expression

Return Value  value

Definition

Use this function to delete an element from a hash array, given the key for the element to delete, returning the value of the deleted element.

Example


%Hash = (1, One, 2, Two, 3, Three);

print("delete() Deleted ",delete($Hash{1}),"\n");

die

Compliance

Syntax


Category  list operator (i/o)

Arguments  list

Return Value  errorlevel

Definition

This function terminates execution of the Perl script when called printing the value of the list argument to STDERR (as if called with print(STDERR, list)). The exit value is the current value of $OS_ERROR ($!), which may have been set by a previous function. If this has a value of zero it returns $CHILD_ERROR ($?). If this is zero, it exits with errorlevel 255. If the error message string specified by the list does not end in a newline, the text "at $PROGRAM_NAME at line line, where line is the line number of the Perl script.

Example


die("die() Now we can give an example of die()...exiting");

do

Compliance

Syntax


Category  (flow)

Arguments  block

Arguments  subroutine(list)

Arguments  expression

Return Value  special

Definition

This is a syntax structure that allows repeated execution of a block of statements. The value returned is the result of the last statement in the block. Normally an exit condition is supplied after the block. The second form where the argument is subroutine() is a depreciated form. The third form executes the contents of the file name specified by the expression (but it is better to use use() or require() instead, because this has better error checking).

Example


$i=1;

print("do ");

$return = do {

  print $i, " ";

  $i++;

} until $i==3;

print("Returned $return\n");

dump

Compliance

Syntax


Category  named unary operator (misc)

Arguments  label

Return Value  N/A

Definition

This function causes the program to create a binary image core dump. This then allows the dumped image to be reloaded using the undump() function (if supported) which can effectively allow the use of precompiled Perl images. When reloaded, the program begins execution from the label specified. It is possible to set up a program which initializes data structures to dump() after the initialization so that execution is faster when reloading the dumped image.

each

Compliance

Syntax


Category  named unary operator (hash)

Arguments  variable

Return Value  key, value

Definition

This function allows iteration over the elements in an associative array. Each time it is evaluated, it returns another list of two elements (a key, value pair from the associative array). When all the elements have been returned, it returns a null list.

Example


%NumberWord = (1, One, 2, Two, 3, Three);

print("each() ");

while (($number,$wordform)=each(%NumberWord)) {

  print("$number:$wordform ");

}

print("\n");

endgrent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the /etc/group file used by getgrent() and other group-related functions. It is equivalent to the UNIX system call.

Example


($name,$pw,$gid,@members)=getgrent();

$returned = endgrent();

print("endgrent() Closes /etc/group [$name,$gid]",

	" file returning $returned.\n");

endhostent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the TCP socket used by name server queries gethostbyname() and host- related functions. It is equivalent to the UNIX system call.

Example


$host = gethostbyname("lynch");

$returned = endhostent();

print("endhostent() Closes /etc/hosts [$host]",

	" returning $returned.\n");

endnetent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the /etc/networks file used by getnetent() and network-related functions. This function is equivalent to the UNIX system call.

Example


($name,$alias,$net,$net) = getnetent();

$returned = endnetent();

print("endnetent() Closes /etc/networks [$name]",

	" returning $returned.\n");

endprotoent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the /etc/protocols file used by getprotoent() and protocol-related functions. It is equivalent to the UNIX system call.

Example


($name, $alias, $protocol) = getprotoent();

$returned = endprotoent();

print("endprotoent() Closes /etc/protocols ",

	"[$name,$alias,$protocol] file returning $returned.\n");

endpwent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the /etc/passwd file used by getpwent() and password-related functions. It is equivalent to the UNIX system call.

Example


($name,$pass,$uid,$gid,$quota,$name,$gcos,$logindir,$shell) = getpwent();

$returned = endpwent();

print("endpwent() Closes /etc/passwd [$logindir,$shell] ",

	"file returning $returned.\n");

endservent

Compliance

Syntax


Category  (system files)

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function closes the /etc/servers file used by getservent() and related functions. It is equivalent to the UNIX system call.

Example


($name,$aliases,$port,$protocol) = getservent();

$returned = endservent();

print("endservent() Closes /etc/servers [$name]",

	" file returning $returned.\n");

eof

Compliance

Syntax


Category  named unary operator (i/o)

Arguments  handle

Arguments  ()

Arguments  none

Return Value  1 (true) '' (false)

Definition

This function tests if the file pointer to file specified by the file handle is at the end of the file. This is done by reading the next character and then undoing this operation (so is only suitable on files where this can be done safely). If no argument is supplied the file tested is the last file that was read. If the empty list is supplied then this tests if all the last files that supplied an argument to the Perl script are eof() (that is, it can be used as a termination condition in a while loop).

Example


open INF, "/tmp/test1.txt";

if (eof INF) 

  {print "eof() TRUE\n";}

else

  {print "eof() FALSE\n";}

close INF;

eval

Compliance

Syntax


Category  named unary operator (flow)

Arguments  expression

Arguments  block

Arguments  none

Return Value  special

Definition

This function treats the expression like a Perl program and executes it by returning the return value of the last statement executed. As the context of this execution is the same as that of the script itself, variable definitions and subroutine definitions persist. Syntax errors and runtime errors (including die()) are trapped and an undefined result is returned. If such an error does occur, $EVAL_ERROR ($@) is set. If no errors are found, $@ is equal to a defined null string. If no expression is supplied, $_ is the default argument. If the block syntax is used, the expressions in the block are evaluated only once within the script, which may be more efficient for certain situations.

TIP
eval() traps possible error conditions that would otherwise crash a program. Therefore, it can be used to test if certain features are available that would cause runtime errors if used when not available.

Example


$ans = 3;

eval "$ans = ;";

if ($@ eq "")

  {print "eval() returned success.\n";}

else

  {print "eval() error: $@";}

exec

Compliance

Syntax


Category  list operator (process)

Arguments  list

Return Value  N/A

Definition

This function passes control from the script to an external system command. There is no retain from this call so there is no return value. Note that system() calls external commands and does return to the next line in the calling Perl program.

This is equivalent to the UNIX system call execvp().

Example


exec("cat /etc/motd");

exists

Compliance

Syntax


Category  named unary operator (hash)

Arguments  expression

Return Value  1 (true) '' (false)

Definition

This function tests if a given key value exists in an associative array, returning a Boolean value.

Example


%test = ( One, 1, Two, 2);

if (exists $test{One}) 

  {print "exists() returned success.\n";}

else

  {print "exists() returned an error.\n";}

exit

Compliance

Syntax


Category  named unary operator (flow)

Arguments  expression

Arguments  none

Return Value  value

Definition

This function evaluates the expression given as an argument and exits the program with that error. The default value for the error is 0 if no argument is supplied. Note that die() allows an error message.

Example


exit(16);

exp

Compliance

Syntax


Category  named unary operator (numeric)

Arguments  expression

Arguments  none

Return Value  numeric

Definition

This function returns the natural log base (e) to the power of expression (or of $_ if none specified).

Example


print "exp() e**1 is ",exp(1),"\n";

fcntl

Compliance

Syntax


Category  list operator (i/o)

Arguments  handle, function, packed_parameters

Definition

This function is equivalent to the UNIX fnctl() call. In Perl 5, use the fntcl module. In Perl 4, there should be some mechanism for linking the Perl functions to the system functions. This is usually executed when Perl is installed.

fileno

Compliance

Syntax


Category  named unary operator (i/o)

Arguments  handle

Return Value  descriptor

Definition

This function returns the file descriptor given a file handle.

Example


print("fileno() ",fileno(INF),"\n");

flock

Compliance

Syntax


Category  list operator (i/o)

Arguments  handle, operation

Return Value  1 (true) '' (false)

Definition

This calls the UNIX flock() function to access file locks. The handle is a Perl file handle. The operation is any valid flock() operation: place exclusive lock, place shared lock, unlock. These operations are represented by numeric values.

fork

Compliance

Syntax


Category  (process)

Arguments  none

Return Value  pid

Definition

The fork function calls the UNIX fork() function or equivalent to fork a subprocess at this point. Returns the process ID (pid) of the child process to the calling process; returns 0 to the child process itself. The calling program should wait() on any child process it forks to avoid creating zombie processes.

Example


$pid = fork;

# Child only prints this

if ($pid != 0) { 

  print("fork() Forking a process duplicates o/p: $pid \n");

}

waitpid($pid,0);

# Child exits here

if ($$ != $origpid) { die; }

format

Compliance

Syntax


Category:  list operator (i/o)

Arguments:  format

Definition

This function declares an output format specification. These formats are used in conjunction with the write() function to control the output of variables and text to conform to a standard layout structure. Normally, the specification i