summaryrefslogtreecommitdiff
path: root/pod/perlfunc.pod
diff options
context:
space:
mode:
Diffstat (limited to 'pod/perlfunc.pod')
-rw-r--r--pod/perlfunc.pod855
1 files changed, 496 insertions, 359 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 8d8e9a642a..a193d672f6 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -12,11 +12,12 @@ 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 and list
+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.
+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
@@ -47,6 +48,11 @@ 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 C<time>
+and C<endpwent>. For example, C<time+86_400> always means
+C<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
@@ -56,7 +62,7 @@ Remember the following important rule: There is B<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 a scalar context. Some operators return the
+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
@@ -209,10 +215,10 @@ C<dbmclose>, C<dbmopen>
=head2 Portability
-Perl was born in UNIX and therefore it can access all the common UNIX
-system calls. In non-UNIX environments the functionality of many
-UNIX system calls may not be available or the details of the available
-functionality may be slightly different. The Perl functions affected
+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:
C<-X>, C<binmode>, C<chmod>, C<chown>, C<chroot>, C<crypt>,
@@ -224,13 +230,13 @@ C<getppid>, C<getprgp>, C<getpriority>, C<getprotobynumber>,
C<getprotoent>, C<getpwent>, C<getpwnam>, C<getpwuid>,
C<getservbyport>, C<getservent>, C<getsockopt>, C<glob>, C<ioctl>,
C<kill>, C<link>, C<lstat>, C<msgctl>, C<msgget>, C<msgrcv>,
-C<msgsnd>, C<open>, C<pipe>, C<readlink>, C<select>, C<semctl>,
+C<msgsnd>, C<open>, C<pipe>, C<readlink>, C<rename>, C<select>, C<semctl>,
C<semget>, C<semop>, C<setgrent>, C<sethostent>, C<setnetent>,
C<setpgrp>, C<setpriority>, C<setprotoent>, C<setpwent>,
C<setservent>, C<setsockopt>, C<shmctl>, C<shmget>, C<shmread>,
-C<shmwrite>, C<socketpair>, C<stat>, C<symlink>, C<syscall>,
-C<sysopen>, C<system>, C<times>, C<truncate>, C<umask>, C<utime>,
-C<wait>, C<waitpid>
+C<shmwrite>, C<socket>, C<socketpair>, C<stat>, C<symlink>, C<syscall>,
+C<sysopen>, C<system>, C<times>, C<truncate>, C<umask>, C<unlink>,
+C<utime>, C<wait>, C<waitpid>
For more information about the portability of these functions, see
L<perlport> and other available platform-specific documentation.
@@ -307,15 +313,14 @@ reasons may be for example network filesystem access controls, ACLs
(access control lists), read-only filesystems, and unrecognized
executable formats.
-Also note that, for the superuser on the local filesystems, C<-r>,
-C<-R>, C<-w>, and C<-W> always return 1, and C<-x> and C<-X> return 1
+Also note that, for the superuser on the local filesystems, the C<-r>,
+C<-R>, C<-w>, and C<-W> tests always return 1, and C<-x> and C<-X> return 1
if any execute bit is set in the mode. Scripts run by the superuser
may thus need to do a stat() to determine the actual mode of the file,
-or temporarily set the uid to something else.
+or temporarily set their effective uid to something else.
If you are using ACLs, there is a pragma called C<filetest> that may
produce more accurate results than the bare stat() mode bits.
-
When under the C<use filetest 'access'> the above-mentioned filetests
will test whether the permission can (not) be granted using the
access() family of system calls. Also note that the C<-x> and C<-X> may
@@ -369,7 +374,7 @@ If VALUE is omitted, uses C<$_>.
Accepts an incoming socket connect, just as the accept(2) system call
does. Returns the packed address if it succeeded, FALSE otherwise.
-See example in L<perlipc/"Sockets: Client/Server Communication">.
+See the example in L<perlipc/"Sockets: Client/Server Communication">.
=item alarm SECONDS
@@ -386,8 +391,12 @@ starting a new one. The returned value is the amount of time remaining
on the previous timer.
For delays of finer granularity than one second, you may use Perl's
-C<syscall()> interface to access setitimer(2) if your system supports it,
-or else see L</select()>. It is usually a mistake to intermix C<alarm()>
+four-arugment version of select() leaving the first three arguments
+undefined, or you might be able to use the C<syscall()> interface to
+access setitimer(2) if your system supports it. The Time::HiRes module
+from CPAN may also prove useful.
+
+It is usually a mistake to intermix C<alarm()>
and C<sleep()> calls.
If you want to use C<alarm()> to time out a system call you need to use an
@@ -429,33 +438,40 @@ L<perlipc/"Sockets: Client/Server Communication">.
=item binmode FILEHANDLE
Arranges for the file to be read or written in "binary" mode in operating
-systems that distinguish between binary and text files. Files that are
-not in binary mode have CR LF sequences translated to LF on input and LF
-translated to CR LF on output. Binmode has no effect under Unix; in MS-DOS
-and similarly archaic systems, it may be imperative--otherwise your
-MS-DOS-damaged C library may mangle your file. The key distinction between
-systems that need C<binmode()> and those that don't is their text file
-formats. Systems like Unix, MacOS, and Plan9 that delimit lines with a single
-character, and that encode that character in C as C<"\n">, do not need
-C<binmode()>. The rest need it. If FILEHANDLE is an expression, the value
-is taken as the name of the filehandle.
+systems that distinguish between binary and text files. Files that
+are not in binary mode have CR LF sequences translated to LF on input
+and LF translated to CR LF on output. Binmode has no effect under
+many sytems, but in MS-DOS and similarly archaic systems, it may be
+imperative--otherwise your MS-DOS-damaged C library may mangle your file.
+The key distinction between systems that need C<binmode()> and those
+that don't is their text file formats. Systems like Unix, MacOS, and
+Plan9 that delimit lines with a single character, and that encode that
+character in C as C<"\n">, do not need C<binmode()>. The rest may need it.
+If FILEHANDLE is an expression, the value is taken as the name of the
+filehandle.
+
+If the system does care about it, using it when you shouldn't is just as
+perilous as failing to use it when you should. Fortunately for most of
+us, you can't go wrong using binmode() on systems that don't care about
+it, though.
=item bless REF,CLASSNAME
=item bless REF
-This function tells the thingy referenced by REF that it is now
-an object in the CLASSNAME package--or the current package if no CLASSNAME
-is specified, which is often the case. It returns the reference for
-convenience, because a C<bless()> is often the last thing in a constructor.
-Always use the two-argument version if the function doing the blessing
-might be inherited by a derived class. See L<perltoot> and L<perlobj>
-for more about the blessing (and blessings) of objects.
+This function tells the thingy referenced by REF that it is now an object
+in the CLASSNAME package. If CLASSNAME is omitted, the current package
+is used. Because a C<bless()> is often the last thing in a constructor.
+it returns the reference for convenience. Always use the two-argument
+version if the function doing the blessing might be inherited by a
+derived class. See L<perltoot> and L<perlobj> for more about the blessing
+(and blessings) of objects.
Consider always blessing objects in CLASSNAMEs that are mixed case.
-Namespaces with all lowercase names are considered reserved for Perl
-pragmata. Builtin types have all uppercase names, so to prevent confusion,
-it is best to avoid such package names as well.
+Namespaces with all lowercase names are considered reserved for
+Perl pragmata. Builtin types have all uppercase names, so to prevent
+confusion, you may wish to avoid such package names as well. Make sure
+that CLASSNAME is a true value.
See L<perlmod/"Perl Modules">.
@@ -498,9 +514,9 @@ previous time C<caller()> was called.
=item chdir EXPR
-Changes the working directory to EXPR, if possible. If EXPR is
-omitted, changes to home directory. Returns TRUE upon success, FALSE
-otherwise. See example under C<die()>.
+Changes the working directory to EXPR, if possible. If EXPR is omitted,
+changes to the user's home directory. Returns TRUE upon success,
+FALSE otherwise. See the example under C<die()>.
=item chmod LIST
@@ -523,14 +539,14 @@ successfully changed. See also L</oct>, if all you have is a string.
=item chomp
-This is a slightly safer version of L</chop>. It removes any
-line ending that corresponds to the current value of C<$/> (also known as
+This safer version of L</chop> removes any trailing string
+that corresponds to the current value of C<$/> (also known as
$INPUT_RECORD_SEPARATOR in the C<English> module). It returns the total
number of characters removed from all its arguments. It's often used to
remove the newline from the end of an input record when you're worried
-that the final record may be missing its newline. When in paragraph mode
-(C<$/ = "">), it removes all trailing newlines from the string. If
-VARIABLE is omitted, it chomps C<$_>. Example:
+that the final record may be missing its newline. When in paragraph
+mode (C<$/ = "">), it removes all trailing newlines from the string.
+If VARIABLE is omitted, it chomps C<$_>. Example:
while (<>) {
chomp; # avoid \n on last field
@@ -608,8 +624,9 @@ restrictions may be relaxed, but this is not a portable assumption.
Returns the character represented by that NUMBER in the character set.
For example, C<chr(65)> is C<"A"> in either ASCII or Unicode, and
-chr(0x263a) is a Unicode smiley face (but only within the scope of a
-C<use utf8>). For the reverse, use L</ord>.
+chr(0x263a) is a Unicode smiley face (but only within the scope of
+a C<use utf8>). For the reverse, use L</ord>.
+See L<utf8> for more about Unicode.
If NUMBER is omitted, uses C<$_>.
@@ -641,10 +658,10 @@ counter (C<$.>), while the implicit close done by C<open()> does not.
If the file handle came from a piped open C<close()> will additionally
return FALSE if one of the other system calls involved fails or if the
program exits with non-zero status. (If the only problem was that the
-program exited non-zero C<$!> will be set to C<0>.) Also, closing a pipe
-waits for the process executing on the pipe to complete, in case you
-want to look at the output of the pipe afterwards. Closing a pipe
-explicitly also puts the exit status value of the command into C<$?>.
+program exited non-zero C<$!> will be set to C<0>.) Closing a pipe
+also waits for the process executing on the pipe to complete, in case you
+want to look at the output of the pipe afterwards, and
+implicitly puts the exit status value of that command into C<$?>.
Example:
@@ -756,13 +773,13 @@ for it is unwise.
=item dbmclose HASH
-[This function has been superseded by the C<untie()> function.]
+[This function has been largely superseded by the C<untie()> function.]
Breaks the binding between a DBM file and a hash.
=item dbmopen HASH,DBNAME,MODE
-[This function has been superseded by the C<tie()> function.]
+[This function has been largely superseded by the C<tie()> function.]
This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or Berkeley DB file to a
hash. HASH is the name of the hash. (Unlike normal C<open()>, the first
@@ -795,6 +812,13 @@ See also L<AnyDBM_File> for a more general description of the pros and
cons of the various dbm approaches, as well as L<DB_File> for a particularly
rich implementation.
+You can control which DBM library you use by loading that library
+before you call dbmopen():
+
+ use DB_File;
+ dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
+ or die "Can't open netscape history file: $!";
+
=item defined EXPR
=item defined
@@ -839,7 +863,7 @@ defined values. For example, if you say
The pattern match succeeds, and C<$1> is defined, despite the fact that it
matched "nothing". But it didn't really match nothing--rather, it
-matched something that happened to be C<0> characters long. This is all
+matched something that happened to be zero characters long. This is all
very above-board and honest. When a function returns an undefined value,
it's an admission that it couldn't give you an honest answer. So you
should use C<defined()> only when you're questioning the integrity of what
@@ -885,9 +909,14 @@ And so does this:
delete @HASH{keys %HASH}
-(But both of these are slower than just assigning the empty list, or
-using C<undef()>.) Note that the EXPR can be arbitrarily complicated as
-long as the final operation is a hash element lookup or hash slice:
+But both of these are slower than just assigning the empty list
+or undefining it:
+
+ %hash = (); # completely empty %hash
+ undef %hash; # forget %hash every existed
+
+Note that the EXPR can be arbitrarily complicated as long as the final
+operation is a hash element lookup or hash slice:
delete $ref->[$x][$y]{$key};
delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys};
@@ -925,7 +954,7 @@ produce, respectively
/etc/games is no good at canasta line 123.
/etc/games is no good, stopped at canasta line 123.
-See also C<exit()> and C<warn()>.
+See also exit(), warn(), and the Carp module.
If LIST is empty and C<$@> already contains a value (typically from a
previous eval) that value is reused after appending C<"\t...propagated">.
@@ -942,13 +971,15 @@ will be called with the error text and can change the error message, if
it sees fit, by calling C<die()> again. See L<perlvar/$SIG{expr}> for details on
setting C<%SIG> entries, and L<"eval BLOCK"> for some examples.
-Note that the C<$SIG{__DIE__}> hook is called even inside eval()ed
-blocks/strings. If one wants the hook to do nothing in such
+Note that the C<$SIG{__DIE__}> hook is currently called even inside
+eval()ed blocks/strings! If one wants the hook to do nothing in such
situations, put
die @_ if $^S;
-as the first line of the handler (see L<perlvar/$^S>).
+as the first line of the handler (see L<perlvar/$^S>). Because this
+promotes action at a distance, this counterintuitive behavior may be fixed
+in a future release.
=item do BLOCK
@@ -958,7 +989,8 @@ modifier, executes the BLOCK once before testing the loop condition.
(On other statements the loop modifiers test the conditional first.)
C<do BLOCK> does I<not> count as a loop, so the loop control statements
-C<next>, C<last> or C<redo> cannot be used to leave or restart the block.
+C<next>, C<last>, or C<redo> cannot be used to leave or restart the block.
+See L<perlsyn> for alternative strategies.
=item do SUBROUTINE(LIST)
@@ -976,17 +1008,16 @@ is just like
scalar eval `cat stat.pl`;
-except that it's more efficient and concise, keeps track of the
-current filename for error messages, and searches all the B<-I>
-libraries if the file isn't in the current directory (see also the @INC
-array in L<perlvar/Predefined Names>). It is also different in how
-code evaluated with C<do FILENAME> doesn't see lexicals in the enclosing
-scope like C<eval STRING> does. It's the same, however, in that it does
-reparse the file every time you call it, so you probably don't want to
-do this inside a loop.
+except that it's more efficient and concise, keeps track of the current
+filename for error messages, searches the @INC libraries, and updates
+C<%INC> if the file is found. See L<perlvar/Predefined Names> for these
+variables. It also differs in that code evaluated with C<do FILENAME>
+cannot see lexicals in the enclosing scope; C<eval STRING> does. It's the
+same, however, in that it does reparse the file every time you call it,
+so you probably don't want to do this inside a loop.
If C<do> cannot read the file, it returns undef and sets C<$!> to the
-error. If C<do> can read the file but cannot compile it, it
+error. If C<do> can read the file but cannot compile it, it
returns undef and sets an error message in C<$@>. If the file is
successfully compiled, C<do> returns the value of the last expression
evaluated.
@@ -1000,7 +1031,8 @@ file. Manual error checking can be done this way:
# read in config files: system first, then user
for $file ("/share/prog/defaults.rc",
- "$ENV{HOME}/.someprogrc") {
+ "$ENV{HOME}/.someprogrc")
+ {
unless ($return = do $file) {
warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!" unless defined $return;
@@ -1094,11 +1126,11 @@ C<eof(FILEHANDLE)> on it) after end-of-file is reached. Filetypes such
as terminals may lose the end-of-file condition if you do.
An C<eof> without an argument uses the last file read as argument.
-Using C<eof()> with empty parentheses is very different. It indicates the pseudo file formed of
-the files listed on the command line, i.e., C<eof()> is reasonable to
-use inside a C<while (E<lt>E<gt>)> loop to detect the end of only the
-last file. Use C<eof(ARGV)> or eof without the parentheses to test
-I<EACH> file in a while (E<lt>E<gt>) loop. Examples:
+Using C<eof()> with empty parentheses is very different. It indicates
+the pseudo file formed of the files listed on the command line, i.e.,
+C<eof()> is reasonable to use inside a C<while (E<lt>E<gt>)> loop to
+detect the end of only the last file. Use C<eof(ARGV)> or eof without the
+parentheses to test I<EACH> file in a while (E<lt>E<gt>) loop. Examples:
# reset line numbering on each input file
while (<>) {
@@ -1112,7 +1144,7 @@ I<EACH> file in a while (E<lt>E<gt>) loop. Examples:
while (<>) {
if (eof()) { # check for end of current file
print "--------------\n";
- close(ARGV); # close or break; is needed if we
+ close(ARGV); # close or last; is needed if we
# are reading from the terminal
}
print;
@@ -1181,10 +1213,11 @@ Examples:
# a run-time error
eval '$answer ='; # sets $@
-When using the C<eval{}> form as an exception trap in libraries, you may
-wish not to trigger any C<__DIE__> hooks that user code may have
-installed. You can use the C<local $SIG{__DIE__}> construct for this
-purpose, as shown in this example:
+Due to the current arguably broken state of C<__DIE__> hooks, when using
+the C<eval{}> form as an exception trap in libraries, you may wish not
+to trigger any C<__DIE__> hooks that user code may have installed.
+You can use the C<local $SIG{__DIE__}> construct for this purpose,
+as shown in this example:
# a very private exception trap for divide-by-zero
eval { local $SIG{'__DIE__'}; $answer = $a / $b; };
@@ -1201,6 +1234,9 @@ C<die()> again, which has the effect of changing their error messages:
print $@ if $@; # prints "bar lives here"
}
+Because this promotes action at a distance, this counterintuive behavior
+may be fixed in a future release.
+
With an C<eval()>, you should be especially careful to remember what's
being looked at when:
@@ -1225,8 +1261,7 @@ particular situation, you can just use symbolic references instead, as
in case 6.
C<eval BLOCK> does I<not> count as a loop, so the loop control statements
-C<next>, C<last> or C<redo> cannot be used to leave or restart the block.
-
+C<next>, C<last>, or C<redo> cannot be used to leave or restart the block.
=item exec LIST
@@ -1285,9 +1320,9 @@ shell expanding wildcards or splitting up words with whitespace in them.
@args = ( "echo surprise" );
- system @args; # subject to shell escapes
+ exec @args; # subject to shell escapes
# if @args == 1
- system { $args[0] } @args; # safe even with one-arg list
+ exec { $args[0] } @args; # safe even with one-arg list
The first version, the one without the indirect object, ran the I<echo>
program, passing it C<"surprise"> an argument. The second version
@@ -1302,9 +1337,9 @@ any C<DESTROY> methods in your objects.
Returns TRUE if the specified hash key exists in its hash array, even
if the corresponding value is undefined.
- print "Exists\n" if exists $array{$key};
- print "Defined\n" if defined $array{$key};
- print "True\n" if $array{$key};
+ print "Exists\n" if exists $array{$key};
+ print "Defined\n" if defined $array{$key};
+ print "True\n" if $array{$key};
A hash element can be TRUE only if it's defined, and defined if
it exists, but the reverse doesn't necessarily hold true.
@@ -1312,40 +1347,53 @@ it exists, but the reverse doesn't necessarily hold true.
Note that the EXPR can be arbitrarily complicated as long as the final
operation is a hash key lookup:
- if (exists $ref->{"A"}{"B"}{$key}) { ... }
+ if (exists $ref->{A}->{B}->{$key}) { }
+ if (exists $hash{A}{B}{$key}) { }
+
+Although the last element will not spring into existence just because
+its existence was tested, intervening ones will. Thus C<$ref-E<gt>{"A"}>
+and C<$ref-E<gt>{"A"}-E<gt>{"B"}> will spring into existence due to the
+existence test for a $key element. This happens anywhere the arrow
+operator is used, including even
-Although the last element will not spring into existence just because its
-existence was tested, intervening ones will. Thus C<$ref-E<gt>{"A"}>
-C<$ref-E<gt>{"B"}> will spring into existence due to the existence
-test for a $key element. This autovivification may be fixed in a later
+ undef $ref;
+ if (exists $ref->{"Some key"}) { }
+ print $ref; # prints HASH(0x80d3d5c)
+
+This surprising autovivification in what does not at first--or even
+second--glance appear to be an lvalue context may be fixed in a future
release.
=item exit EXPR
-Evaluates EXPR and exits immediately with that value. (Actually, it
-calls any defined C<END> routines first, but the C<END> routines may not
-abort the exit. Likewise any object destructors that need to be called
-are called before exit.) Example:
+Evaluates EXPR and exits immediately with that value. Example:
$ans = <STDIN>;
exit 0 if $ans =~ /^[Xx]/;
See also C<die()>. If EXPR is omitted, exits with C<0> status. The only
-universally portable values for EXPR are C<0> for success and C<1> for error;
-all other values are subject to unpredictable interpretation depending
-on the environment in which the Perl program is running.
+universally recognized values for EXPR are C<0> for success and C<1>
+for error; other values are subject to interpretation depending on the
+environment in which the Perl program is running. For example, exiting
+69 (EX_UNAVAILABLE) from a I<sendmail> incoming-mail filter will cause
+the mailer to return the item undelivered, but that's not true everywhere.
-You shouldn't use C<exit()> to abort a subroutine if there's any chance that
+Don't use C<exit()> to abort a subroutine if there's any chance that
someone might want to trap whatever error happened. Use C<die()> instead,
which can be trapped by an C<eval()>.
-All C<END{}> blocks are run at exit time. See L<perlsub> for details.
+The exit() function does not always exit immediately. It calls any
+defined C<END> routines first, but these C<END> routines may not
+themselves abort the exit. Likewise any object destructors that need to
+be called are called before the real exit. If this is a problem, you
+can call C<POSIX:_exit($status)> to avoid END and destructor processing.
+See L<perlsub> for details.
=item exp EXPR
=item exp
-Returns I<e> (the natural logarithm base) to the power of EXPR.
+Returns I<e> (the natural logarithm base) to the power of EXPR.
If EXPR is omitted, gives C<exp($_)>.
=item fcntl FILEHANDLE,FUNCTION,SCALAR
@@ -1362,22 +1410,23 @@ For example:
fcntl($filehandle, F_GETFL, $packed_return_buffer)
or die "can't fcntl F_GETFL: $!";
-You don't have to check for C<defined()> on the return from
-C<fnctl()>. Like C<ioctl()>, it maps a C<0> return from the system
-call into "C<0> but true" in Perl. This string is true in
-boolean context and C<0> in numeric context. It is also
-exempt from the normal B<-w> warnings on improper numeric
-conversions.
+You don't have to check for C<defined()> on the return from C<fnctl()>.
+Like C<ioctl()>, it maps a C<0> return from the system call into "C<0>
+but true" in Perl. This string is true in boolean context and C<0>
+in numeric context. It is also exempt from the normal B<-w> warnings
+on improper numeric conversions.
Note that C<fcntl()> will produce a fatal error if used on a machine that
-doesn't implement fcntl(2).
+doesn't implement fcntl(2). See the Fcntl module or your fcntl(2)
+manpage to learn what functions are available on your system.
=item fileno FILEHANDLE
-Returns the file descriptor for a filehandle. This is useful for
-constructing bitmaps for C<select()> and low-level POSIX tty-handling
-operations. If FILEHANDLE is an expression, the value is taken as
-an indirect filehandle, generally its name.
+Returns the file descriptor for a filehandle, or undefined if the
+filehandle is not open. This is mainly useful for constructing
+bitmaps for C<select()> and low-level POSIX tty-handling operations.
+If FILEHANDLE is an expression, the value is taken as an indirect
+filehandle, generally its name.
You can use this to find out whether two handles refer to the
same underlying descriptor:
@@ -1388,18 +1437,23 @@ same underlying descriptor:
=item flock FILEHANDLE,OPERATION
-Calls flock(2), or an emulation of it, on FILEHANDLE. Returns TRUE for
-success, FALSE on failure. Produces a fatal error if used on a machine
-that doesn't implement flock(2), fcntl(2) locking, or lockf(3). C<flock()>
-is Perl's portable file locking interface, although it locks only entire
-files, not records.
-
-On many platforms (including most versions or clones of Unix), locks
-established by C<flock()> are B<merely advisory>. Such discretionary locks
-are more flexible, but offer fewer guarantees. This means that files
-locked with C<flock()> may be modified by programs that do not also use
-C<flock()>. Windows NT and OS/2 are among the platforms which
-enforce mandatory locking. See your local documentation for details.
+Calls flock(2), or an emulation of it, on FILEHANDLE. Returns TRUE
+for success, FALSE on failure. Produces a fatal error if used on a
+machine that doesn't implement flock(2), fcntl(2) locking, or lockf(3).
+C<flock()> is Perl's portable file locking interface, although it locks
+only entire files, not records.
+
+Two potentially non-obvious but traditional C<flock> semantics are
+that it waits indefinitely until the lock is granted, and that its locks
+B<merely advisory>. Such discretionary locks are more flexible, but offer
+fewer guarantees. This means that files locked with C<flock()> may be
+modified by programs that do not also use C<flock()>. See L<perlport>,
+your port's specific documentation, or your system-specific local manpages
+for details. It's best to assume traditional behavior if you're writing
+portable programs. (But if you're not, you should as always feel perfectly
+free to write for your own system's idiosyncrasies (sometimes called
+"features"). Slavish adherence to portability concerns shouldn't get
+in the way of your getting your job done.)
OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with
LOCK_NB. These constants are traditionally valued 1, 2, 8 and 4, but
@@ -1410,12 +1464,12 @@ releases a previously requested lock. If LOCK_NB is added to LOCK_SH or
LOCK_EX then C<flock()> will return immediately rather than blocking
waiting for the lock (check the return status to see if you got it).
-To avoid the possibility of mis-coordination, Perl flushes FILEHANDLE
-before (un)locking it.
+To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE
+before locking or unlocking it.
Note that the emulation built with lockf(3) doesn't provide shared
locks, and it requires that FILEHANDLE be open with write intent. These
-are the semantics that lockf(3) implements. Most (all?) systems
+are the semantics that lockf(3) implements. Most if not all systems
implement lockf(3) in terms of fcntl(2) locking, though, so the
differing semantics shouldn't bite too many people.
@@ -1448,44 +1502,37 @@ Here's a mailbox appender for BSD systems.
print MBOX $msg,"\n\n";
unlock();
+On systems that support a real flock(), locks are inherited across fork()
+calls, whereas those that must resort to the more capricious fcntl()
+function lose the locks, making it harder to write servers.
+
See also L<DB_File> for other flock() examples.
=item fork
-Does a fork(2) system call. Returns the child pid to the parent process,
-C<0> to the child process, or C<undef> if the fork is unsuccessful.
+Does a fork(2) system call to create a new process running the
+same program at the same point. It returns the child pid to the
+parent process, C<0> to the child process, or C<undef> if the fork is
+unsuccessful. File descriptors (and sometimes locks on those descriptors)
+are shared, while everything else is copied. On most systems supporting
+fork(), great care has gone into making it extremely efficient (for
+example, using copy-on-write technology on data pages), making it the
+dominant paradigm for multitasking over the last few decades.
Note: unflushed buffers remain unflushed in both processes, which means
you may need to set C<$|> ($AUTOFLUSH in English) or call the C<autoflush()>
method of C<IO::Handle> to avoid duplicate output.
-If you C<fork()> without ever waiting on your children, you will accumulate
-zombies:
-
- $SIG{CHLD} = sub { wait };
-
-There's also the double-fork trick (error checking on
-C<fork()> returns omitted);
-
- unless ($pid = fork) {
- unless (fork) {
- exec "what you really wanna do";
- die "no exec";
- # ... or ...
- ## (some_perl_code_here)
- exit 0;
- }
- exit 0;
- }
- waitpid($pid,0);
-
-See also L<perlipc> for more examples of forking and reaping
-moribund children.
+If you C<fork()> without ever waiting on your children, you will
+accumulate zombies. On some systems, you can avoid this by setting
+C<$SIG{CHLD}> to C<"IGNORE">. See also L<perlipc> for more examples of
+forking and reaping moribund children.
Note that if your forked child inherits system file descriptors like
STDIN and STDOUT that are actually connected by a pipe or socket, even
-if you exit, then the remote server (such as, say, httpd or rsh) won't think
-you're done. You should reopen those to F</dev/null> if it's any issue.
+if you exit, then the remote server (such as, say, a CGI script or a
+backgrounded job launced from a remote shell) won't think you're done.
+You should reopen those to F</dev/null> if it's any issue.
=item format
@@ -1528,10 +1575,11 @@ C<formline()> always returns TRUE. See L<perlform> for other examples.
=item getc
Returns the next character from the input file attached to FILEHANDLE,
-or the undefined value at end of file, or if there was an error. If
-FILEHANDLE is omitted, reads from STDIN. This is not particularly
-efficient. It cannot be used to get unbuffered single-characters,
-however. For that, try something more like:
+or the undefined value at end of file, or if there was an error.
+If FILEHANDLE is omitted, reads from STDIN. This is not particularly
+efficient. However, it cannot be used by itself to fetch single
+characters without waiting for the user to hit enter. For that, try
+something more like:
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
@@ -1553,10 +1601,10 @@ however. For that, try something more like:
Determination of whether $BSD_STYLE should be set
is left as an exercise to the reader.
-The C<POSIX::getattr()> function can do this more portably on systems
-purporting POSIX compliance.
-See also the C<Term::ReadKey> module from your nearest CPAN site;
-details on CPAN can be found on L<perlmodlib/CPAN>.
+The C<POSIX::getattr()> function can do this more portably on
+systems purporting POSIX compliance. See also the C<Term::ReadKey>
+module from your nearest CPAN site; details on CPAN can be found on
+L<perlmodlib/CPAN>.
=item getlogin
@@ -1684,21 +1732,26 @@ lookup by name, in which case you get the other thing, whatever it is.
$name = getgrent();
#etc.
-In I<getpw*()> the fields C<$quota>, C<$comment>, and C<$expire> are special
-cases in the sense that in many systems they are unsupported. If the
-C<$quota> is unsupported, it is an empty scalar. If it is supported, it
-usually encodes the disk quota. If the C<$comment> field is unsupported,
-it is an empty scalar. If it is supported it usually encodes some
-administrative comment about the user. In some systems the $quota
-field may be C<$change> or C<$age>, fields that have to do with password
-aging. In some systems the C<$comment> field may be C<$class>. The C<$expire>
-field, if present, encodes the expiration period of the account or the
-password. For the availability and the exact meaning of these fields
-in your system, please consult your getpwnam(3) documentation and your
-F<pwd.h> file. You can also find out from within Perl which meaning
-your C<$quota> and C<$comment> fields have and whether you have the C<$expire>
-field by using the C<Config> module and the values C<d_pwquota>, C<d_pwage>,
-C<d_pwchange>, C<d_pwcomment>, and C<d_pwexpire>.
+In I<getpw*()> the fields C<$quota>, C<$comment>, and C<$expire> are
+special cases in the sense that in many systems they are unsupported.
+If the C<$quota> is unsupported, it is an empty scalar. If it is
+supported, it usually encodes the disk quota. If the C<$comment>
+field is unsupported, it is an empty scalar. If it is supported it
+usually encodes some administrative comment about the user. In some
+systems the $quota field may be C<$change> or C<$age>, fields that have
+to do with password aging. In some systems the C<$comment> field may
+be C<$class>. The C<$expire> field, if present, encodes the expiration
+period of the account or the password. For the availability and the
+exact meaning of these fields in your system, please consult your
+getpwnam(3) documentation and your F<pwd.h> file. You can also find
+out from within Perl what your C<$quota> and C<$comment> fields mean
+and whether you have the C<$expire> field by using the C<Config> module
+and the values C<d_pwquota>, C<d_pwage>, C<d_pwchange>, C<d_pwcomment>,
+and C<d_pwexpire>. Shadow password files are only supported if your
+vendor has implemented them in the intuitive fashion that calling the
+regular C library routines gets the shadow versions if you're running
+under privilege. Those that incorrectly implement a separate library
+call are not supported.
The C<$members> value returned by I<getgr*()> is a space separated list of
the login names of the members of the group.
@@ -1712,6 +1765,15 @@ by saying something like:
($a,$b,$c,$d) = unpack('C4',$addr[0]);
+The Socket library makes this slightly easier:
+
+ use Socket;
+ $iaddr = inet_aton("127.1"); # or whatever address
+ $name = gethostbyaddr($iaddr, AF_INET);
+
+ # or going the other way
+ $straddr = inet_ntoa($iaddr");
+
If you get tired of remembering which element of the return list contains
which return value, by-name interfaces are also provided in modules:
C<File::stat>, C<Net::hostent>, C<Net::netent>, C<Net::protoent>, C<Net::servent>,
@@ -1742,11 +1804,11 @@ Returns the socket option requested, or undef if there is an error.
=item glob
-Returns the value of EXPR with filename expansions such as the standard Unix shell F</bin/sh> would
-do. This is the internal function implementing the C<E<lt>*.cE<gt>>
-operator, but you can use it directly. If EXPR is omitted, C<$_> is used.
-The C<E<lt>*.cE<gt>> operator is discussed in more detail in
-L<perlop/"I/O Operators">.
+Returns the value of EXPR with filename expansions such as the
+standard Unix shell F</bin/csh> would do. This is the internal function
+implementing the C<E<lt>*.cE<gt>> operator, but you can use it directly.
+If EXPR is omitted, C<$_> is used. The C<E<lt>*.cE<gt>> operator is
+discussed in more detail in L<perlop/"I/O Operators">.
=item gmtime EXPR
@@ -1759,9 +1821,12 @@ Typically used as follows:
gmtime(time);
All array elements are numeric, and come straight out of a struct tm.
-In particular this means that C<$mon> has the range C<0..11> and C<$wday> has
-the range C<0..6> with sunday as day C<0>. Also, C<$year> is the number of
-years since 1900, that is, C<$year> is C<123> in year 2023, I<not> simply the last two digits of the year.
+In particular this means that C<$mon> has the range C<0..11> and C<$wday>
+has the range C<0..6> with sunday as day C<0>. Also, C<$year> is the
+number of years since 1900, that is, C<$year> is C<123> in year 2023,
+I<not> simply the last two digits of the year. If you assume it is,
+then you create non-Y2K-compliant programs--and you wouldn't want to do
+that, would you?
If EXPR is omitted, does C<gmtime(time())>.
@@ -1772,18 +1837,19 @@ In scalar context, returns the ctime(3) value:
Also see the C<timegm()> function provided by the C<Time::Local> module,
and the strftime(3) function available via the POSIX module.
-This scalar value is B<not> locale dependent, see L<perllocale>, but
-instead a Perl builtin. Also see the C<Time::Local> module, and the
-strftime(3) and mktime(3) function available via the POSIX module. To
+This scalar value is B<not> locale dependent (see L<perllocale>), but
+is instead a Perl builtin. Also see the C<Time::Local> module, and the
+strftime(3) and mktime(3) functions available via the POSIX module. To
get somewhat similar but locale dependent date strings, set up your
locale environment variables appropriately (please see L<perllocale>)
and try for example:
use POSIX qw(strftime);
- $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
+ $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
-Note that the C<%a> and C<%b>, the short forms of the day of the week
-and the month of the year, may not necessarily be three characters wide.
+Note that the C<%a> and C<%b> escapes, which represent the short forms
+of the day of the week and the month of the year, may not necessarily
+be three characters wide in all locales.
=item goto LABEL
@@ -1819,13 +1885,12 @@ will be able to tell that this routine was called first.
=item grep EXPR,LIST
-This is similar in spirit to, but not the same as, grep(1)
-and its relatives. In particular, it is not limited to using
-regular expressions.
+This is similar in spirit to, but not the same as, grep(1) and its
+relatives. In particular, it is not limited to using regular expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally setting
C<$_> to each element) and returns the list value consisting of those
-elements for which the expression evaluated to TRUE. In a scalar
+elements for which the expression evaluated to TRUE. In scalar
context, returns the number of times the expression was TRUE.
@foo = grep(!/^#/, @bar); # weed out comments
@@ -1834,14 +1899,14 @@ or equivalently,
@foo = grep {!/^#/} @bar; # weed out comments
-Note that, because C<$_> is a reference into the list value, it can be used
-to modify the elements of the array. While this is useful and
-supported, it can cause bizarre results if the LIST is not a named
-array. Similarly, grep returns aliases into the original list,
-much like the way that a for loop's index variable aliases the list
-elements. That is, modifying an element of a list returned by grep
-(for example, in a C<foreach>, C<map()> or another C<grep()>)
-actually modifies the element in the original list.
+Note that, because C<$_> is a reference into the list value, it can
+be used to modify the elements of the array. While this is useful and
+supported, it can cause bizarre results if the LIST is not a named array.
+Similarly, grep returns aliases into the original list, much as a for
+loop's index variable aliases the list elements. That is, modifying an
+element of a list returned by grep (for example, in a C<foreach>, C<map()>
+or another C<grep()>) actually modifies the element in the original list.
+This is usually something to be avoided when writing clear code.
See also L</map> for an array composed of the results of the BLOCK or EXPR.
@@ -1849,9 +1914,9 @@ See also L</map> for an array composed of the results of the BLOCK or EXPR.
=item hex
-Interprets EXPR as a hex string and returns the corresponding
-value. (To convert strings that might start with either 0 or 0x
-see L</oct>.) If EXPR is omitted, uses C<$_>.
+Interprets EXPR as a hex string and returns the corresponding value.
+(To convert strings that might start with either 0, 0x, or 0b, see
+L</oct>.) If EXPR is omitted, uses C<$_>.
print hex '0xAf'; # prints '175'
print hex 'aF'; # same
@@ -1867,29 +1932,34 @@ for the package used. See also L</use()>, L<perlmod>, and L<Exporter>.
=item index STR,SUBSTR
-Returns the position of the first occurrence of SUBSTR in STR at or after
-POSITION. If POSITION is omitted, starts searching from the beginning of
-the string. The return value is based at C<0> (or whatever you've set the C<$[>
-variable to--but don't do that). If the substring is not found, returns
-one less than the base, ordinarily C<-1>.
+The index function searches for one string within another, but without
+the wildcard-like behavior of a full regular-expression pattern match.
+It returns the position of the first occurrence of SUBSTR in STR at
+or after POSITION. If POSITION is omitted, starts searching from the
+beginning of the string. The return value is based at C<0> (or whatever
+you've set the C<$[> variable to--but don't do that). If the substring
+is not found, returns one less than the base, ordinarily C<-1>.
=item int EXPR
=item int
Returns the integer portion of EXPR. If EXPR is omitted, uses C<$_>.
-You should not use this for rounding, because it truncates
-towards C<0>, and because machine representations of floating point
-numbers can sometimes produce counterintuitive results. Usually C<sprintf()> or C<printf()>,
-or the C<POSIX::floor> or C<POSIX::ceil> functions, would serve you better.
+You should not use this function for rounding: one because it truncates
+towards C<0>, and two because machine representations of floating point
+numbers can sometimes produce counterintuitive results. For example,
+C<int(-6.725/0.025)> produces -268 rather than the correct -269; that's
+because it's really more like -268.99999999999994315658 instead. Usually,
+the C<sprintf()>, C<printf()>, or the C<POSIX::floor> and C<POSIX::ceil>
+functions will serve you better than will int().
=item ioctl FILEHANDLE,FUNCTION,SCALAR
-Implements the ioctl(2) function. You'll probably have to say
+Implements the ioctl(2) function. You'll probably first have to say
require "ioctl.ph"; # probably in /usr/local/lib/perl/ioctl.ph
-first to get the correct function definitions. If F<ioctl.ph> doesn't
+to get the correct function definitions. If F<ioctl.ph> doesn't
exist or doesn't have the correct definitions you'll have to roll your
own, based on your C header files such as F<E<lt>sys/ioctl.hE<gt>>.
(There is a Perl script called B<h2ph> that comes with the Perl kit that
@@ -1925,7 +1995,7 @@ Thus Perl returns TRUE on success and FALSE on failure, yet you can
still easily determine the actual value returned by the operating
system:
- ($retval = ioctl(...)) || ($retval = -1);
+ $retval = ioctl(...) || -1;
printf "System returned %d\n", $retval;
The special string "C<0> but true" is exempt from B<-w> complaints
@@ -1933,11 +2003,10 @@ about improper numeric conversions.
=item join EXPR,LIST
-Joins the separate strings of LIST into a single string with
-fields separated by the value of EXPR, and returns the string.
-Example:
+Joins the separate strings of LIST into a single string with fields
+separated by the value of EXPR, and returns that new string. Example:
- $_ = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
+ $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
See L</split>.
@@ -2019,7 +2088,8 @@ C<continue> block, if any, is not executed:
}
C<last> cannot be used to exit a block which returns a value such as
-C<eval {}>, C<sub {}> or C<do {}>.
+C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit
+a grep() or map() operation.
See also L</continue> for an illustration of how C<last>, C<next>, and
C<redo> work.
@@ -2049,29 +2119,31 @@ If EXPR is omitted, uses C<$_>.
=item length
Returns the length in characters of the value of EXPR. If EXPR is
-omitted, returns length of C<$_>.
+omitted, returns length of C<$_>. Note that this cannot be used on
+an entire array or hash to find out how many elements these have.
+For that, use C<scalar @array> and C<scalar keys %hash> respectively.
=item link OLDFILE,NEWFILE
Creates a new filename linked to the old filename. Returns TRUE for
-success, FALSE otherwise.
+success, FALSE otherwise.
=item listen SOCKET,QUEUESIZE
Does the same thing that the listen system call does. Returns TRUE if
-it succeeded, FALSE otherwise. See example in L<perlipc/"Sockets: Client/Server Communication">.
+it succeeded, FALSE otherwise. See the example in L<perlipc/"Sockets: Client/Server Communication">.
=item local EXPR
+You really probably want to be using C<my()> instead, because C<local()> isn't
+what most people think of as "local". See L<perlsub/"Private Variables
+via my()"> for details.
+
A local modifies the listed variables to be local to the enclosing
block, file, or eval. If more than one value is listed, the list must
be placed in parentheses. See L<perlsub/"Temporary Values via local()">
for details, including issues with tied arrays and hashes.
-You really probably want to be using C<my()> instead, because C<local()> isn't
-what most people think of as "local". See L<perlsub/"Private Variables
-via my()"> for details.
-
=item localtime EXPR
Converts a time as returned by the time function to a 9-element array
@@ -2083,9 +2155,12 @@ follows:
localtime(time);
All array elements are numeric, and come straight out of a struct tm.
-In particular this means that C<$mon> has the range C<0..11> and C<$wday> has
-the range C<0..6> with sunday as day C<0>. Also, C<$year> is the number of
-years since 1900, that is, C<$year> is C<123> in year 2023, and I<not> simply the last two digits of the year.
+In particular this means that C<$mon> has the range C<0..11> and C<$wday>
+has the range C<0..6> with sunday as day C<0>. Also, C<$year> is the
+number of years since 1900, that is, C<$year> is C<123> in year 2023,
+and I<not> simply the last two digits of the year. If you assume it is,
+then you create non-Y2K-compliant programs--and you wouldn't want to do
+that, would you?
If EXPR is omitted, uses the current time (C<localtime(time)>).
@@ -2101,7 +2176,7 @@ locale environment variables appropriately (please see L<perllocale>)
and try for example:
use POSIX qw(strftime);
- $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
+ $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
Note that the C<%a> and C<%b>, the short forms of the day of the week
and the month of the year, may not necessarily be three characters wide.
@@ -2110,8 +2185,17 @@ and the month of the year, may not necessarily be three characters wide.
=item log
-Returns the natural logarithm (base I<e>) of EXPR. If EXPR is omitted, returns log
-of C<$_>.
+Returns the natural logarithm (base I<e>) of EXPR. If EXPR is omitted,
+returns log of C<$_>. To get the log of another base, use basic algebra:
+The base-N log of a number is is equal to the natural log of that number
+divided by the natural log of N. For example:
+
+ sub log10 {
+ my $n = shift;
+ return log($n)/log(10);
+ }
+
+See also L</exp> for the inverse operation.
=item lstat FILEHANDLE
@@ -2152,11 +2236,12 @@ is just a funny way to write
$hash{getkey($_)} = $_;
}
-Note that, because C<$_> is a reference into the list value, it can be used
-to modify the elements of the array. While this is useful and
-supported, it can cause bizarre results if the LIST is not a named
-array. See also L</grep> for an array composed of those items of the
-original list for which the BLOCK or EXPR evaluates to true.
+Note that, because C<$_> is a reference into the list value, it can
+be used to modify the elements of the array. While this is useful and
+supported, it can cause bizarre results if the LIST is not a named array.
+Using a regular C<foreach> loop for this purpose would be clearer in
+most cases. See also L</grep> for an array composed of those items of
+the original list for which the BLOCK or EXPR evaluates to true.
=item mkdir FILENAME,MODE
@@ -2230,7 +2315,8 @@ executed even on discarded lines. If the LABEL is omitted, the command
refers to the innermost enclosing loop.
C<next> cannot be used to exit a block which returns a value such as
-C<eval {}>, C<sub {}> or C<do {}>.
+C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit
+a grep() or map() operation.
See also L</continue> for an illustration of how C<last>, C<next>, and
C<redo> work.
@@ -2266,7 +2352,8 @@ name of the real filehandle wanted. If EXPR is omitted, the scalar
variable of the same name as the FILEHANDLE contains the filename.
(Note that lexical variables--those declared with C<my()>--will not work
for this purpose; so if you're using C<my()>, specify EXPR in your call
-to open.)
+to open.) See L<perlopentut> for a kinder, gentler explanation of opening
+files.
If the filename begins with C<'E<lt>'> or nothing, the file is opened for input.
If the filename begins with C<'E<gt>'>, the file is truncated and opened for
@@ -2388,7 +2475,6 @@ STDERR:
print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
-
If you specify C<'E<lt>&=N'>, where C<N> is a number, then Perl will do an
equivalent of C's C<fdopen()> of that file descriptor; this is more
parsimonious of file descriptors. For example:
@@ -2484,6 +2570,7 @@ DIRHANDLEs have their own namespace separate from FILEHANDLEs.
Returns the numeric (ASCII or Unicode) value of the first character of EXPR. If
EXPR is omitted, uses C<$_>. For the reverse, see L</chr>.
+See L<utf8> for more about Unicode.
=item pack TEMPLATE,LIST
@@ -2567,7 +2654,7 @@ left.
=item *
-The C<"a">, C<"A"> and C<"Z"> types gobble just one value, but pack it as a
+The C<"a">, C<"A">, and C<"Z"> types gobble just one value, but pack it as a
string of length count, padding with nulls or spaces as necessary. When
unpacking, C<"A"> strips trailing spaces and nulls, C<"Z"> strips everything
after the first null, and C<"a"> returns data verbatim.
@@ -2718,18 +2805,20 @@ The same template may generally also be used in unpack().
=item package NAMESPACE
Declares the compilation unit as being in the given namespace. The scope
-of the package declaration is from the declaration itself through the end of
-the enclosing block (the same scope as the C<local()> operator). All further
-unqualified dynamic identifiers will be in this namespace. A package
-statement affects only dynamic variables--including those you've used
-C<local()> on--but I<not> lexical variables created with C<my()>. Typically it
-would be the first declaration in a file to be included by the C<require>
-or C<use> operator. You can switch into a package in more than one place;
-it merely influences which symbol table is used by the compiler for the
-rest of that block. You can refer to variables and filehandles in other
-packages by prefixing the identifier with the package name and a double
-colon: C<$Package::Variable>. If the package name is null, the C<main>
-package as assumed. That is, C<$::sail> is equivalent to C<$main::sail>.
+of the package declaration is from the declaration itself through the end
+of the enclosing block, file, or eval (the same as the C<my()> operator).
+All further unqualified dynamic identifiers will be in this namespace.
+A package statement affects only dynamic variables--including those
+you've used C<local()> on--but I<not> lexical variables, which are created
+with C<my()>. Typically it would be the first declaration in a file to
+be included by the C<require> or C<use> operator. You can switch into a
+package in more than one place; it merely influences which symbol table
+is used by the compiler for the rest of that block. You can refer to
+variables and filehandles in other packages by prefixing the identifier
+with the package name and a double colon: C<$Package::Variable>.
+If the package name is null, the C<main> package as assumed. That is,
+C<$::sail> is equivalent to C<$main::sail> (as well as to C<$main'sail>,
+still seen in older code).
If NAMESPACE is omitted, then there is no current package, and all
identifiers must be fully qualified or lexicals. This is stricter
@@ -2758,14 +2847,13 @@ See L<perlvar/$^F>.
=item pop
Pops and returns the last value of the array, shortening the array by
-1. Has a similar effect to
+one element. Has a similar effect to
$tmp = $ARRAY[$#ARRAY--];
If there are no elements in the array, returns the undefined value.
-If ARRAY is omitted, pops the
-C<@ARGV> array in the main program, and the C<@_> array in subroutines, just
-like C<shift()>.
+If ARRAY is omitted, pops the C<@ARGV> array in the main program, and
+the C<@_> array in subroutines, just like C<shift()>.
=item pos SCALAR
@@ -2785,20 +2873,20 @@ L<perlop>.
Prints a string or a comma-separated list of strings. Returns TRUE
if successful. FILEHANDLE may be a scalar variable name, in which case
-the variable contains the name of or a reference to the filehandle, thus introducing one
-level of indirection. (NOTE: If FILEHANDLE is a variable and the next
-token is a term, it may be misinterpreted as an operator unless you
-interpose a C<+> or put parentheses around the arguments.) If FILEHANDLE is
-omitted, prints by default to standard output (or to the last selected
-output channel--see L</select>). If LIST is also omitted, prints C<$_> to
-the currently selected output channel. To set the default output channel to something other than
-STDOUT use the select operation. Note that, because print takes a
-LIST, anything in the LIST is evaluated in list context, and any
-subroutine that you call will have one or more of its expressions
-evaluated in list context. Also be careful not to follow the print
-keyword with a left parenthesis unless you want the corresponding right
-parenthesis to terminate the arguments to the print--interpose a C<+> or
-put parentheses around all the arguments.
+the variable contains the name of or a reference to the filehandle, thus
+introducing one level of indirection. (NOTE: If FILEHANDLE is a variable
+and the next token is a term, it may be misinterpreted as an operator
+unless you interpose a C<+> or put parentheses around the arguments.)
+If FILEHANDLE is omitted, prints by default to standard output (or to the
+last selected output channel--see L</select>). If LIST is also omitted,
+prints C<$_> to the currently selected output channel. To set the default
+output channel to something other than STDOUT use the select operation.
+Note that, because print takes a LIST, anything in the LIST is evaluated
+in list context, and any subroutine that you call will have one or
+more of its expressions evaluated in list context. Also be careful
+not to follow the print keyword with a left parenthesis unless you want
+the corresponding right parenthesis to terminate the arguments to the
+print--interpose a C<+> or put parentheses around all the arguments.
Note that if you're storing FILEHANDLES in an array or other expression,
you will have to use a block returning its value instead:
@@ -2826,12 +2914,12 @@ Returns the prototype of a function as a string (or C<undef> if the
function has no prototype). FUNCTION is a reference to, or the name of,
the function whose prototype you want to retrieve.
-If FUNCTION is a string starting with C<CORE::>, the rest is taken as
-a name for Perl builtin. If builtin is not I<overridable> (such as
+If FUNCTION is a string starting with C<CORE::>, the rest is taken as a
+name for Perl builtin. If the builtin is not I<overridable> (such as
C<qw//>) or its arguments cannot be expressed by a prototype (such as
-C<system()>) - in other words, the builtin does not behave like a Perl
-function - returns C<undef>. Otherwise, the string describing the
-equivalent prototype is returned.
+C<system()>) returns C<undef> because the builtin does not really behave
+like a Perl function. Otherwise, the string describing the equivalent
+prototype is returned.
=item push ARRAY,LIST
@@ -2919,9 +3007,9 @@ reads until end-of-file is reached and returns a list of lines. Note that
the notion of "line" used here is however you may have defined it
with C<$/> or C<$INPUT_RECORD_SEPARATOR>). See L<perlvar/"$/">.
-When C<$/> is set to C<undef> and when readline() is in a scalar
-context (i.e. file slurp mode), it returns C<''> the first time,
-followed by C<undef> subsequently.
+When C<$/> is set to C<undef>, when readline() is in scalar
+context (i.e. file slurp mode), and when an empty file is read, it
+returns C<''> the first time, followed by C<undef> subsequently.
This is the internal function implementing the C<E<lt>EXPRE<gt>>
operator, but you can use it directly. The C<E<lt>EXPRE<gt>>
@@ -2988,7 +3076,8 @@ themselves about what was just input:
}
C<redo> cannot be used to retry a block which returns a value such as
-C<eval {}>, C<sub {}> or C<do {}>.
+C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit
+a grep() or map() operation.
See also L</continue> for an illustration of how C<last>, C<next>, and
C<redo> work.
@@ -3015,16 +3104,24 @@ name is returned instead. You can think of C<ref()> as a C<typeof()> operator.
if (ref($r) eq "HASH") {
print "r is a reference to a hash.\n";
}
- if (!ref($r)) {
+ unless (ref($r)) {
print "r is not a reference at all.\n";
}
+ if (UNIVERSAL::isa($r, "HASH")) { # for subclassing
+ print "r is a reference to something that isa hash.\n";
+ }
See also L<perlref>.
=item rename OLDNAME,NEWNAME
-Changes the name of a file. Returns C<1> for success, C<0> otherwise. Will
-not work across file system boundaries.
+Changes the name of a file. Returns C<1> for success, C<0> otherwise.
+Behavior of this function varies wildly depending on your system
+implementation. For example, it will usually not work across file system
+boundaries, even though the system I<mv> command sometimes compensates
+for this. Other restrictions include whether it works on directories,
+open files, or pre-existing files. Check L<perlport> and either the
+rename(2) manpage or equivalent system documentation for details.
=item require EXPR
@@ -3107,12 +3204,13 @@ only variables or searches in the current package. Always returns
reset 'X'; # reset all X variables
reset 'a-z'; # reset lower case variables
- reset; # just reset ?? searches
+ reset; # just reset ?one-time? searches
Resetting C<"A-Z"> is not recommended because you'll wipe out your
-C<@ARGV> and C<@INC> arrays and your C<%ENV> hash. Resets only package variables--lexical variables
-are unaffected, but they clean themselves up on scope exit anyway,
-so you'll probably want to use them instead. See L</my>.
+C<@ARGV> and C<@INC> arrays and your C<%ENV> hash. Resets only package
+variables--lexical variables are unaffected, but they clean themselves
+up on scope exit anyway, so you'll probably want to use them instead.
+See L</my>.
=item return EXPR
@@ -3122,17 +3220,18 @@ Returns from a subroutine, C<eval()>, or C<do FILE> with the value
given in EXPR. Evaluation of EXPR may be in list, scalar, or void
context, depending on how the return value will be used, and the context
may vary from one execution to the next (see C<wantarray()>). If no EXPR
-is given, returns an empty list in list context, an undefined value in
-scalar context, or nothing in a void context.
+is given, returns an empty list in list context, the undefined value in
+scalar context, and (of course) nothing at all in a void context.
-(Note that in the absence of a return, a subroutine, eval, or do FILE
-will automatically return the value of the last expression evaluated.)
+(Note that in the absence of a explicit C<return>, a subroutine, eval,
+or do FILE will automatically return the value of the last expression
+evaluated.)
=item reverse LIST
In list context, returns a list value consisting of the elements
of LIST in the opposite order. In scalar context, concatenates the
-elements of LIST, and returns a string value with all the characters
+elements of LIST and returns a string value with all characters
in the opposite order.
print reverse <>; # line tac, last line first
@@ -3144,7 +3243,7 @@ This operator is also handy for inverting a hash, although there are some
caveats. If a value is duplicated in the original hash, only one of those
can be represented as a key in the inverted hash. Also, this has to
unwind one hash and build a whole new one, which may take some time
-on a large hash.
+on a large hash, such as from a DBM file.
%by_name = reverse %by_address; # Invert the hash
@@ -3157,7 +3256,7 @@ C<readdir()> routine on DIRHANDLE.
=item rindex STR,SUBSTR
-Works just like index except that it returns the position of the LAST
+Works just like index() except that it returns the position of the LAST
occurrence of SUBSTR in STR. If POSITION is specified, returns the
last occurrence at or before that position.
@@ -3181,16 +3280,15 @@ of EXPR.
@counts = ( scalar @a, scalar @b, scalar @c );
There is no equivalent operator to force an expression to
-be interpolated in list context because it's in practice never
+be interpolated in list context because in practice, this is never
needed. If you really wanted to do so, however, you could use
the construction C<@{[ (some expression) ]}>, but usually a simple
C<(some expression)> suffices.
-Though C<scalar> can be considered in general to be a unary operator,
-EXPR is also allowed to be a parenthesized list. The list in fact
-behaves as a scalar comma expression, evaluating all but the last
-element in void context and returning the final element evaluated in
-a scalar context.
+Wince C<scalar> is unary operator, if you accidentally use for EXPR a
+parenthesized list, this behaves as a scalar comma expression, evaluating
+all but the last element in void context and returning the final element
+evaluated in scalar context. This is seldom what you want.
The following single statement:
@@ -3217,10 +3315,10 @@ If you want to position file for C<sysread()> or C<syswrite()>, don't use
C<seek()> -- buffering makes its effect on the file's system position
unpredictable and non-portable. Use C<sysseek()> instead.
-On some systems you have to do a seek whenever you switch between reading
-and writing. Amongst other things, this may have the effect of calling
-stdio's clearerr(3). A WHENCE of C<1> (C<SEEK_CUR>) is useful for not moving
-the file position:
+Due to the rules and rigors of ANSI C, on some systems you have to do a
+seek whenever you switch between reading and writing. Amongst other
+things, this may have the effect of calling stdio's clearerr(3).
+A WHENCE of C<1> (C<SEEK_CUR>) is useful for not moving the file position:
seek(TEST,0,1);
@@ -3367,7 +3465,7 @@ Sends a message on a socket. Takes the same flags as the system call
of the same name. On unconnected sockets you must specify a
destination to send TO, in which case it does a C C<sendto()>. Returns
the number of characters sent, or the undefined value if there is an
-error.
+error. The C system call sendmsg(2) is currently unimplemented.
See L<perlipc/"UDP: Message Passing"> for examples.
=item setpgrp PID,PGRP
@@ -3376,7 +3474,7 @@ Sets the current process group for the specified PID, C<0> for the current
process. Will produce a fatal error if used on a machine that doesn't
implement setpgrp(2). If the arguments are omitted, it defaults to
C<0,0>. Note that the POSIX version of C<setpgrp()> does not accept any
-arguments, so only setpgrp C<0,0> is portable.
+arguments, so only C<setpgrp(0,0)> is portable. See also C<POSIX::setsid()>.
=item setpriority WHICH,WHO,PRIORITY
@@ -3432,7 +3530,8 @@ detaching from it. When reading, VAR must be a variable that will
hold the data read. When writing, if STRING is too long, only SIZE
bytes are used; if STRING is too short, nulls are written to fill out
SIZE bytes. Return TRUE if successful, or FALSE if there is an error.
-See also C<IPC::SysV> documentation.
+See also C<IPC::SysV> documentation and the C<IPC::Shareable> module
+from CPAN.
=item shutdown SOCKET,HOW
@@ -3488,7 +3587,7 @@ See also the POSIX module's C<sigpause()> function.
Opens a socket of the specified kind and attaches it to filehandle
SOCKET. DOMAIN, TYPE, and PROTOCOL are specified the same as for the
system call of the same name. You should "C<use Socket;>" first to get
-the proper definitions imported. See the example in L<perlipc/"Sockets: Client/Server Communication">.
+the proper definitions imported. See the examples in L<perlipc/"Sockets: Client/Server Communication">.
=item socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL
@@ -3828,7 +3927,11 @@ See L<perllocale>.
=item sqrt
Return the square root of EXPR. If EXPR is omitted, returns square
-root of C<$_>.
+root of C<$_>. Only works on non-negative operands, unless you've
+loaded the standard Math::Complex module.
+
+ use Math::Complex;
+ print sqrt(-2); # prints 1.4142135623731i
=item srand EXPR
@@ -3918,10 +4021,26 @@ last stat or filetest are returned. Example:
(This works on machines only for which the device number is negative under NFS.)
+Because the mode contains both the file type and its permissions, you
+should mask off the file type portion and (s)printf using a C<"%o">
+if you want to see the real permissions.
+
+ $mode = (stat($filename))[2];
+ printf "Permissions are %04o\n", $mode & 07777;
+
+
In scalar context, C<stat()> returns a boolean value indicating success
or failure, and, if successful, sets the information associated with
the special filehandle C<_>.
+The File::stat module provides a convenient, by-name access mechanism:
+
+ use File::stat;
+ $sb = stat($filename);
+ printf "File is %s, size is %s, perm %04o, mtime %s\n",
+ $filename, $sb->size, $sb->mode & 07777,
+ scalar localtime $sb->mtime;
+
=item study SCALAR
=item study
@@ -3947,9 +4066,9 @@ before any line containing a certain pattern:
while (<>) {
study;
- print ".IX foo\n" if /\bfoo\b/;
- print ".IX bar\n" if /\bbar\b/;
- print ".IX blurfl\n" if /\bblurfl\b/;
+ print ".IX foo\n" if /\bfoo\b/;
+ print ".IX bar\n" if /\bbar\b/;
+ print ".IX blurfl\n" if /\bblurfl\b/;
# ...
print;
}
@@ -4010,16 +4129,16 @@ If you specify a substring that is partly outside the string, the part
within the string is returned. If the substring is totally outside
the string a warning is produced.
-You can use the C<substr()> function
-as an lvalue, in which case EXPR must be an lvalue. If you assign
-something shorter than LEN, the string will shrink, and if you assign
-something longer than LEN, the string will grow to accommodate it. To
-keep the string the same length you may need to pad or chop your value
-using C<sprintf()>.
+You can use the substr() function as an lvalue, in which case EXPR
+must itself be an lvalue. If you assign something shorter than LEN,
+the string will shrink, and if you assign something longer than LEN,
+the string will grow to accommodate it. To keep the string the same
+length you may need to pad or chop your value using C<sprintf()>.
-An alternative to using C<substr()> as an lvalue is to specify the
+An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument. This allows you to replace
-parts of the EXPR and return what was there before in one operation.
+parts of the EXPR and return what was there before in one operation,
+just as you can with splice().
=item symlink OLDFILE,NEWFILE
@@ -4028,7 +4147,7 @@ Returns C<1> for success, C<0> otherwise. On systems that don't support
symbolic links, produces a fatal error at run time. To check for that,
use eval:
- $symlink_exists = eval { symlink("",""); 1 };
+ $symlink_exists = eval { symlink("",""); 1 };
=item syscall LIST
@@ -4089,12 +4208,12 @@ the PERMS argument to C<sysopen()>, Perl uses the octal value C<0666>.
These permission values need to be in octal, and are modified by your
process's current C<umask>.
-Seldom if ever use C<0644> as argument to C<sysopen()> because that
-takes away the user's option to have a more permissive umask. Better
-to omit it. See the perlfunc(1) entry on C<umask> for more on this.
+You should seldom if ever use C<0644> as argument to C<sysopen()>, because
+that takes away the user's option to have a more permissive umask.
+Better to omit it. See the perlfunc(1) entry on C<umask> for more
+on this.
-The C<IO::File> module provides a more object-oriented approach, if you're
-into that kind of thing.
+See L<perlopentut> for a kinder, gentler explanation of opening files.
=item sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
@@ -4116,6 +4235,10 @@ string. A positive OFFSET greater than the length of SCALAR results
in the string being padded to the required size with C<"\0"> bytes before
the result of the read is appended.
+There is no syseof() function, which is ok, since eof() doesn't work
+very well on device files (like ttys) anyway. Use sysread() and check
+ofr a return value for 0 to decide whether you're done.
+
=item sysseek FILEHANDLE,POSITION,WHENCE
Sets FILEHANDLE's system position using the system call lseek(2). It
@@ -4137,7 +4260,7 @@ the new position.
=item system PROGRAM LIST
-Does exactly the same thing as "C<exec LIST>" except that a fork is done
+Does exactly the same thing as "C<exec LIST>", except that a fork is done
first, and the parent process waits for the child process to complete.
Note that argument processing varies depending on the number of
arguments. If there is more than one argument in LIST, or if LIST is
@@ -4204,7 +4327,9 @@ case the SCALAR is empty you can use OFFSET but only zero offset.
Returns the current position for FILEHANDLE. FILEHANDLE may be an
expression whose value gives the name of the actual filehandle. If
-FILEHANDLE is omitted, assumes the file last read.
+FILEHANDLE is omitted, assumes the file last read.
+
+There is no C<systell()> function. Use C<sysseek(FH, 0, 1)> for that.
=item telldir DIRHANDLE
@@ -4285,7 +4410,7 @@ A class implementing a scalar should have the following methods:
DESTROY this
Not all methods indicated above need be implemented. See L<perltie>,
-L<Tie::Hash>, L<Tie::Array>, L<Tie::Scalar> and L<Tie::Handle>.
+L<Tie::Hash>, L<Tie::Array>, L<Tie::Scalar>, and L<Tie::Handle>.
Unlike C<dbmopen()>, the C<tie()> function will not use or require a module
for you--you need to do that explicitly yourself. See L<DB_File>
@@ -4346,7 +4471,8 @@ If EXPR is omitted, uses C<$_>.
Returns the value of EXPR with the first character
in uppercase (titlecase in Unicode). This is
the internal function implementing the C<\u> escape in double-quoted strings.
-Respects current LC_CTYPE locale if C<use locale> in force. See L<perllocale>.
+Respects current LC_CTYPE locale if C<use locale> in force. See L<perllocale>
+and L<utf8>.
If EXPR is omitted, uses C<$_>.
@@ -4387,8 +4513,6 @@ not trying to restrict access for yourself, returns C<undef>.
Remember that a umask is a number, usually given in octal; it is I<not> a
string of octal digits. See also L</oct>, if all you have is a string.
-
-
=item undef EXPR
=item undef
@@ -4578,8 +4702,15 @@ subject to change in future versions of perl, but it is guaranteed to
be the same order as either the C<keys()> or C<each()> function would
produce on the same (unmodified) hash.
-As a side effect, it resets HASH's iterator. See also C<keys()>, C<each()>,
-and C<sort()>.
+Note that you cannot modify the values of a hash this way, because the
+returned list is just a copy. You need to use a hash slice for that,
+since it's lvaluable in a way that values() is not.
+
+ for (values %hash) { s/foo/bar/g } # FAILS!
+ for (@hash{keys %hash}) { s/foo/bar/g } # ok
+
+As a side effect, calling values() resets the HASH's internal iterator.
+See also C<keys()>, C<each()>, and C<sort()>.
=item vec EXPR,OFFSET,BITS
@@ -4594,7 +4725,7 @@ the correct precedence as in
Vectors created with C<vec()> can also be manipulated with the logical
operators C<|>, C<&>, and C<^>, which will assume a bit vector operation is
-desired when both operands are strings.
+desired when both operands are strings. See L<perlop/"Bitwise String Operators">.
The following code will build up an ASCII string saying C<'PerlPerlPerl'>.
The comments show the string after each step. Note that this code works
@@ -4623,30 +4754,35 @@ If you know the exact length in bits, it can be used in place of the C<*>.
=item wait
-Waits for a child process to terminate and returns the pid of the
-deceased process, or C<-1> if there are no child processes. The status is
-returned in C<$?>. Note that a return value of C<-1> could mean that
-child processes are being automatically reaped, as described in L<perlipc>.
+Behaves like the wait(2) system call on your system: it waits for a child
+process to terminate and returns the pid of the deceased process, or
+C<-1> if there are no child processes. The status is rketurned in C<$?>.
+Note that a return value of C<-1> could mean that child processes are
+being automatically reaped, as described in L<perlipc>.
=item waitpid PID,FLAGS
-Waits for a particular child process to terminate and returns the pid
-of the deceased process, or C<-1> if there is no such child process. The
-status is returned in C<$?>. If you say
+Waits for a particular child process to terminate and returns the pid of
+the deceased process, or C<-1> if there is no such child process. On some
+systems, a value of 0 indicates that there are processes still running.
+The status is returned in C<$?>. If you say
use POSIX ":sys_wait_h";
#...
- waitpid(-1,&WNOHANG);
+ do {
+ $kid = waitpid(-1,&WNOHANG);
+ } until $kid == -1;
-then you can do a non-blocking wait for any process. Non-blocking wait
-is available on machines supporting either the waitpid(2) or
-wait4(2) system calls. However, waiting for a particular pid with
-FLAGS of C<0> is implemented everywhere. (Perl emulates the system call
-by remembering the status values of processes that have exited but have
-not been harvested by the Perl script yet.)
+then you can do a non-blocking wait for all pending zombie processes.
+Non-blocking wait is available on machines supporting either the
+waitpid(2) or wait4(2) system calls. However, waiting for a particular
+pid with FLAGS of C<0> is implemented everywhere. (Perl emulates the
+system call by remembering the status values of processes that have
+exited but have not been harvested by the Perl script yet.)
-Note that a return value of C<-1> could mean that child processes are being
-automatically reaped. See L<perlipc> for details, and for other examples.
+Note that on some systems, a return value of C<-1> could mean that child
+processes are being automatically reaped. See L<perlipc> for details,
+and for other examples.
=item wantarray
@@ -4699,7 +4835,8 @@ warnings (even the so-called mandatory ones). An example:
warn "\$foo is alive and $foo!"; # does show up
See L<perlvar> for details on setting C<%SIG> entries, and for more
-examples.
+examples. See the Carp module for other kinds of warnings using its
+carp() and cluck() functions.
=item write FILEHANDLE