Here's a list of perl builtins that default to act on $_
when given no argument.
- filetest operators (except for -t)
- mkdir (since perl 5.10.0)
- reverse (Update: only in scalar context)
- split (actually defaults to $_ on its second argument, ' ' on first one)
- unlink (the scaryest)
- unpack (since perl 5.10.0, the second argument defaults to $_)
There are other operations that use $_ implicitly.
For example, regexps, substitutions, and transliterations (m, s, tr, y)
act on $_ unless a different string is given by the =~ operator;
map, grep, the for statement modifier, and for loops without explicit
loop variable specified use $_ as the loop variable;
a diamond operator in a while loop stores the line to $_.
Below are some functions that do not default to $_.
Note that a few functions behave differently if
they are called with an empty pair of parenthesis
(usually taken as an empty list).
- -t (terminal) filetest: defaults to STDOUT
- bless: requires an argument
- caller: returns less information without argument
- chdir: uses the home directory
- die: defaults to $@ (to rethrow an exception caught with eval)
or a generic message: "Died"
- do: syntax error without parenthesis;
with empty parenthesis, dies with "Null filename used"
- eof: with empty parenthesis defaults to ARGV,
without parenthesis defaults to the last file read
(but detects the ends of individual files read by ARGV)
- exec: it seems that the call just returns failure,
I can't find anything in the docs
- exit: defaults to zero
- gethostbyname, getnetbyname, getprotobyname: requires an argument
- getpgrp: uses zero, meaning the current process
- getpwnam, getgrname: requires an argument
- getpwuid, getgrgid: requries an argument
- gmtime: uses current time
- local: syntax error without parenthesis;
with empty parenthesis, is a no-op
(localizing $_ could be useful,
but this way is more consistent with localizing lists of variables)
- localtime: uses the current time
- lock: requires an argument
- mkdir: required an argument before perl 5.10.0
- my: requires an argument (even empty parenthesis don't suffice)
- our: requires an argument
- pack: requires an argument
- prototype: seems to just return undef,
I've found nothing in the docs
- rand: uses 1
- readpipe: no idea, but it can produce segfaults
- reset: resets only question-mark searches, no variables
- return: returns empty list in list context, undef in scalar context
- scalar: requires an argument
- sleep: sleeps forever
- sort: gives a funny error message without parenthesis;
with parenthesis, sorts the empty list
- sprintf: requires an argument
- srand: uses some reasonable seed
- syscall: requires an argument
- system: seems to just return an exit code 255,
I've found nothing relevant in the docs
- tied: requires an argument
- umask: returns the current umask without changing it
- undef: returns undef without undefining anything
- untie: requires an argument
- waitpid: requires two arguments
- warn: uses $@ suffixed with "\t...caught",
or a sensible message "Warning: something's wrong" if $@ is unset.
- write: uses the currently selected filehandle
The following functions accept a
filehandle, directory handle, label, or bareword
as an argument, and thus don't default to $_.
(I do not list those functions that need an array or hash argument, like pop.)
- binmode: requires an argument
- close: defaults to the selected filehandle
- closedir: requires an argument
- dump: defaults to the top of the program
- fileno: requires an argument
- getc: uses STDIN
- getpeername, getsockname: requires an argument
- goto: requires an argument
- last, next: uses the innermost loop
- no: requires an argument
- open: requires an argument
- package: without argument, declares that there is no current package,
but this usage is strongly depreciated
- readdir: requires an argument
- readline: no idea, I could even get a segfault
(the diamond shortcut, however, defaults to the special ARGV filehandle)
- redo: uses the innermost loop
- rewinddir: requires an argument
- select: returns the current selected filehandle without changing it
- tell: uses the filehandle last read
- telldir: requires an argument
- use: requires an argument
I've compiled these lists using both the perldoc perlfunc
and testing how the functions actually behave with perl (5.8.8 i686-linux).
For this reason, there can be errors and omissions in this list.
If you know about any omission, please contact me.
Update: added readmore.
Update 2012-12-27: added the new evalbytes function.
Update 2014-01-29: added changes to mkdir and unpack.