was never a list to start with.
In general, functions in Perl that serve as wrappers for system calls
-of the same name (like C<chown(2)>, C<fork(2)>, C<closedir(2)>, etc.) all return
+of the same name (like chown(2), fork(2), closedir(2), etc.) all return
true when they succeed and C<undef> otherwise, as is usually mentioned
in the descriptions below. This is different from the C interfaces,
which return C<-1> on failure. Exceptions to this rule are C<wait()>,
=item accept NEWSOCKET,GENERICSOCKET
-Accepts an incoming socket connect, just as the C<accept(2)> system call
+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">.
on the previous timer.
For delays of finer granularity than one second, you may use Perl's
-C<syscall()> interface to access C<setitimer(2)> if your system supports it,
+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()>
and C<sleep()> calls.
If you want to use C<alarm()> to time out a system call you need to use an
C<eval()>/C<die()> pair. You can't rely on the alarm causing the system call to
-fail with C<$!> set to EINTR because Perl sets up signal handlers to
+fail with C<$!> set to C<EINTR> because Perl sets up signal handlers to
restart system calls on some systems. Using C<eval()>/C<die()> always works,
modulo the caveats given in L<perlipc/"Signals">.
eval {
- local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
+ local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
$nread = sysread SOCKET, $buffer, $size;
alarm 0;
};
if ($@) {
- die unless $@ eq "alarm\n"; # propagate unexpected errors
+ die unless $@ eq "alarm\n"; # propagate unexpected errors
# timed out
}
else {
$cnt = chmod 0755, 'foo', 'bar';
chmod 0755, @executables;
- $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to --w----r-T
+ $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to
+ # --w----r-T
$mode = '0644'; chmod oct($mode), 'foo'; # this is better
$mode = 0644; chmod $mode, 'foo'; # this is best
=item crypt PLAINTEXT,SALT
-Encrypts a string exactly like the C<crypt(3)> function in the C library
+Encrypts a string exactly like the crypt(3) function in the C library
(assuming that you actually have a version there that has not been
extirpated as a potential munition). This can prove useful for checking
the password file for lousy passwords, amongst other things. Only the
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") {
+ for $file ("/share/prog/defaults.rc",
+ "$ENV{HOME}/.someprogrc") {
unless ($return = do $file) {
- warn "couldn't parse $file: $@" if $@;
- warn "couldn't do $file: $!" unless defined $return;
- warn "couldn't run $file" unless $return;
+ warn "couldn't parse $file: $@" if $@;
+ warn "couldn't do $file: $!" unless defined $return;
+ warn "couldn't run $file" unless $return;
}
}
after having initialized all your variables at the beginning of the
program. When the new binary is executed it will begin by executing a
C<goto LABEL> (with all the restrictions that C<goto> suffers). Think of
-it as a goto with an intervening core dump and reincarnation. If LABEL
+it as a goto with an intervening core dump and reincarnation. If C<LABEL>
is omitted, restarts the program from the top. WARNING: Any files
opened at the time of the dump will NOT be open any more when the
program is reincarnated, with possible resulting confusion on the part
C<values HASH>. If you add or delete elements of a hash while you're
iterating over it, you may get entries skipped or duplicated, so don't.
-The following prints out your environment like the C<printenv(1)> program,
+The following prints out your environment like the printenv(1) program,
only in a different order:
while (($key,$value) = each %ENV) {
purpose, as shown in this example:
# a very private exception trap for divide-by-zero
- eval { local $SIG{'__DIE__'}; $answer = $a / $b; }; warn $@ if $@;
+ eval { local $SIG{'__DIE__'}; $answer = $a / $b; };
+ warn $@ if $@;
This is especially significant, given that C<__DIE__> hooks can call
C<die()> again, which has the effect of changing their error messages:
# __DIE__ hooks may modify error messages
{
- local $SIG{'__DIE__'} = sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
+ local $SIG{'__DIE__'} =
+ sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
eval { die "foo lives here" };
print $@ if $@; # prints "bar lives here"
}
{ exec ('foo') }; print STDERR "couldn't exec foo: $!";
If there is more than one argument in LIST, or if LIST is an array
-with more than one value, calls C<execvp(3)> with the arguments in LIST.
+with more than one value, calls execvp(3) with the arguments in LIST.
If there is only one scalar argument or an array with one element in it,
the argument is checked for shell metacharacters, and if there are any,
the entire argument is passed to the system's command shell for parsing
@args = ( "echo surprise" );
- system @args; # subject to shell escapes if @args == 1
+ system @args; # subject to shell escapes
+ # if @args == 1
system { $args[0] } @args; # safe even with one-arg list
The first version, the one without the indirect object, ran the I<echo>
=item fcntl FILEHANDLE,FUNCTION,SCALAR
-Implements the C<fcntl(2)> function. You'll probably have to say
+Implements the fcntl(2) function. You'll probably have to say
use Fcntl;
conversions.
Note that C<fcntl()> will produce a fatal error if used on a machine that
-doesn't implement C<fcntl(2)>.
+doesn't implement fcntl(2).
=item fileno FILEHANDLE
=item flock FILEHANDLE,OPERATION
-Calls C<flock(2)>, or an emulation of it, on FILEHANDLE. Returns TRUE for
+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 C<flock(2)>, C<fcntl(2)> locking, or C<lockf(3)>. C<flock()>
+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.
To avoid the possibility of mis-coordination, Perl flushes FILEHANDLE
before (un)locking it.
-Note that the emulation built with C<lockf(3)> doesn't provide shared
+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 C<lockf(3)> implements. Most (all?) systems
-implement C<lockf(3)> in terms of C<fcntl(2)> locking, though, so the
+are the semantics that lockf(3) implements. Most (all?) systems
+implement lockf(3) in terms of fcntl(2) locking, though, so the
differing semantics shouldn't bite too many people.
Note also that some versions of C<flock()> cannot lock things over the
network; you would need to use the more system-specific C<fcntl()> for
-that. If you like you can force Perl to ignore your system's C<flock(2)>
-function, and so provide its own C<fcntl(2)>-based emulation, by passing
+that. If you like you can force Perl to ignore your system's flock(2)
+function, and so provide its own fcntl(2)-based emulation, by passing
the switch C<-Ud_flock> to the F<Configure> program when you configure
perl.
=item fork
-Does a C<fork(2)> system call. Returns the child pid to the parent process,
+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.
Note: unflushed buffers remain unflushed in both processes, which means
Returns the current process group for the specified PID. Use
a PID of C<0> to get the current process group for the
current process. Will raise an exception if used on a machine that
-doesn't implement C<getpgrp(2)>. If PID is omitted, returns process
+doesn't implement getpgrp(2). If PID is omitted, returns process
group of current process. Note that the POSIX version of C<getpgrp()>
does not accept a PID argument, so only C<PID==0> is truly portable.
Returns the current priority for a process, a process group, or a user.
(See L<getpriority(2)>.) Will raise a fatal exception if used on a
-machine that doesn't implement C<getpriority(2)>.
+machine that doesn't implement getpriority(2).
=item getpwnam NAME
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 C<getpwnam(3)> documentation and your
+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>,
If EXPR is omitted, does C<gmtime(time())>.
-In scalar context, returns the C<ctime(3)> value:
+In scalar context, returns the ctime(3) value:
$now_string = gmtime; # e.g., "Thu Oct 13 04:54:34 1994"
Also see the C<timegm()> function provided by the C<Time::Local> module,
-and the C<strftime(3)> function available via the POSIX 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
-C<strftime(3)> and C<mktime(3)> function available via the POSIX module. To
+strftime(3) and mktime(3) function 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:
=item grep EXPR,LIST
-This is similar in spirit to, but not the same as, C<grep(1)>
+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.
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 loops's index variable aliases the 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.
=item ioctl FILEHANDLE,FUNCTION,SCALAR
-Implements the C<ioctl(2)> function. You'll probably have to say
+Implements the ioctl(2) function. You'll probably have to say
require "ioctl.ph"; # probably in /usr/local/lib/perl/ioctl.ph
If EXPR is omitted, uses the current time (C<localtime(time)>).
-In scalar context, returns the C<ctime(3)> value:
+In scalar context, returns the ctime(3) value:
$now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
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
-C<strftime(3)> and C<mktime(3)> function available via the POSIX module. To
+strftime(3) and mktime(3) function 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:
=item msgctl ID,CMD,ARG
-Calls the System V IPC function C<msgctl(2)>. You'll probably have to say
+Calls the System V IPC function msgctl(2). You'll probably have to say
use IPC::SysV;
=item msgget KEY,FLAGS
-Calls the System V IPC function C<msgget(2)>. Returns the message queue
+Calls the System V IPC function msgget(2). Returns the message queue
id, or the undefined value if there is an error. See also C<IPC::SysV>
and C<IPC::SysV::Msg> documentation.
switch in L<perlrun> for a better approach.
The prefix and the filename may be separated with spaces.
-These various prefixes correspond to the C<fopen(3)> modes of C<'r'>, C<'r+'>, C<'w'>,
+These various prefixes correspond to the fopen(3) modes of C<'r'>, C<'r+'>, C<'w'>,
C<'w+'>, C<'a'>, and C<'a+'>.
If the filename begins with C<'|'>, the filename is interpreted as a
child to finish, and returns the status value in C<$?>.
The filename passed to open will have leading and trailing
-whitespace deleted, and the normal redirection chararacters
+whitespace deleted, and the normal redirection characters
honored. This property, known as "magic open",
can often be used to good effect. A user could specify a filename of
F<"rsh cat file |">, or you could change certain filenames as needed:
i A signed integer value.
I An unsigned integer value.
- (This 'integer' is _at_least_ 32 bits wide. Its exact size
- depends on what a local C compiler calls 'int', and may
- even be larger than the 'long' described in the next item.)
+ (This 'integer' is _at_least_ 32 bits wide. Its exact
+ size depends on what a local C compiler calls 'int',
+ and may even be larger than the 'long' described in
+ the next item.)
l A signed long value.
L An unsigned long value.
u A uuencoded string.
w A BER compressed integer. Its bytes represent an unsigned
- integer in base 128, most significant digit first, with as few
- digits as possible. Bit eight (the high bit) is set on each
- byte except the last.
+ integer in base 128, most significant digit first, with as
+ few digits as possible. Bit eight (the high bit) is set
+ on each byte except the last.
x A null byte.
X Back up a byte.
C<0> at end of file, or undef if there was an error. SCALAR will be grown
or shrunk to the length actually read. An OFFSET may be specified to
place the read data at some other place than the beginning of the
-string. This call is actually implemented in terms of stdio's C<fread(3)>
-call. To get a true C<read(2)> system call, see C<sysread()>.
+string. This call is actually implemented in terms of stdio's fread(3)
+call. To get a true read(2) system call, see C<sysread()>.
=item readdir DIRHANDLE
In other words, if you try this:
- require Foo::Bar; # a splendid bareword
+ require Foo::Bar; # a splendid bareword
The require function will actually look for the "F<Foo/Bar.pm>" file in the
directories specified in the C<@INC> array.
But if you try this:
$class = 'Foo::Bar';
- require $class; # $class is not a bareword
+ require $class; # $class is not a bareword
#or
- require "Foo::Bar"; # not a bareword because of the ""
+ require "Foo::Bar"; # not a bareword because of the ""
The require function will look for the "F<Foo::Bar>" file in the @INC array and
will complain about not finding "F<Foo::Bar>" there. In this case you can do:
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 C<clearerr(3)>. A WHENCE of C<1> (C<SEEK_CUR>) is useful for not moving
+stdio's clearerr(3). A WHENCE of C<1> (C<SEEK_CUR>) is useful for not moving
the file position:
seek(TEST,0,1);
you may need something more like this:
for (;;) {
- for ($curpos = tell(FILE); $_ = <FILE>; $curpos = tell(FILE)) {
+ for ($curpos = tell(FILE); $_ = <FILE>;
+ $curpos = tell(FILE)) {
# search for some stuff and put it into files
}
sleep($for_a_while);
=item select RBITS,WBITS,EBITS,TIMEOUT
-This calls the C<select(2)> system call with the bit masks specified, which
+This calls the select(2) system call with the bit masks specified, which
can be constructed using C<fileno()> and C<vec()>, along these lines:
$rin = $win = $ein = '';
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 C<setpgrp(2)>. If the arguments are omitted, it defaults to
+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.
=item setpriority WHICH,WHO,PRIORITY
Sets the current priority for a process, a process group, or a user.
-(See C<setpriority(2)>.) Will produce a fatal error if used on a machine
-that doesn't implement C<setpriority(2)>.
+(See setpriority(2).) Will produce a fatal error if used on a machine
+that doesn't implement setpriority(2).
=item setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL
Shuts down a socket connection in the manner indicated by HOW, which
has the same interpretation as in the system call of the same name.
- shutdown(SOCKET, 0); # I/we have stopped reading data
- shutdown(SOCKET, 1); # I/we have stopped writing data
- shutdown(SOCKET, 2); # I/we have stopped using this socket
+ shutdown(SOCKET, 0); # I/we have stopped reading data
+ shutdown(SOCKET, 1); # I/we have stopped writing data
+ shutdown(SOCKET, 2); # I/we have stopped using this socket
This is useful with sockets when you want to tell the other
side you're done writing but not done reading, or vice versa.
busy multitasking system.
For delays of finer granularity than one second, you may use Perl's
-C<syscall()> interface to access C<setitimer(2)> if your system supports it,
+C<syscall()> interface to access setitimer(2) if your system supports it,
or else see L</select()> above.
See also the POSIX module's C<sigpause()> function.
open(PASSWD, '/etc/passwd');
while (<PASSWD>) {
- ($login, $passwd, $uid, $gid, $gcos,$home, $shell) = split(/:/);
+ ($login, $passwd, $uid, $gid,
+ $gcos, $home, $shell) = split(/:/);
#...
}
0 use zeros, not spaces, to right-justify
# prefix non-zero octal with "0", non-zero hex with "0x"
number minimum field width
- .number "precision": digits after decimal point for floating-point,
- max length for string, minimum length for integer
+ .number "precision": digits after decimal point for
+ floating-point, max length for string, minimum length
+ for integer
l interpret integer as C type "long" or "unsigned long"
h interpret integer as C type "short" or "unsigned short"
runtime, you can build an entire loop as a string and C<eval()> that to
avoid recompiling all your patterns all the time. Together with
undefining C<$/> to input entire files as one record, this can be very
-fast, often faster than specialized programs like C<fgrep(1)>. The following
+fast, often faster than specialized programs like fgrep(1). The following
scans a list of files (C<@files>) for a list of words (C<@words>), and prints
out the names of those files that contain a match:
=item sysread FILEHANDLE,SCALAR,LENGTH
Attempts to read LENGTH bytes of data into variable SCALAR from the
-specified FILEHANDLE, using the system call C<read(2)>. It bypasses
+specified FILEHANDLE, using the system call read(2). It bypasses
stdio, so mixing this with other kinds of reads, C<print()>, C<write()>,
C<seek()>, or C<tell()> can cause confusion because stdio usually buffers
data. Returns the number of bytes actually read, C<0> at end of file,
=item sysseek FILEHANDLE,POSITION,WHENCE
-Sets FILEHANDLE's system position using the system call C<lseek(2)>. It
+Sets FILEHANDLE's system position using the system call lseek(2). It
bypasses stdio, so mixing this with reads (other than C<sysread()>),
C<print()>, C<write()>, C<seek()>, or C<tell()> may cause confusion. FILEHANDLE may
be an expression whose value gives the name of the filehandle. The
=item syswrite FILEHANDLE,SCALAR,LENGTH
Attempts to write LENGTH bytes of data from variable SCALAR to the
-specified FILEHANDLE, using the system call C<write(2)>. It bypasses
+specified FILEHANDLE, using the system call write(2). It bypasses
stdio, so mixing this with reads (other than C<sysread())>, C<print()>,
C<write()>, C<seek()>, or C<tell()> may cause confusion because stdio usually
buffers data. Returns the number of bytes actually written, or C<undef>
Sets the umask for the process to EXPR and returns the previous value.
If EXPR is omitted, merely returns the current umask.
-If C<umask(2)> is not implemented on your system and you are trying to
+If umask(2) is not implemented on your system and you are trying to
restrict access for I<yourself> (i.e., (EXPR & 0700) > 0), produces a
-fatal error at run time. If C<umask(2)> is not implemented and you are
+fatal error at run time. If umask(2) is not implemented and you are
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
parameter. Examples:
undef $foo;
- undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'};
+ undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'};
undef @ary;
undef %hash;
undef &mysub;
vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02"
- vec($foo, 21, 4) = 7; # 'PerlPerlPer' # 'r' is "\x72"
+ vec($foo, 21, 4) = 7; # 'PerlPerlPer'
+ # 'r' is "\x72"
vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
- vec($foo, 94, 1) = 1; # 'PerlPerlPerl' # 'l' is "\x6c"
+ vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
+ # 'l' is "\x6c"
To transform a bit vector into a string or array of 0's and 1's, use these:
waitpid(-1,&WNOHANG);
then you can do a non-blocking wait for any process. Non-blocking wait
-is available on machines supporting either the C<waitpid(2)> or
-C<wait4(2)> system calls. However, waiting for a particular pid with
+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.)
Returns the glob with the given C<name> and a defined subroutine or
C<NULL>. The glob lives in the given C<stash>, or in the stashes
-accessable via @ISA and @UNIVERSAL.
+accessible via @ISA and @UNIVERSAL.
The argument C<level> should be either 0 or -1. If C<level==0>, as a
side-effect creates a glob with the given C<name> in the given
function without an explicit return statement is called a subroutine, but
there's really no difference from the language's perspective.)
-Any arguments passed to the routine come in as the array @_. Thus if you
+Any arguments passed to the routine come in as the array C<@_>. Thus if you
called a function with two arguments, those would be stored in C<$_[0]>
-and C<$_[1]>. The array @_ is a local array, but its elements are
+and C<$_[1]>. The array C<@_> is a local array, but its elements are
aliases for the actual scalar parameters. In particular, if an element
C<$_[0]> is updated, the corresponding argument is updated (or an error
occurs if it is not updatable). If an argument is an array or hash
element which did not exist when the function was called, that element is
created only when (and if) it is modified or if a reference to it is
taken. (Some earlier versions of Perl created the element whether or not
-it was assigned to.) Note that assigning to the whole array @_ removes
+it was assigned to.) Note that assigning to the whole array C<@_> removes
the aliasing, and does not update any arguments.
The return value of the subroutine is the value of the last expression
-evaluated. Alternatively, a return statement may be used to exit the
+evaluated. Alternatively, a C<return> statement may be used to exit the
subroutine, optionally specifying the returned value, which will be
evaluated in the appropriate context (list, scalar, or void) depending
on the context of the subroutine call. If you specify no return value,
one large indistinguishable list.
Perl does not have named formal parameters, but in practice all you do is
-assign to a my() list of these. Any variables you use in the function
+assign to a C<my()> list of these. Any variables you use in the function
that aren't declared private are global variables. For the gory details
on creating private variables, see
L<"Private Variables via my()"> and L<"Temporary Values via local()">.
This also has the effect of turning call-by-reference into call-by-value,
because the assignment copies the values. Otherwise a function is free to
-do in-place modifications of @_ and change its caller's values.
+do in-place modifications of C<@_> and change its caller's values.
upcase_in($v1, $v2); # this changes $v1 and $v2
sub upcase_in {
upcase_in("frederick");
-It would be much safer if the upcase_in() function
+It would be much safer if the C<upcase_in()> function
were written to return a copy of its parameters instead
of changing them in place:
}
Notice how this (unprototyped) function doesn't care whether it was passed
-real scalars or arrays. Perl will see everything as one big long flat @_
+real scalars or arrays. Perl will see everything as one big long flat C<@_>
parameter list. This is one of the ways where Perl's simple
-argument-passing style shines. The upcase() function would work perfectly
-well without changing the upcase() definition even if we fed it things
+argument-passing style shines. The C<upcase()> function would work perfectly
+well without changing the C<upcase()> definition even if we fed it things
like this:
@newlist = upcase(@list1, @list2);
(@a, @b) = upcase(@list1, @list2);
Because like its flat incoming parameter list, the return list is also
-flat. So all you have managed to do here is stored everything in @a and
-made @b an empty list. See L<Pass by Reference> for alternatives.
+flat. So all you have managed to do here is stored everything in C<@a> and
+made C<@b> an empty list. See L<Pass by Reference> for alternatives.
-A subroutine may be called using the "&" prefix. The "&" is optional
+A subroutine may be called using the "C<&>" prefix. The "C<&>" is optional
in modern Perls, and so are the parentheses if the subroutine has been
-predeclared. (Note, however, that the "&" is I<NOT> optional when
+predeclared. (Note, however, that the "C<&>" is I<NOT> optional when
you're just naming the subroutine, such as when it's used as an
-argument to defined() or undef(). Nor is it optional when you want to
+argument to C<defined()> or C<undef()>. Nor is it optional when you want to
do an indirect subroutine call with a subroutine name or reference
using the C<&$subref()> or C<&{$subref}()> constructs. See L<perlref>
for more on that.)
Subroutines may be called recursively. If a subroutine is called using
-the "&" form, the argument list is optional, and if omitted, no @_ array is
-set up for the subroutine: the @_ array at the time of the call is
+the "C<&>" form, the argument list is optional, and if omitted, no C<@_> array is
+set up for the subroutine: the C<@_> array at the time of the call is
visible to subroutine instead. This is an efficiency mechanism that
new users may wish to avoid.
&foo; # foo() get current args, like foo(@_) !!
foo; # like foo() IFF sub foo predeclared, else "foo"
-Not only does the "&" form make the argument list optional, but it also
+Not only does the "C<&>" form make the argument list optional, but it also
disables any prototype checking on the arguments you do provide. This
is partly for historical reasons, and partly for having a convenient way
to cheat if you know what you're doing. See the section on Prototypes below.
just as are modules whose names are in all lower case. A function in
all capitals is a loosely-held convention meaning it will be called
indirectly by the run-time system itself. Functions that do special,
-pre-defined things BEGIN, END, AUTOLOAD, and DESTROY--plus all the
-functions mentioned in L<perltie>. The 5.005 release adds INIT
+pre-defined things are C<BEGIN>, C<END>, C<AUTOLOAD>, and C<DESTROY>--plus all the
+functions mentioned in L<perltie>. The 5.005 release adds C<INIT>
to this list.
-=head2 Private Variables via my()
+=head2 Private Variables via C<my()>
Synopsis:
my $foo = "flurp"; # declare $foo lexical, and init it
my @oof = @bar; # declare @oof lexical, and init it
-A "my" declares the listed variables to be confined (lexically) to the
+A "C<my>" declares the listed variables to be confined (lexically) to the
enclosing block, conditional (C<if/unless/elsif/else>), loop
(C<for/foreach/while/until/continue>), subroutine, C<eval>, or
C<do/require/use>'d file. If more than one value is listed, the list
must be placed in parentheses. All listed elements must be legal lvalues.
Only alphanumeric identifiers may be lexically scoped--magical
-builtins like $/ must currently be localized with "local" instead.
+builtins like C<$/> must currently be C<local>ize with "C<local>" instead.
-Unlike dynamic variables created by the "local" operator, lexical
-variables declared with "my" are totally hidden from the outside world,
+Unlike dynamic variables created by the "C<local>" operator, lexical
+variables declared with "C<my>" are totally hidden from the outside world,
including any called subroutines (even if it's the same subroutine called
from itself or elsewhere--every call gets its own copy).
-This doesn't mean that a my() variable declared in a statically
+This doesn't mean that a C<my()> variable declared in a statically
I<enclosing> lexical scope would be invisible. Only the dynamic scopes
-are cut off. For example, the bumpx() function below has access to the
-lexical $x variable because both the my and the sub occurred at the same
+are cut off. For example, the C<bumpx()> function below has access to the
+lexical C<$x> variable because both the my and the sub occurred at the same
scope, presumably the file scope.
my $x = 10;
sub bumpx { $x++ }
-(An eval(), however, can see the lexical variables of the scope it is
+(An C<eval()>, however, can see the lexical variables of the scope it is
being evaluated in so long as the names aren't hidden by declarations within
-the eval() itself. See L<perlref>.)
+the C<eval()> itself. See L<perlref>.)
-The parameter list to my() may be assigned to if desired, which allows you
+The parameter list to C<my()> may be assigned to if desired, which allows you
to initialize your variables. (If no initializer is given for a
particular variable, it is created with the undefined value.) Commonly
this is used to name the parameters to a subroutine. Examples:
return $arg;
}
-The "my" is simply a modifier on something you might assign to. So when
-you do assign to the variables in its argument list, the "my" doesn't
+The "C<my>" is simply a modifier on something you might assign to. So when
+you do assign to the variables in its argument list, the "C<my>" doesn't
change whether those variables are viewed as a scalar or an array. So
my ($foo) = <STDIN>; # WRONG?
my $x = $x;
-can be used to initialize the new $x with the value of the old $x, and
+can be used to initialize the new $x with the value of the old C<$x>, and
the expression
my $x = 123 and $x == 123
-is false unless the old $x happened to have the value 123.
+is false unless the old C<$x> happened to have the value C<123>.
Lexical scopes of control structures are not bounded precisely by the
braces that delimit their controlled blocks; control expressions are
print $line;
}
-the scope of $line extends from its declaration throughout the rest of
+the scope of C<$line> extends from its declaration throughout the rest of
the loop construct (including the C<continue> clause), but not beyond
it. Similarly, in the conditional
die "'$answer' is neither 'yes' nor 'no'";
}
-the scope of $answer extends from its declaration throughout the rest
+the scope of C<$answer> extends from its declaration throughout the rest
of the conditional (including C<elsif> and C<else> clauses, if any),
but not beyond it.
The C<foreach> loop defaults to scoping its index variable dynamically
(in the manner of C<local>; see below). However, if the index
-variable is prefixed with the keyword "my", then it is lexically
+variable is prefixed with the keyword "C<my>", then it is lexically
scoped instead. Thus in the loop
for my $i (1, 2, 3) {
some_function();
}
-the scope of $i extends to the end of the loop, but not beyond it, and
-so the value of $i is unavailable in some_function().
+the scope of C<$i> extends to the end of the loop, but not beyond it, and
+so the value of C<$i> is unavailable in C<some_function()>.
Some users may wish to encourage the use of lexically scoped variables.
As an aid to catching implicit references to package variables,
then any variable reference from there to the end of the enclosing
block must either refer to a lexical variable, or must be fully
qualified with the package name. A compilation error results
-otherwise. An inner block may countermand this with S<"no strict 'vars'">.
+otherwise. An inner block may countermand this with S<"C<no strict 'vars'>">.
-A my() has both a compile-time and a run-time effect. At compile time,
+A C<my()> has both a compile-time and a run-time effect. At compile time,
the compiler takes notice of it; the principle usefulness of this is to
-quiet C<use strict 'vars'>. The actual initialization is delayed until
+quiet S<"C<use strict 'vars'>">. The actual initialization is delayed until
run time, so it gets executed appropriately; every time through a loop,
for example.
-Variables declared with "my" are not part of any package and are therefore
+Variables declared with "C<my>" are not part of any package and are therefore
never fully qualified with the package name. In particular, you're not
allowed to try to make a package variable (or other global) lexical:
my $_; # also illegal (currently)
In fact, a dynamic variable (also known as package or global variables)
-are still accessible using the fully qualified :: notation even while a
+are still accessible using the fully qualified C<::> notation even while a
lexical of the same name is also visible:
package main;
my $x = 20;
print "$x and $::x\n";
-That will print out 20 and 10.
+That will print out C<20> and C<10>.
-You may declare "my" variables at the outermost scope of a file to hide
+You may declare "C<my>" variables at the outermost scope of a file to hide
any such identifiers totally from the outside world. This is similar
to C's static variables at the file level. To do this with a subroutine
requires the use of a closure (anonymous function with lexical access).
-If a block (such as an eval(), function, or C<package>) wants to create
+If a block (such as an C<eval()>, function, or C<package>) wants to create
a private subroutine that cannot be called from outside that block,
it can declare a lexical variable containing an anonymous sub reference:
As long as the reference is never returned by any function within the
module, no outside module can see the subroutine, because its name is not in
any package's symbol table. Remember that it's not I<REALLY> called
-$some_pack::secret_version or anything; it's just $secret_version,
+C<$some_pack::secret_version> or anything; it's just C<$secret_version>,
unqualified and unqualifiable.
This does not work with object methods, however; all object methods have
=head2 Peristent Private Variables
Just because a lexical variable is lexically (also called statically)
-scoped to its enclosing block, eval, or do FILE, this doesn't mean that
+scoped to its enclosing block, C<eval>, or C<do> FILE, this doesn't mean that
within a function it works like a C static. It normally works more
like a C auto, but with implicit garbage collection.
If this function is being sourced in from a separate file
via C<require> or C<use>, then this is probably just fine. If it's
-all in the main program, you'll need to arrange for the my()
+all in the main program, you'll need to arrange for the C<my()>
to be executed early, either by putting the whole block above
-your main program, or more likely, placing merely a BEGIN
+your main program, or more likely, placing merely a C<BEGIN>
sub around it to make sure it gets executed before your program
starts to run:
}
}
-See L<perlrun> about the BEGIN function.
+See L<perlmod/"Package Constructors and Destructors"> about the C<BEGIN> function.
If declared at the outermost scope, the file scope, then lexicals work
someone like C's file statics. They are available to all functions in
=head2 Temporary Values via local()
-B<NOTE>: In general, you should be using "my" instead of "local", because
+B<NOTE>: In general, you should be using "C<my>" instead of "C<local>", because
it's faster and safer. Exceptions to this include the global punctuation
variables, filehandles and formats, and direct manipulation of the Perl
-symbol table itself. Format variables often use "local" though, as do
+symbol table itself. Format variables often use "C<local>" though, as do
other variables whose current value must be visible to called
subroutines.
local *merlyn = 'randal'; # SAME THING: promote 'randal' to *randal
local *merlyn = \$randal; # just alias $merlyn, not @merlyn etc
-A local() modifies its listed variables to be "local" to the enclosing
-block, eval, or C<do FILE>--and to I<any called from within that block>.
-A local() just gives temporary values to global (meaning package)
-variables. It does not create a local variable. This is known as
-dynamic scoping. Lexical scoping is done with "my", which works more
+A C<local()> modifies its listed variables to be "local" to the enclosing
+block, C<eval>, or C<do FILE>--and to I<any subroutine called from within that block>.
+A C<local()> just gives temporary values to global (meaning package)
+variables. It does B<not> create a local variable. This is known as
+dynamic scoping. Lexical scoping is done with "C<my>", which works more
like C's auto declarations.
-If more than one variable is given to local(), they must be placed in
+If more than one variable is given to C<local()>, they must be placed in
parentheses. All listed elements must be legal lvalues. This operator works
by saving the current values of those variables in its argument list on a
hidden stack and restoring them upon exiting the block, subroutine, or
}
# old %digits restored here
-Because local() is a run-time command, it gets executed every time
+Because C<local()> is a run-time command, it gets executed every time
through a loop. In releases of Perl previous to 5.0, this used more stack
storage each time until the loop was exited. Perl now reclaims the space
each time through, but it's still more efficient to declare your variables
outside the loop.
-A local is simply a modifier on an lvalue expression. When you assign to
-a localized variable, the local doesn't change whether its list is viewed
+A C<local> is simply a modifier on an lvalue expression. When you assign to
+a C<local>ized variable, the C<local> doesn't change whether its list is viewed
as a scalar or an array. So
local($foo) = <STDIN>;
[..normal %ENV behavior here..]
It's also worth taking a moment to explain what happens when you
-localize a member of a composite type (i.e. an array or hash element).
-In this case, the element is localized I<by name>. This means that
+C<local>ize a member of a composite type (i.e. an array or hash element).
+In this case, the element is C<local>ized I<by name>. This means that
when the scope of the C<local()> ends, the saved value will be
restored to the hash element whose key was named in the C<local()>, or
the array element whose index was named in the C<local()>. If that
When evaluated, the typeglob produces a scalar value that represents
all the objects of that name, including any filehandle, format, or
subroutine. When assigned to, it causes the name mentioned to refer to
-whatever "*" value was assigned to it. Example:
+whatever "C<*>" value was assigned to it. Example:
sub doubleary {
local(*someary) = @_;
Note that scalars are already passed by reference, so you can modify
scalar arguments without using this mechanism by referring explicitly
to C<$_[0]> etc. You can modify all the elements of an array by passing
-all the elements as scalars, but you have to use the * mechanism (or
-the equivalent reference mechanism) to push, pop, or change the size of
+all the elements as scalars, but you have to use the C<*> mechanism (or
+the equivalent reference mechanism) to C<push>, C<pop>, or change the size of
an array. It will certainly be faster to pass the typeglob (or reference).
Even if you don't want to modify an array, this mechanism is useful for
=head2 When to Still Use local()
-Despite the existence of my(), there are still three places where the
-local() operator still shines. In fact, in these three places, you
+Despite the existence of C<my()>, there are still three places where the
+C<local()> operator still shines. In fact, in these three places, you
I<must> use C<local> instead of C<my>.
=over
-=item 1. You need to give a global variable a temporary value, especially $_.
+=item 1. You need to give a global variable a temporary value, especially C<$_>.
-The global variables, like @ARGV or the punctuation variables, must be
-localized with local(). This block reads in I</etc/motd>, and splits
+The global variables, like C<@ARGV> or the punctuation variables, must be
+C<local>ized with C<local()>. This block reads in F</etc/motd>, and splits
it up into chunks separated by lines of equal signs, which are placed
-in @Fields.
+in C<@Fields>.
{
local @ARGV = ("/etc/motd");
@Fields = split /^\s*=+\s*$/;
}
-It particular, its important to localize $_ in any routine that assigns
+It particular, it's important to C<local>ize C<$_> in any routine that assigns
to it. Look out for implicit assignments in C<while> conditionals.
=item 2. You need to create a local file or directory handle or a local function.
-A function that needs a filehandle of its own must use local() uses
-local() on complete typeglob. This can be used to create new symbol
+A function that needs a filehandle of its own must use C<local()> uses
+C<local()> on complete typeglob. This can be used to create new symbol
table entries:
sub ioqueue {
a local alias.
{
- local *grow = \&shrink; # only until this block exists
- grow(); # really calls shrink()
- move(); # if move() grow()s, it shrink()s too
+ local *grow = \&shrink; # only until this block exists
+ grow(); # really calls shrink()
+ move(); # if move() grow()s, it shrink()s too
}
- grow(); # get the real grow() again
+ grow(); # get the real grow() again
See L<perlref/"Function Templates"> for more about manipulating
functions by name in this way.
=item 3. You want to temporarily change just one element of an array or hash.
-You can localize just one element of an aggregate. Usually this
+You can C<local>ize just one element of an aggregate. Usually this
is done on dynamics:
{
This section may not make much sense to you otherwise.
Here are a few simple examples. First, let's pass in several
-arrays to a function and have it pop all of then, return a new
+arrays to a function and have it C<pop> all of then, return a new
list of all their former last elements:
@tailings = popmany ( \@a, \@b, \@c, \@d );
or
(%a, %b) = func(%c, %d);
-That syntax simply won't work. It sets just @a or %a and clears the @b or
-%b. Plus the function didn't get passed into two separate arrays or
-hashes: it got one long list in @_, as always.
+That syntax simply won't work. It sets just C<@a> or C<%a> and clears the C<@b> or
+C<%b>. Plus the function didn't get passed into two separate arrays or
+hashes: it got one long list in C<@_>, as always.
If you can arrange for everyone to deal with this through references, it's
cleaner code, although not so nice to look at. Here's a function that
}
Here we're using the typeglobs to do symbol table aliasing. It's
-a tad subtle, though, and also won't work if you're using my()
-variables, because only globals (well, and local()s) are in the symbol table.
+a tad subtle, though, and also won't work if you're using C<my()>
+variables, because only globals (well, and C<local()>s) are in the symbol table.
If you're passing around filehandles, you could usually just use the bare
-typeglob, like *STDOUT, but typeglobs references would be better because
-they'll still work properly under C<use strict 'refs'>. For example:
+typeglob, like C<*STDOUT>, but typeglobs references would be better because
+they'll still work properly under S<C<use strict 'refs'>>. For example:
splutter(\*STDOUT);
sub splutter {
return scalar <$fh>;
}
-Another way to do this is using *HANDLE{IO}, see L<perlref> for usage
+Another way to do this is using C<*HANDLE{IO}>, see L<perlref> for usage
and caveats.
If you're planning on generating new filehandles, you could do this:
}
Although that will actually produce a small memory leak. See the bottom
-of L<perlfunc/open()> for a somewhat cleaner way using the IO::Handle
+of L<perlfunc/open()> for a somewhat cleaner way using the C<IO::Handle>
package.
=head2 Prototypes
sub mypush (\@@)
-then mypush() takes arguments exactly like push() does. The declaration
+then C<mypush()> takes arguments exactly like C<push()> does. The declaration
of the function to be called must be visible at compile time. The prototype
affects only the interpretation of new-style calls to the function, where
new-style is defined as not using the C<&> character. In other words,
Declared as Called as
- sub mylink ($$) mylink $old, $new
- sub myvec ($$$) myvec $var, $offset, 1
- sub myindex ($$;$) myindex &getstring, "substr"
- sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
- sub myreverse (@) myreverse $a,$b,$c
- sub myjoin ($@) myjoin ":",$a,$b,$c
- sub mypop (\@) mypop @array
- sub mysplice (\@$$@) mysplice @array,@array,0,@pushme
- sub mykeys (\%) mykeys %{$hashref}
- sub myopen (*;$) myopen HANDLE, $name
- sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
- sub mygrep (&@) mygrep { /foo/ } $a,$b,$c
- sub myrand ($) myrand 42
- sub mytime () mytime
+ sub mylink ($$) mylink $old, $new
+ sub myvec ($$$) myvec $var, $offset, 1
+ sub myindex ($$;$) myindex &getstring, "substr"
+ sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
+ sub myreverse (@) myreverse $a, $b, $c
+ sub myjoin ($@) myjoin ":", $a, $b, $c
+ sub mypop (\@) mypop @array
+ sub mysplice (\@$$@) mysplice @array, @array, 0, @pushme
+ sub mykeys (\%) mykeys %{$hashref}
+ sub myopen (*;$) myopen HANDLE, $name
+ sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
+ sub mygrep (&@) mygrep { /foo/ } $a, $b, $c
+ sub myrand ($) myrand 42
+ sub mytime () mytime
Any backslashed prototype character represents an actual argument
that absolutely must start with that character. The value passed
C<\> to that argument.
Unbackslashed prototype characters have special meanings. Any
-unbackslashed @ or % eats all the rest of the arguments, and forces
-list context. An argument represented by $ forces scalar context. An
-& requires an anonymous subroutine, which, if passed as the first
-argument, does not require the "sub" keyword or a subsequent comma. A
-* does whatever it has to do to turn the argument into a reference to a
+unbackslashed C<@> or C<%> eats all the rest of the arguments, and forces
+list context. An argument represented by C<$> forces scalar context. An
+C<&> requires an anonymous subroutine, which, if passed as the first
+argument, does not require the "C<sub>" keyword or a subsequent comma. A
+C<*> does whatever it has to do to turn the argument into a reference to a
symbol table entry.
A semicolon separates mandatory arguments from optional arguments.
-(It is redundant before @ or %.)
+(It is redundant before C<@> or C<%>.)
Note how the last three examples above are treated specially by the parser.
-mygrep() is parsed as a true list operator, myrand() is parsed as a
-true unary operator with unary precedence the same as rand(), and
-mytime() is truly without arguments, just like time(). That is, if you
+C<mygrep()> is parsed as a true list operator, C<myrand()> is parsed as a
+true unary operator with unary precedence the same as C<rand()>, and
+C<mytime()> is truly without arguments, just like C<time()>. That is, if you
say
mytime +2;
-you'll get mytime() + 2, not mytime(2), which is how it would be parsed
+you'll get C<mytime() + 2>, not C<mytime(2)>, which is how it would be parsed
without the prototype.
-The interesting thing about & is that you can generate new syntax with it:
+The interesting thing about C<&> is that you can generate new syntax with it:
sub try (&@) {
my($try,$catch) = @_;
/phooey/ and print "unphooey\n";
};
-That prints "unphooey". (Yes, there are still unresolved
-issues having to do with the visibility of @_. I'm ignoring that
-question for the moment. (But note that if we make @_ lexically
+That prints C<"unphooey">. (Yes, there are still unresolved
+issues having to do with the visibility of C<@_>. I'm ignoring that
+question for the moment. (But note that if we make C<@_> lexically
scoped, those anonymous subroutines can act like closures... (Gee,
is this sounding a little Lispish? (Never mind.))))
-And here's a reimplementation of grep:
+And here's a reimplementation of C<grep>:
sub mygrep (&@) {
my $code = shift;
func(@foo);
func( split /:/ );
-Then you've just supplied an automatic scalar() in front of their
-argument, which can be more than a bit surprising. The old @foo
+Then you've just supplied an automatic C<scalar()> in front of their
+argument, which can be more than a bit surprising. The old C<@foo>
which used to hold one thing doesn't get passed in. Instead,
-the func() now gets passed in 1, that is, the number of elements
-in @foo. And the split() gets called in a scalar context and
-starts scribbling on your @_ parameter list.
+the C<func()> now gets passed in C<1>, that is, the number of elements
+in C<@foo>. And the C<split()> gets called in a scalar context and
+starts scribbling on your C<@_> parameter list.
This is all very powerful, of course, and should be used only in moderation
to make the world a better place.
either a constant or a lexically-scoped scalar which has no other
references, then it will be used in place of function calls made
without C<&> or C<do>. Calls made using C<&> or C<do> are never
-inlined. (See constant.pm for an easy way to declare most
+inlined. (See F<constant.pm> for an easy way to declare most
constants.)
The following functions would all be inlined:
if the current package has imported some other subroutine called
C<&open()> from elsewhere.
-Library modules should not in general export builtin names like "open"
-or "chdir" as part of their default @EXPORT list, because these may
+Library modules should not in general export builtin names like "C<open>"
+or "C<chdir>" as part of their default C<@EXPORT> list, because these may
sneak into someone else's namespace and change the semantics unexpectedly.
-Instead, if the module adds the name to the @EXPORT_OK list, then it's
+Instead, if the module adds the name to the C<@EXPORT_OK> list, then it's
possible for a user to import the name explicitly, but not implicitly.
That is, they could say
use Module 'open';
-and it would import the open override, but if they said
+and it would import the C<open> override, but if they said
use Module;
packages that were searched for the original subroutine, then that
C<AUTOLOAD> subroutine is called with the arguments that would have been
passed to the original subroutine. The fully qualified name of the
-original subroutine magically appears in the $AUTOLOAD variable in the
+original subroutine magically appears in the C<$AUTOLOAD> variable in the
same package as the C<AUTOLOAD> routine. The name is not passed as an
ordinary argument because, er, well, just because, that's why...
without a trace. (See the standard C<AutoLoader> module, for example.)
But an C<AUTOLOAD> routine can also just emulate the routine and never
define it. For example, let's pretend that a function that wasn't defined
-should just call system() with those arguments. All you'd do is this:
+should just call C<system()> with those arguments. All you'd do is this:
sub AUTOLOAD {
my $program = $AUTOLOAD;
The only things that need to be declared in Perl are report formats
and subroutines. See the sections below for more information on those
declarations. All uninitialized user-created objects are assumed to
-start with a null or 0 value until they are defined by some explicit
+start with a C<null> or C<0> value until they are defined by some explicit
operation such as assignment. (Though you can get warnings about the
use of undefined values if you like.) The sequence of statements is
executed just once, unlike in B<sed> and B<awk> scripts, where the
Perl is, for the most part, a free-form language. (The only
exception to this is format declarations, for obvious reasons.) Comments
-are indicated by the "#" character, and extend to the end of the line. If
+are indicated by the C<"#"> character, and extend to the end of the line. If
you attempt to use C</* */> C-style comments, it will be interpreted
either as division or pattern matching, depending on the context, and C++
C<//> comments just look like a null regular expression, so don't do
the execution of the primary sequence of statements--declarations all
take effect at compile time. Typically all the declarations are put at
the beginning or the end of the script. However, if you're using
-lexically-scoped private variables created with my(), you'll have to make sure
+lexically-scoped private variables created with C<my()>, you'll have to make sure
your format or subroutine definition is within the same block scope
as the my if you expect to be able to access those private variables.
The C<if> and C<unless> modifiers have the expected semantics,
presuming you're a speaker of English. The C<foreach> modifier is an
-iterator: For each value in EXPR, it aliases $_ to the value and
+iterator: For each value in EXPR, it aliases C<$_> to the value and
executes the statement. The C<while> and C<until> modifiers have the
-usual "while loop" semantics (conditional evaluated first), except
-when applied to a do-BLOCK (or to the now-deprecated do-SUBROUTINE
+usual "C<while> loop" semantics (conditional evaluated first), except
+when applied to a C<do>-BLOCK (or to the now-deprecated C<do>-SUBROUTINE
statement), in which case the block executes once before the
conditional is evaluated. This is so that you can write loops like:
later will I<NOT> work in this construct, because modifiers don't take
loop labels. Sorry. You can always put another block inside of it
(for C<next>) or around it (for C<last>) to do that sort of thing.
-For next, just double the braces:
+For C<next>, just double the braces:
do {{
next if $x == $y;
# do something here
}} until $x++ > $z;
-For last, you have to be more elaborate:
+For C<last>, you have to be more elaborate:
LOOP: {
do {
the sense of the test is reversed.
The C<while> statement executes the block as long as the expression is
-true (does not evaluate to the null string or 0 or "0"). The LABEL is
+true (does not evaluate to the null string (C<"">) or C<0> or C<"0")>. The LABEL is
optional, and if present, consists of an identifier followed by a colon.
The LABEL identifies the loop for the loop control statements C<next>,
C<last>, and C<redo>. If the LABEL is omitted, the loop control statement
The C<foreach> keyword is actually a synonym for the C<for> keyword, so
you can use C<foreach> for readability or C<for> for brevity. (Or because
the Bourne shell is more familiar to you than I<csh>, so writing C<for>
-comes more naturally.) If VAR is omitted, $_ is set to each value.
+comes more naturally.) If VAR is omitted, C<$_> is set to each value.
If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. That's because the C<foreach> loop index variable is
an implicit alias for each item in the list that you're looping over.
$nothing = 1;
}
-There is no official switch statement in Perl, because there are
+There is no official C<switch> statement in Perl, because there are
already several ways to write the equivalent. In addition to the
above, you could write
$nothing = 1;
}
-or formatted so it stands out more as a "proper" switch statement:
+or formatted so it stands out more as a "proper" C<switch> statement:
SWITCH: {
/^abc/ && do {
else
{ $nothing = 1 }
-A common idiom for a switch statement is to use C<foreach>'s aliasing to make
-a temporary assignment to $_ for convenient matching:
+A common idiom for a C<switch> statement is to use C<foreach>'s aliasing to make
+a temporary assignment to C<$_> for convenient matching:
SWITCH: for ($where) {
/In Card Names/ && do { push @flags, '-e'; last; };
Or if you are certainly that all the C<&&> clauses are true, you can use
something like this, which "switches" on the value of the
-HTTP_USER_AGENT envariable.
+C<HTTP_USER_AGENT> envariable.
#!/usr/bin/perl
# pick out jargon file page based on browser
That kind of switch statement only works when you know the C<&&> clauses
will be true. If you don't, the previous C<?:> example should be used.
-You might also consider writing a hash instead of synthesizing a switch
+You might also consider writing a hash instead of synthesizing a C<switch>
statement.
=head2 Goto
Although not for the faint of heart, Perl does support a C<goto> statement.
A loop's LABEL is not actually a valid target for a C<goto>;
-it's just the name of the loop. There are three forms: goto-LABEL,
-goto-EXPR, and goto-&NAME.
+it's just the name of the loop. There are three forms: C<goto>-LABEL,
+C<goto>-EXPR, and C<goto>-&NAME.
-The goto-LABEL form finds the statement labeled with LABEL and resumes
+The C<goto>-LABEL form finds the statement labeled with LABEL and resumes
execution there. It may not be used to go into any construct that
-requires initialization, such as a subroutine or a foreach loop. It
+requires initialization, such as a subroutine or a C<foreach> loop. It
also can't be used to go into a construct that is optimized away. It
can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it's usually better to use some other
-construct such as last or die. The author of Perl has never felt the
-need to use this form of goto (in Perl, that is--C is another matter).
+construct such as C<last> or C<die>. The author of Perl has never felt the
+need to use this form of C<goto> (in Perl, that is--C is another matter).
-The goto-EXPR form expects a label name, whose scope will be resolved
-dynamically. This allows for computed gotos per FORTRAN, but isn't
+The C<goto>-EXPR form expects a label name, whose scope will be resolved
+dynamically. This allows for computed C<goto>s per FORTRAN, but isn't
necessarily recommended if you're optimizing for maintainability:
goto ("FOO", "BAR", "GLARCH")[$i];
-The goto-&NAME form is highly magical, and substitutes a call to the
+The C<goto>-&NAME form is highly magical, and substitutes a call to the
named subroutine for the currently running subroutine. This is used by
-AUTOLOAD() subroutines that wish to load another subroutine and then
+C<AUTOLOAD()> subroutines that wish to load another subroutine and then
pretend that the other subroutine had been called in the first place
-(except that any modifications to @_ in the current subroutine are
-propagated to the other subroutine.) After the C<goto>, not even caller()
+(except that any modifications to C<@_> in the current subroutine are
+propagated to the other subroutine.) After the C<goto>, not even C<caller()>
will be able to tell that this routine was called first.
In almost all cases like this, it's usually a far, far better idea to use the
=cut back
print "got $a\n";
-You probably shouldn't rely upon the warn() being podded out forever.
+You probably shouldn't rely upon the C<warn()> being podded out forever.
Not all pod translators are well-behaved in this regard, and perhaps
the compiler will become pickier.
Much like the C preprocessor, Perl can process line directives. Using
this, one can control Perl's idea of filenames and line numbers in
error or warning messages (especially for strings that are processed
-with eval()). The syntax for this mechanism is the same as for most
+with C<eval()>). The syntax for this mechanism is the same as for most
C preprocessors: it matches the regular expression
C</^#\s*line\s+(\d+)\s*(?:\s"([^"]*)")?/> with C<$1> being the line
number for the next line, and C<$2> being the optional filename