Named Unary and File Test Operators
Some of the “functions” described in Chapter 27 are really unary operators. Table 3-2 lists all the named unary operators.
Table 3-2. Named unary operators
–X (file
tests) | fileno | lock | setnetent |
abs | getc | log | setprotoent |
alarm | getgrgid | lstat | setservent |
caller | getgrnam | my | shift |
chdir | gethostbyname | oct | sin |
chomp | getnetbyname | ord | sleep |
chop | getpeername | our | sqrt |
chr | getpgrp | pop | srand |
chroot | getprotobyname | pos | stat |
close | getpwnam | prototype | state |
closedir | getpwuid | quotemeta | study |
cos | getsockname | rand | tell |
dbmclose | glob | readdir | telldir |
defined | gmtime | readline | tied |
delete | hex | readlink | uc |
do | int | readpipe | ucfirst |
each | keys | ref | umask |
eof | lc | reset | undef |
eval | lcfirst | rewinddir | untie |
exists | length | rmdir | values |
exit | local | scalar | write |
exp | localtime | sethostent | any ($) sub |
fc |
Unlike list operators, unary operators have a higher precedence than some of the binary operators. For example:
sleep 4 | 3;
does not sleep for 7 seconds. It sleeps for 4 seconds and then takes
the return value of sleep (typically
zero) and bitwise ors
that with 3, as if the expression were parenthesized as:
(sleep 4) | 3;
Compare this with:
print 4 | 3;
which does take the value of 4 ored with 3 before printing it (7, in this case), as if it were written:
print (4 | 3);
This is because print is a list
operator, not a simple unary operator. Once you’ve learned which operators
are list operators, you’ll have no trouble telling unary operators and
list operators apart. When in doubt, you can always use parentheses to turn a named unary operator into a function. Remember, if it looks like ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access