|
perlfunc - Perl builtin functions
The functions in this section can serve as terms in an expression.
They fall into two major categories: list operators and named unary
operators. These differ in their precedence relationship with a
following comma. (See the precedence table in the perlop manpage.) List
operators take more than one argument, while unary operators can never
take more than one argument. Thus, a comma terminates the argument of
a unary operator, but merely separates the arguments of a list
operator. A unary operator generally provides a scalar context to its
argument, while a list operator may provide either scalar or list
contexts for its arguments. If it does both, the scalar arguments will
be first, and the list argument will follow. (Note that there can ever
be only one such list argument.) For instance, splice() has three scalar
arguments followed by a list, whereas gethostbyname() has four scalar
arguments.
In the syntax descriptions that follow, list operators that expect a
list (and provide list context for the elements of the list) are shown
with LIST as an argument. Such a list may consist of any combination
of scalar arguments or list values; the list values will be included
in the list as if each individual element were interpolated at that
point in the list, forming a longer single-dimensional list value.
Commas should separate elements of the LIST.
Any function in the list below may be used either with or without
parentheses around its arguments. (The syntax descriptions omit the
parentheses.) If you use the parentheses, the simple (but occasionally
surprising) rule is this: It looks like a function, therefore it is a
function, and precedence doesn't matter. Otherwise it's a list
operator or unary operator, and precedence does matter. And whitespace
between the function and left parenthesis doesn't count--so you need to
be careful sometimes:
print 1+2+4;
print(1+2) + 4;
print (1+2)+4;
print +(1+2)+4;
print ((1+2)+4);
If you run Perl with the -w switch it can warn you about this. For
example, the third line above produces:
print (...) interpreted as function at - line 1.
Useless use of integer addition in void context at - line 1.
A few functions take no arguments at all, and therefore work as neither
unary nor list operators. These include such functions as time
and endpwent. For example, time+86_400 always means
time() + 86_400.
For functions that can be used in either a scalar or list context,
nonabortive failure is generally indicated in a scalar context by
returning the undefined value, and in a list context by returning the
null list.
Remember the following important rule: There is no rule that relates
the behavior of an expression in list context to its behavior in scalar
context, or vice versa. It might do two totally different things.
Each operator and function decides which sort of value it would be most
appropriate to return in scalar context. Some operators return the
length of the list that would have been returned in list context. Some
operators return the first value in the list. Some operators return the
last value in the list. Some operators return a count of successful
operations. In general, they do what you want, unless you want
consistency.
A named array in scalar context is quite different from what would at
first glance appear to be a list in scalar context. You can't get a list
like (1,2,3) into being in scalar context, because the compiler knows
the context at compile time. It would generate the scalar comma operator
there, not the list construction version of the comma. That means it
was never a list to start with.
In general, functions in Perl that serve as wrappers for system calls
of the same name (like chown(2), fork(2), closedir(2), etc.) all return
true when they succeed and undef otherwise, as is usually mentioned
in the descriptions below. This is different from the C interfaces,
which return -1 on failure. Exceptions to this rule are wait,
waitpid, and syscall. System calls also set the special $!
variable on failure. Other functions do not, except accidentally.
Here are Perl's functions (including things that look like
functions, like some keywords and named operators)
arranged by category. Some functions appear in more
than one place.
- Functions for SCALARs or strings
-
chomp, chop, chr, crypt, hex, index, lc, lcfirst,
length, oct, ord, pack, q//, qq//, reverse,
rindex, sprintf, substr, tr///, uc, ucfirst, y///
- Regular expressions and pattern matching
-
m//, pos, quotemeta, s///, split, study, qr//
- Numeric functions
-
abs, atan2, cos, exp, hex, int, log, oct, rand,
sin, sqrt, srand
- Functions for real @ARRAYs
-
pop, push, shift, splice, unshift
- Functions for list data
-
grep, join, map, qw//, reverse, sort, unpack
- Functions for real %HASHes
-
delete, each, exists, keys, values
- Input and output functions
-
binmode, close, closedir, dbmclose, dbmopen, die, eof,
fileno, flock, format, getc, print, printf, read,
readdir, rewinddir, say, seek, seekdir, select, syscall,
sysread, sysseek, syswrite, tell, telldir, truncate,
warn, write
- Functions for fixed length data or records
-
pack, read, syscall, sysread, syswrite, unpack, vec
- Functions for filehandles, files, or directories
-
-X, chdir, chmod, chown, chroot, fcntl, glob,
ioctl, link, lstat, mkdir, open, opendir,
readlink, rename, rmdir, stat, symlink, sysopen,
umask, unlink, utime
- Keywords related to the control flow of your Perl program
-
caller, continue, die, do, dump, eval, exit,
goto, last, next, redo, return, sub, wantarray
- Keywords related to switch
-
break, continue, given, when, default
-
(These are only available if you enable the "switch" feature.
See the feature manpage and Switch statements in the perlsyn manpage.)
- Keywords related to scoping
-
caller, import, local, my, our, state, package,
use
-
(state is only available if the "state" feature is enabled. See
the feature manpage.)
- Miscellaneous functions
-
defined, dump, eval, formline, local, my, our,
reset, scalar, state, undef, wantarray
- Functions for processes and process groups
-
alarm, exec, fork, getpgrp, getppid, getpriority, kill,
pipe, qx//, setpgrp, setpriority, sleep, system,
times, wait, waitpid
- Keywords related to perl modules
-
do, import, no, package, require, use
- Keywords related to classes and object-orientation
-
bless, dbmclose, dbmopen, package, ref, tie, tied,
untie, use
- Low-level socket functions
-
accept, bind, connect, getpeername, getsockname,
getsockopt, listen, recv, send, setsockopt, shutdown,
socket, socketpair
- System V interprocess communication functions
-
msgctl, msgget, msgrcv, msgsnd, semctl, semget, semop,
shmctl, shmget, shmread, shmwrite
- Fetching user and group info
-
endgrent, endhostent, endnetent, endpwent, getgrent,
getgrgid, getgrnam, getlogin, getpwent, getpwnam,
getpwuid, setgrent, setpwent
- Fetching network info
-
endprotoent, endservent, gethostbyaddr, gethostbyname,
gethostent, getnetbyaddr, getnetbyname, getnetent,
getprotobyname, getprotobynumber, getprotoent,
getservbyname, getservbyport, getservent, sethostent,
setnetent, setprotoent, setservent
- Time-related functions
-
gmtime, localtime, time, times
- Functions new in perl5
-
abs, bless, break, chomp, chr, continue, default,
exists, formline, given, glob, import, lc, lcfirst,
lock, map, my, no, our, prototype, qr//, qw//, qx//,
readline, readpipe, ref, sub*, sysopen, tie, tied, uc,
ucfirst, untie, use, when
-
* - sub was a keyword in perl4, but in perl5 it is an
operator, which can be used in expressions.
- Functions obsoleted in perl5
-
dbmclose, dbmopen
Perl was born in Unix and can therefore access all common Unix
system calls. In non-Unix environments, the functionality of some
Unix system calls may not be available, or details of the available
functionality may differ slightly. The Perl functions affected
by this are:
-X, binmode, chmod, chown, chroot, crypt,
dbmclose, dbmopen, dump, endgrent, endhostent,
endnetent, endprotoent, endpwent, endservent, exec,
fcntl, flock, fork, getgrent, getgrgid, gethostbyname,
gethostent, getlogin, getnetbyaddr, getnetbyname, getnetent,
getppid, getpgrp, getpriority, getprotobynumber,
getprotoent, getpwent, getpwnam, getpwuid,
getservbyport, getservent, getsockopt, glob, ioctl,
kill, link, lstat, msgctl, msgget, msgrcv,
msgsnd, open, pipe, readlink, rename, select, semctl,
semget, semop, setgrent, sethostent, setnetent,
setpgrp, setpriority, setprotoent, setpwent,
setservent, setsockopt, shmctl, shmget, shmread,
shmwrite, socket, socketpair,
stat, symlink, syscall, sysopen, system,
times, truncate, umask, unlink,
utime, wait, waitpid
For more information about the portability of these functions, see
the perlport manpage and other available platform-specific documentation.
- -X FILEHANDLE
- -X EXPR
- -X DIRHANDLE
- -X
-
A file test, where X is one of the letters listed below. This unary
operator takes one argument, either a filename, a filehandle, or a dirhandle,
and tests the associated file to see if something is true about it. If the
argument is omitted, tests $_, except for -t, which tests STDIN.
Unless otherwise documented, it returns 1 for true and '' for false, or
the undefined value if the file doesn't exist. Despite the funny
names, precedence is the same as any other named unary operator. The
operator may be any of:
-
-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-
-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
-
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
-
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other platforms)
-
Example:
-
while (<>) {
|