A.21 Built-in Functions

Perl has many built-in functions. Table A-6 is a partial list with short descriptions.

Table A-6. Perl built-in functions

Function

Summary

abs VALUE

Return the absolute value of its numeric argument

atan2 Y, X

Return the principal value of the arc tangent of Y/X from -p to p

chdir EXPR

Change the working directory to EXPR (or home directory by default)

chmod MODE LIST

Change the file permissions of the LIST of files to MODE

chomp (VARIABLE or LIST)

Remove ending newline from string(s), if present

chop (VARIABLE or LIST)

Remove ending character from string(s)

chown UID, GID, LIST

Change owner and group of LIST of files to numeric UID and GID

close FILEHANDLE

Close the file, socket, or pipe associated with FILEHANDLE

closedir DIRHANDLE

Close the directory associated with DIRHANDLE

cos EXPR

Return the cosine of the radian number EXPR

dbmclose HASH

Break the binding between a DBM file and a hash

dbmopen HASH, DBNAME, MODE

Bind a DBM file to a HASH with permissions given in MODE

defined EXPR

Return true or false if EXPR has a defined value or not

delete EXPR

Delete an element (or slice) from a hash or an array

die LIST

Exit the program with an error message that includes LIST

each HASH

Step through a hash with one key, or key/value pair, at a time

exec PATHNAME LIST

Terminate the program and execute the program PATHNAME with arguments LIST

exists EXPR

Return true if hash key or array index exists

exit EXPR

Exit the program with the return value of EXPR

exp EXPR

Return the value of e raised to the exponent EXPR

format

Declare a format for use by the write function

grep EXPR, LIST

Return list of elements of LIST for which EXPR is true

gmtime

Get Greenwich mean time; Sunday is day 0, January is month 0, year is number of years since 1900?example:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday, $isdaylightsavingstime) = gmtime;

goto LABEL

Program control goes to statement marked with LABEL

hex EXPR

Return decimal value of hexadecimal EXPR

index STR, SUBSTR

Give the position of the first occurrence of SUBSTR in STR

int EXPR

Give the integer portion of the number in EXPR

join EXPR, LIST

Join the strings in LIST into a single string, separated by EXPR

keys HASH

Return a list of all the keys in HASH

last LABEL

Exit the immediately enclosing loop by default, or loop with LABEL

lc EXPR

Return a lowercased copy of string in EXPR

lcfirst EXPR

Return a copy of EXPR with first character lowercased

length EXPR

Return the length in characters of EXPR

localtime

Get local time in same format as in gmtime function

log EXPR

Return natural logarithm of number EXPR

m/PATTERN/

The match operator for the regular-expression PATTERN, often abbreviated as /PATTERN/

map BLOCK LIST (or map EXPR, LIST)

Evaluate BLOCK or EXPR for each element of LIST, return list of return values

mkdir FILENAME

Create the directory FILENAME

my EXPR

Localize the variables in EXPR to the enclosing block

next LABEL

Go to next iteration of enclosing loop by default or to loop marked with LABEL

oct EXPR

Return decimal value of octal value in EXPR

open FILEHANDLE, EXPR

Open a file by associating FILEHANDLE with the file and options given in EXPR

opendir DIRHANDLE, EXPR

Open the directory EXPR and assign handle DIRHANDLE

pop ARRAY

Remove and return the last element of ARRAY

pos SCALAR

Give location in string SCALAR where last m//g search left off

print FILEHANDLE LIST

Print LIST of strings to FILEHANDLE (default STDOUT)

printf FILEHANDLE FORMAT, LIST

Print string specified by FORMAT and variables LIST to FILEHANDLE

push ARRAY, LIST

Place the elements of LIST at the end of ARRAY

rand EXPR

Give pseudorandom decimal number from 0 to less than EXPR (default 1)

readdir DIRHANDLE

Return list of entries of directory DIRHANDLE

redo LABEL

Restart a loop block without reevaluating the conditional

ref EXPR

Return true or false if EXPR is a reference or not: if true, returned value indicates type of reference

rename OLDNAME, NEWNAME

Change the name of a file

return EXPR

Return from the current subroutine with value EXPR

reverse LIST

Give LIST in reverse order, or reverse strings in scalar context

rindex STR, SUBSTR

Like the index function but returns last occurrence of SUBSTR in STR

rmdir FILENAME

Delete the directory FILENAME

s/PATTERN/REPLACEMENT/

Replace the match of regular-expression PATTERN with string REPLACEMENT

scalar EXPR

Force EXPR to be evaluated in scalar context

seek FILEHANDLE, OFFSET, WHENCE

Position the file pointer for FILEHANDLE to OFFSET bytes (if WHENCE is 0, current position plus OFFSET if WHENCE is 1, or OFFSET bytes from the end if WHENCE is 2)

shift ARRAY

Remove and return the first element of ARRAY

sin EXPR

Return the sine of the radian number EXPR

sleep EXPR

Cause the program to sleep for EXPR seconds

sort USERSUB LIST (or sort BLOCK LIST)

Sort the LIST according to the order in USERSUB or BLOCK (default standard string order)

splice ARRAY, OFFSET, LENGTH, LIST

Remove LENGTH elements at OFFSET in ARRAY and replace with LIST, if present

split /PATTERN/, EXPR

Split the string EXPR at occurrences of /PATTERN/, return list

sprintf FORMAT, LIST

Return a string formatted as in the printf function

sqrt EXPR

Return the square root of the number EXPR.

srand EXPR

Set random number seed for rand operator; only needed in versions of Perl before 5.004

stat (FILEHANDLE or EXPR)

Return statistics on file EXPR or its FILEHANDLE?example:

($dev,$inode,$mode,$num_of_links,$uid,$gid,$rdev,$size,$accesstime, $modifiedtime,$changetime,$blksize,$blocks) = stat $filename;

study SCALAR

Try to optimize subsequent pattern matches on string SCALAR

sub NAME BLOCK

Define a subroutine named NAME with program code in BLOCK

substr EXPR, OFFSET, LENGTH,REPLACEMENT

Return substring of string EXPR at position OFFSET and length LENGTH; the substring is replaced with REPLACEMENT if used

system PATHNAME LIST

Execute any program PATHNAME with arguments LIST; returns exit status of program, not its output; to capture ouput, use backticks?example:

@output = '/bin/who';

tell FILEHANDLE

Return current file position in bytes in FILEHANDLE

tr/ORIGINAL/REPLACEMENT/

Transliterates each character in ORIGINAL with corresponding character in REPLACEMENT

truncate (FILEHANDLE or EXPR), LENGTH

Shorten file EXPR or opened with FILEHANDLE to LENGTH bytes

uc EXPR

Return uppercased version of string EXPR

ucfirst EXPR

Return string EXPR with first character capitalized

undef EXPR

Return the undefined value; if a defined variable or subroutine EXPR is given, it's no longer defined; it can be assigned a value when you don't need to save the value

unlink LIST

Delete the LIST of files

unshift ARRAY, LIST

Add LIST elements to the beginning of ARRAY

use MODULE

Load the MODULE

values HASH

Return a list of all values of the HASH

wantarray

In a subroutine, return true if calling program expects a list return value

warn LIST

Print error message including LIST

write FILEHANDLE

Write formatted record to FILEHANDLE (default STDOUT) as defined by the format function