between the function and left parenthesis doesn't count--so you need to
be careful sometimes:
- print 1+2+4; # Prints 7.
- print(1+2) + 4; # Prints 3.
- print (1+2)+4; # Also prints 3!
- print +(1+2)+4; # Prints 7.
- print ((1+2)+4); # Prints 7.
+ print 1+2+4; # Prints 7.
+ print(1+2) + 4; # Prints 3.
+ print (1+2)+4; # Also prints 3!
+ print +(1+2)+4; # Prints 7.
+ print ((1+2)+4); # Prints 7.
If you run Perl with the B<-w> switch it can warn you about this. For
example, the third line above produces:
C<waitpid>, and C<syscall>. System calls also set the special C<$!>
variable on failure. Other functions do not, except accidentally.
+Extension modules can also hook into the Perl parser to define new
+kinds of keyword-headed expression. These may look like functions, but
+may also look completely different. The syntax following the keyword
+is defined entirely by the extension. If you are an implementor, see
+L<perlapi/PL_keyword_plugin> for the mechanism. If you are using such
+a module, see the module's documentation for details of the syntax that
+it defines.
+
=head2 Perl Functions by Category
X<function>
names, precedence is the same as any other named unary operator. The
operator may be any of:
- -r File is readable by effective uid/gid.
- -w File is writable by effective uid/gid.
- -x File is executable by effective uid/gid.
- -o File is owned by effective uid.
+ -r File is readable by effective uid/gid.
+ -w File is writable by effective uid/gid.
+ -x File is executable by effective uid/gid.
+ -o File is owned by effective uid.
- -R File is readable by real uid/gid.
- -W File is writable by real uid/gid.
- -X File is executable by real uid/gid.
- -O File is owned by real uid.
+ -R File is readable by real uid/gid.
+ -W File is writable by real uid/gid.
+ -X File is executable by real uid/gid.
+ -O File is owned by real uid.
- -e File exists.
- -z File has zero size (is empty).
- -s File has nonzero size (returns size in bytes).
+ -e File exists.
+ -z File has zero size (is empty).
+ -s File has nonzero size (returns size in bytes).
- -f File is a plain file.
- -d File is a directory.
- -l File is a symbolic link.
- -p File is a named pipe (FIFO), or Filehandle is a pipe.
- -S File is a socket.
- -b File is a block special file.
- -c File is a character special file.
- -t Filehandle is opened to a tty.
+ -f File is a plain file.
+ -d File is a directory.
+ -l File is a symbolic link.
+ -p File is a named pipe (FIFO), or Filehandle is a pipe.
+ -S File is a socket.
+ -b File is a block special file.
+ -c File is a character special file.
+ -t Filehandle is opened to a tty.
- -u File has setuid bit set.
- -g File has setgid bit set.
- -k File has sticky bit set.
+ -u File has setuid bit set.
+ -g File has setgid bit set.
+ -k File has sticky bit set.
- -T File is an ASCII text file (heuristic guess).
- -B File is a "binary" file (opposite of -T).
+ -T File is an ASCII text file (heuristic guess).
+ -B File is a "binary" file (opposite of -T).
- -M Script start time minus file modification time, in days.
- -A Same for access time.
- -C Same for inode change time (Unix, may differ for other platforms)
+ -M Script start time minus file modification time, in days.
+ -A Same for access time.
+ -C Same for inode change time (Unix, may differ for other platforms)
Example:
while (<>) {
- chomp;
- next unless -f $_; # ignore specials
- #...
+ chomp;
+ next unless -f $_; # ignore specials
+ #...
}
The interpretation of the file permission operators C<-r>, C<-R>,
modulo the caveats given in L<perlipc/"Signals">.
eval {
- local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
- alarm $timeout;
- $nread = sysread SOCKET, $buffer, $size;
- alarm 0;
+ 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
- # timed out
+ die unless $@ eq "alarm\n"; # propagate unexpected errors
+ # timed out
}
else {
- # didn't
+ # didn't
}
For more information see L<perlipc>.
sub tan { sin($_[0]) / cos($_[0]) }
-Note that atan2(0, 0) is not well-defined.
+The return value for C<atan2(0,0)> is implementation-defined; consult
+your atan2(3) manpage for more information.
=item bind SOCKET,NAME
X<bind>
If LAYER is present it is a single string, but may contain multiple
directives. The directives alter the behaviour of the file handle.
-When LAYER is present using binmode on text file makes sense.
+When LAYER is present using binmode on a text file makes sense.
If LAYER is omitted or specified as C<:raw> the filehandle is made
suitable for passing binary data. This includes turning off possible CRLF
If VARIABLE is omitted, it chomps C<$_>. Example:
while (<>) {
- chomp; # avoid \n on last field
- @array = split(/:/);
- # ...
+ chomp; # avoid \n on last field
+ @array = split(/:/);
+ # ...
}
If VARIABLE is a hash, it chomps the hash's values, but not its keys.
chomp($pattern = <STDIN>);
($login,$pass,$uid,$gid) = getpwnam($user)
- or die "$user not in passwd file";
+ or die "$user not in passwd file";
- @ary = glob($pattern); # expand filenames
+ @ary = glob($pattern); # expand filenames
chown $uid, $gid, @ary;
On most systems, you are not allowed to change the ownership of the
open(OUTPUT, '|sort >foo') # pipe to sort
or die "Can't start sort: $!";
- #... # print stuff to output
- close OUTPUT # wait for sort to finish
+ #... # print stuff to output
+ close OUTPUT # wait for sort to finish
or warn $! ? "Error closing sort pipe: $!"
: "Exit status $? from sort";
- open(INPUT, 'foo') # get sort's results
+ open(INPUT, 'foo') # get sort's results
or die "Can't open 'foo' for input: $!";
FILEHANDLE may be an expression whose value can be used as an indirect
block, it may be more entertaining.
while (EXPR) {
- ### redo always comes here
- do_something;
+ ### redo always comes here
+ do_something;
} continue {
- ### next always comes here
- do_something_else;
- # then back the top to re-check EXPR
+ ### next always comes here
+ do_something_else;
+ # then back the top to re-check EXPR
}
### last always comes here
Creates a digest 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 munitions).
+been extirpated as a potential munition).
crypt() is a one-way hash function. The PLAINTEXT and SALT is turned
into a short string, called a digest, which is returned. The same
Traditionally the result is a string of 13 bytes: two first bytes of
the salt, followed by 11 bytes from the set C<[./0-9A-Za-z]>, and only
-the first eight bytes of the digest string mattered, but alternative
+the first eight bytes of PLAINTEXT mattered. But alternative
hashing schemes (like MD5), higher level security schemes (like C2),
and implementations on non-UNIX platforms may produce different
strings.
system "stty echo";
if (crypt($word, $pwd) ne $pwd) {
- die "Sorry...\n";
+ die "Sorry...\n";
} else {
- print "ok\n";
+ print "ok\n";
}
Of course, typing in your own password to whoever asks you
# print out history file offsets
dbmopen(%HIST,'/usr/lib/news/history',0666);
while (($key,$val) = each %HIST) {
- print $key, ' = ', unpack('L',$val), "\n";
+ print $key, ' = ', unpack('L',$val), "\n";
}
dbmclose(%HIST);
use DB_File;
dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
- or die "Can't open netscape history file: $!";
+ or die "Can't open netscape history file: $!";
=item defined EXPR
X<defined> X<undef> X<undefined>
print if defined $switch{'D'};
print "$val\n" while defined($val = pop(@ary));
die "Can't readlink $sym: $!"
- unless defined($value = readlink $sym);
+ unless defined($value = readlink $sym);
sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
$debugging = 0 unless defined $debugging;
The following (inefficiently) deletes all the values of %HASH and @ARRAY:
foreach $key (keys %HASH) {
- delete $HASH{$key};
+ delete $HASH{$key};
}
foreach $index (0 .. $#ARRAY) {
- delete $ARRAY[$index];
+ delete $ARRAY[$index];
}
And so do these:
But both of these are slower than just assigning the empty list
or undefining %HASH or @ARRAY:
- %HASH = (); # completely empty %HASH
- undef %HASH; # forget %HASH ever existed
+ %HASH = (); # completely empty %HASH
+ undef %HASH; # forget %HASH ever existed
- @ARRAY = (); # completely empty @ARRAY
- undef @ARRAY; # forget @ARRAY ever existed
+ @ARRAY = (); # completely empty @ARRAY
+ undef @ARRAY; # forget @ARRAY ever existed
Note that the EXPR can be arbitrarily complicated as long as the final
operation is a hash element, array element, hash slice, or array slice
delete $ref->[$x][$y][$index];
delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
+The C<delete local EXPR> construct can also be used to localize the deletion
+of array/hash elements to the current block.
+See L<perlsub/"Localized deletion of elements of composite types">.
+
=item die LIST
X<die> X<throw> X<exception> X<raise> X<$@> X<abort>
See also exit(), warn(), and the Carp module.
-If LIST is empty and C<$@> already contains a value (typically from a
+If the output is empty and C<$@> already contains a value (typically from a
previous eval) that value is reused after appending C<"\t...propagated">.
This is useful for propagating exceptions:
eval { ... };
die unless $@ =~ /Expected exception/;
-If LIST is empty and C<$@> contains an object reference that has a
+If the output is empty and C<$@> contains an object reference that has a
C<PROPAGATE> method, that method will be called with additional file
and line number parameters. The return value replaces the value in
C<$@>. i.e. as if C<< $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; >>
even inside eval()ed blocks/strings! If one wants the hook to do
nothing in such situations, put
- die @_ if $^S;
+ die @_ if $^S;
as the first line of the handler (see L<perlvar/$^S>). Because
this promotes strange action at a distance, this counterintuitive
# read in config files: system first, then user
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;
- }
+ {
+ 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;
+ }
}
=item dump LABEL
only in a different order:
while (($key,$value) = each %ENV) {
- print "$key=$value\n";
+ print "$key=$value\n";
}
See also C<keys>, C<values> and C<sort>.
# reset line numbering on each input file
while (<>) {
- next if /^\s*#/; # skip comments
- print "$.\t$_";
+ next if /^\s*#/; # skip comments
+ print "$.\t$_";
} continue {
- close ARGV if eof; # Not eof()!
+ close ARGV if eof; # Not eof()!
}
# insert dashes just before last line of last file
while (<>) {
- if (eof()) { # check for end of last file
- print "--------------\n";
- }
- print;
- last if eof(); # needed if we're reading from a terminal
+ if (eof()) { # check for end of last file
+ print "--------------\n";
+ }
+ print;
+ last if eof(); # needed if we're reading from a terminal
}
Practical hint: you almost never need to use C<eof> in Perl, because the
determined.
If there is a syntax error or runtime error, or a C<die> statement is
-executed, an undefined value is returned by C<eval>, and C<$@> is set to the
+executed, C<eval> returns an undefined value in scalar context
+or an empty list in list context, and C<$@> is set to the
error message. If there was no error, C<$@> is guaranteed to be a null
string. Beware that using C<eval> neither silences perl from printing
warnings to STDERR, nor does it stuff the text of warning messages into C<$@>.
is implemented. It is also Perl's exception trapping mechanism, where
the die operator is used to raise exceptions.
+If you want to trap errors when loading an XS module, some problems with
+the binary interface (such as Perl version skew) may be fatal even with
+C<eval> unless C<$ENV{PERL_DL_NONLAZY}> is set. See L<perlrun>.
+
If the code to be executed doesn't vary, you may use the eval-BLOCK
form to trap run-time errors without incurring the penalty of
recompiling each time. The error, if any, is still returned in C<$@>.
eval '$answer = $a / $b'; warn $@ if $@;
# a compile-time error
- eval { $answer = }; # WRONG
+ eval { $answer = }; # WRONG
# a run-time error
- eval '$answer ='; # sets $@
+ eval '$answer ='; # sets $@
Using the C<eval{}> form as an exception trap in libraries does have some
issues. Due to the current arguably broken state of C<__DIE__> hooks, you
With an C<eval>, you should be especially careful to remember what's
being looked at when:
- eval $x; # CASE 1
- eval "$x"; # CASE 2
+ eval $x; # CASE 1
+ eval "$x"; # CASE 2
- eval '$x'; # CASE 3
- eval { $x }; # CASE 4
+ eval '$x'; # CASE 3
+ eval { $x }; # CASE 4
- eval "\$$x++"; # CASE 5
- $$x++; # CASE 6
+ eval "\$$x++"; # CASE 5
+ $$x++; # CASE 6
Cases 1 and 2 above behave identically: they run the code contained in
the variable $x. (Although case 2 has misleading double quotes making
particular situation, you can just use symbolic references instead, as
in case 6.
+The assignment to C<$@> occurs before restoration of localised variables,
+which means a temporary is required if you want to mask some but not all
+errors:
+
+ # alter $@ on nefarious repugnancy only
+ {
+ my $e;
+ {
+ local $@; # protect existing $@
+ eval { test_repugnancy() };
+ # $@ =~ /nefarious/ and die $@; # DOES NOT WORK
+ $@ =~ /nefarious/ and $e = $@;
+ }
+ die $e if defined $e
+ }
+
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.
the list.) Example:
$shell = '/bin/csh';
- exec $shell '-sh'; # pretend it's a login shell
+ exec $shell '-sh'; # pretend it's a login shell
or, more directly,
- exec {'/bin/csh'} '-sh'; # pretend it's a login shell
+ exec {'/bin/csh'} '-sh'; # pretend it's a login shell
When the arguments get executed via the system shell, results will
be subject to its quirks and capabilities. See L<perlop/"`STRING`">
Given an expression that specifies a hash element or array element,
returns true if the specified element in the hash or array has ever
-been initialized, even if the corresponding value is undefined. The
-element is not autovivified if it doesn't exist.
+been initialized, even if the corresponding value is undefined.
- print "Exists\n" if exists $hash{$key};
- print "Defined\n" if defined $hash{$key};
+ print "Exists\n" if exists $hash{$key};
+ print "Defined\n" if defined $hash{$key};
print "True\n" if $hash{$key};
- print "Exists\n" if exists $array[$index];
- print "Defined\n" if defined $array[$index];
+ print "Exists\n" if exists $array[$index];
+ print "Defined\n" if defined $array[$index];
print "True\n" if $array[$index];
A hash or array element can be true only if it's defined, and defined if
method that makes it spring into existence the first time that it is
called -- see L<perlsub>.
- print "Exists\n" if exists &subroutine;
- print "Defined\n" if defined &subroutine;
+ print "Exists\n" if exists &subroutine;
+ print "Defined\n" if defined &subroutine;
Note that the EXPR can be arbitrarily complicated as long as the final
operation is a hash or array key lookup or subroutine name:
- if (exists $ref->{A}->{B}->{$key}) { }
- if (exists $hash{A}{B}{$key}) { }
+ if (exists $ref->{A}->{B}->{$key}) { }
+ if (exists $hash{A}{B}{$key}) { }
- if (exists $ref->{A}->{B}->[$ix]) { }
- if (exists $hash{A}{B}[$ix]) { }
+ if (exists $ref->{A}->{B}->[$ix]) { }
+ if (exists $hash{A}{B}[$ix]) { }
if (exists &{$ref->{A}{B}{$key}}) { }
This happens anywhere the arrow operator is used, including even:
undef $ref;
- if (exists $ref->{"Some key"}) { }
- print $ref; # prints HASH(0x80d3d5c)
+ 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
Use of a subroutine call, rather than a subroutine name, as an argument
to exists() is an error.
- exists ⊂ # OK
- exists &sub(); # Error
+ exists ⊂ # OK
+ exists &sub(); # Error
=item exit EXPR
X<exit> X<terminate> X<abort>
use Fcntl;
fcntl($filehandle, F_GETFL, $packed_return_buffer)
- or die "can't fcntl F_GETFL: $!";
+ or die "can't fcntl F_GETFL: $!";
You don't have to check for C<defined> on the return from C<fcntl>.
Like C<ioctl>, it maps a C<0> return from the system call into
same underlying descriptor:
if (fileno(THIS) == fileno(THAT)) {
- print "THIS and THAT are dups\n";
+ print "THIS and THAT are dups\n";
}
(Filehandles connected to memory objects via new features of C<open> may
Here's a mailbox appender for BSD systems.
- use Fcntl ':flock'; # import LOCK_* constants
+ use Fcntl qw(:flock SEEK_END); # import LOCK_* and SEEK_END constants
sub lock {
- flock(MBOX,LOCK_EX);
- # and, in case someone appended
- # while we were waiting...
- seek(MBOX, 0, 2);
+ my ($fh) = @_;
+ flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n";
+
+ # and, in case someone appended while we were waiting...
+ seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n";
}
sub unlock {
- flock(MBOX,LOCK_UN);
+ my ($fh) = @_;
+ flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n";
}
open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}")
- or die "Can't open mailbox: $!";
+ or die "Can't open mailbox: $!";
- lock();
+ lock($mbox);
print $mbox $msg,"\n\n";
- unlock();
+ unlock($mbox);
On systems that support a real flock(), locks are inherited across fork()
calls, whereas those that must resort to the more capricious fcntl()
example:
format Something =
- Test: @<<<<<<<< @||||| @>>>>>
- $str, $%, '$' . int($num)
+ Test: @<<<<<<<< @||||| @>>>>>
+ $str, $%, '$' . int($num)
.
$str = "widget";
to hit enter. For that, try something more like:
if ($BSD_STYLE) {
- system "stty cbreak </dev/tty >/dev/tty 2>&1";
+ system "stty cbreak </dev/tty >/dev/tty 2>&1";
}
else {
- system "stty", '-icanon', 'eol', "\001";
+ system "stty", '-icanon', 'eol', "\001";
}
$key = getc(STDIN);
if ($BSD_STYLE) {
- system "stty -cbreak </dev/tty >/dev/tty 2>&1";
+ system "stty -cbreak </dev/tty >/dev/tty 2>&1";
}
else {
- system "stty", 'icanon', 'eol', '^@'; # ASCII null
+ system "stty", 'icanon', 'eol', '^@'; # ASCII null
}
print "\n";
X<getpriority> X<priority> X<nice>
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
+(See C<getpriority(2)>.) Will raise a fatal exception if used on a
machine that doesn't implement getpriority(2).
=item getpwnam NAME
use Socket qw(:all);
defined(my $tcp = getprotobyname("tcp"))
- or die "Could not determine the protocol number for tcp";
+ or die "Could not determine the protocol number for tcp";
# my $tcp = IPPROTO_TCP; # Alternative
my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
- or die "Could not query TCP_NODELAY socket option: $!";
+ or die "Could not query TCP_NODELAY socket option: $!";
my $nodelay = unpack("I", $packed);
print "Nagle's algorithm is turned ", $nodelay ? "off\n" : "on\n";
EXPR is omitted, C<$_> is used. The C<< <*.c> >> operator is discussed in
more detail in L<perlop/"I/O Operators">.
+Note that C<glob> will split its arguments on whitespace, treating
+each segment as separate pattern. As such, C<glob('*.c *.h')> would
+match all files with a F<.c> or F<.h> extension. The expression
+C<glob('.* *')> would match all files in the current working directory.
+
Beginning with v5.6.0, this operator is implemented using the standard
-C<File::Glob> extension. See L<File::Glob> for details.
+C<File::Glob> extension. See L<File::Glob> for details, including
+C<bsd_glob> which does not treat whitespace as a pattern separator.
=item gmtime EXPR
X<gmtime> X<UTC> X<Greenwich>
=item goto &NAME
-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 C<foreach> loop. It
-also can't be used to go into a construct that is optimized away,
-or to get out of a block or subroutine given to C<sort>.
-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 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 difference being that C does not offer named loops combined with
-loop control. Perl does, and this replaces most structured uses of C<goto>
-in other languages.)
+The C<goto-LABEL> form finds the statement labeled with LABEL and
+resumes execution there. It can't be used to get out of a block or
+subroutine given to C<sort>. 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 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 difference being that C
+does not offer named loops combined with loop control. Perl does, and
+this replaces most structured uses of C<goto> in other languages.)
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
goto ("FOO", "BAR", "GLARCH")[$i];
+Use of C<goto-LABEL> or C<goto-EXPR> to jump into a construct is
+deprecated and will issue a warning. Even then, it may not be used to
+go into any construct that 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.
+
The C<goto-&NAME> form is quite different from the other forms of
C<goto>. In fact, it isn't a goto in the normal sense at all, and
doesn't have the stigma associated with other gotos. Instead, it
Implements the ioctl(2) function. You'll probably first have to say
- require "sys/ioctl.ph"; # probably in $Config{archlib}/sys/ioctl.ph
+ require "sys/ioctl.ph"; # probably in $Config{archlib}/sys/ioctl.ph
to get the correct function definitions. If F<sys/ioctl.ph> doesn't
exist or doesn't have the correct definitions you'll have to roll your
The return value of C<ioctl> (and C<fcntl>) is as follows:
- if OS returns: then Perl returns:
- -1 undefined value
- 0 string "0 but true"
- anything else that number
+ if OS returns: then Perl returns:
+ -1 undefined value
+ 0 string "0 but true"
+ anything else that number
Thus Perl returns true on success and false on failure, yet you can
still easily determine the actual value returned by the operating
@keys = keys %ENV;
@values = values %ENV;
while (@keys) {
- print pop(@keys), '=', pop(@values), "\n";
+ print pop(@keys), '=', pop(@values), "\n";
}
or how about sorted by key:
foreach $key (sort(keys %ENV)) {
- print $key, '=', $ENV{$key}, "\n";
+ print $key, '=', $ENV{$key}, "\n";
}
The returned values are copies of the original keys in the hash, so
Here's a descending numeric sort of a hash by its values:
foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
- printf "%4d %s\n", $hash{$key}, $key;
+ printf "%4d %s\n", $hash{$key}, $key;
}
As an lvalue C<keys> allows you to increase the number of hash buckets
alive (even if only as a zombie) and hasn't changed its UID. See
L<perlport> for notes on the portability of this construct.
-Unlike in the shell, if SIGNAL is negative, it kills
-process groups instead of processes. (On System V, a negative I<PROCESS>
-number will also kill process groups, but that's not portable.) That
-means you usually want to use positive not negative signals. You may also
-use a signal name in quotes.
+Unlike in the shell, if SIGNAL is negative, it kills process groups instead
+of processes. That means you usually want to use positive not negative signals.
+You may also use a signal name in quotes.
+
+The behavior of kill when a I<PROCESS> number is zero or negative depends on
+the operating system. For example, on POSIX-conforming systems, zero will
+signal the current process group and -1 will signal all processes.
See L<perlipc/"Signals"> for more details.
C<continue> block, if any, is not executed:
LINE: while (<STDIN>) {
- last LINE if /^$/; # exit when done with header
- #...
+ last LINE if /^$/; # exit when done with header
+ #...
}
C<last> cannot be used to exit a block which returns a value such as
=item lc
Returns a lowercased version of EXPR. This is the internal function
-implementing the C<\L> escape in double-quoted strings. Respects
-current LC_CTYPE locale if C<use locale> in force. See L<perllocale>
-and L<perlunicode> for more details about locale and Unicode support.
+implementing the C<\L> escape in double-quoted strings.
If EXPR is omitted, uses C<$_>.
+What gets returned depends on several factors:
+
+=over
+
+=item If C<use bytes> is in effect:
+
+=over
+
+=item On EBCDIC platforms
+
+The results are what the C language system call C<tolower()> returns.
+
+=item On ASCII platforms
+
+The results follow ASCII semantics. Only characters C<A-Z> change, to C<a-z>
+respectively.
+
+=back
+
+=item Otherwise, If EXPR has the UTF8 flag set
+
+If the current package has a subroutine named C<ToLower>, it will be used to
+change the case (See L<perlunicode/User-Defined Case Mappings>.)
+Otherwise Unicode semantics are used for the case change.
+
+=item Otherwise, if C<use locale> is in effect
+
+Respects current LC_CTYPE locale. See L<perllocale>.
+
+=item Otherwise, if C<use feature 'unicode_strings'> is in effect:
+
+Unicode semantics are used for the case change. Any subroutine named
+C<ToLower> will not be used.
+
+=item Otherwise:
+
+=over
+
+=item On EBCDIC platforms
+
+The results are what the C language system call C<tolower()> returns.
+
+=item On ASCII platforms
+
+ASCII semantics are used for the case change. The lowercase of any character
+outside the ASCII range is the character itself.
+
+=back
+
+=back
+
=item lcfirst EXPR
X<lcfirst> X<lowercase>
Returns the value of EXPR with the first character lowercased. This
is the internal function implementing the C<\l> escape in
-double-quoted strings. Respects current LC_CTYPE locale if C<use
-locale> in force. See L<perllocale> and L<perlunicode> for more
-details about locale and Unicode support.
+double-quoted strings.
If EXPR is omitted, uses C<$_>.
+This function behaves the same way under various pragma, such as in a locale,
+as L</lc> does.
+
=item length EXPR
X<length> X<size>
be placed in parentheses. See L<perlsub/"Temporary Values via local()">
for details, including issues with tied arrays and hashes.
+The C<delete local EXPR> construct can also be used to localize the deletion
+of array/hash elements to the current block.
+See L<perlsub/"Localized deletion of elements of composite types">.
+
=item localtime EXPR
X<localtime> X<ctime>
Time, false otherwise.
If EXPR is omitted, C<localtime()> uses the current time (as returned
-by the C<time()> built-in).
+by time(3)).
In scalar context, C<localtime()> returns the ctime(3) value:
divided by the natural log of N. For example:
sub log10 {
- my $n = shift;
- return log($n)/log(10);
+ my $n = shift;
+ return log($n)/log(10);
}
See also L</exp> for the inverse operation.
=item m//
-The match operator. See L<perlop>.
+The match operator. See L<perlop/"Regexp Quote-Like Operators">.
=item map BLOCK LIST
X<map>
%hash = ();
foreach (@array) {
- $hash{get_a_key_for($_)} = $_;
+ $hash{get_a_key_for($_)} = $_;
}
Note that C<$_> is an alias to the list value, so it can be used to
the next iteration of the loop:
LINE: while (<STDIN>) {
- next LINE if /^#/; # discard comments
- #...
+ next LINE if /^#/; # discard comments
+ #...
}
Note that if there were a C<continue> block on the above, it would get
open ARTICLE or die "Can't find article $ARTICLE: $!\n";
while (<ARTICLE>) {...
- open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved)
+ open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved)
# if the open fails, output is discarded
- open(my $dbase, '+<', 'dbase.mine') # open for update
- or die "Can't open 'dbase.mine' for update: $!";
+ open(my $dbase, '+<', 'dbase.mine') # open for update
+ or die "Can't open 'dbase.mine' for update: $!";
- open(my $dbase, '+<dbase.mine') # ditto
- or die "Can't open 'dbase.mine' for update: $!";
+ open(my $dbase, '+<dbase.mine') # ditto
+ or die "Can't open 'dbase.mine' for update: $!";
- open(ARTICLE, '-|', "caesar <$article") # decrypt article
- or die "Can't start caesar: $!";
+ open(ARTICLE, '-|', "caesar <$article") # decrypt article
+ or die "Can't start caesar: $!";
- open(ARTICLE, "caesar <$article |") # ditto
- or die "Can't start caesar: $!";
+ open(ARTICLE, "caesar <$article |") # ditto
+ or die "Can't start caesar: $!";
- open(EXTRACT, "|sort >Tmp$$") # $$ is our process id
- or die "Can't start sort: $!";
+ open(EXTRACT, "|sort >Tmp$$") # $$ is our process id
+ or die "Can't start sort: $!";
# in memory files
open(MEMORY,'>', \$var)
- or die "Can't open memory file: $!";
- print MEMORY "foo!\n"; # output will end up in $var
+ or die "Can't open memory file: $!";
+ print MEMORY "foo!\n"; # output will end up in $var
# process argument list of files along with any includes
foreach $file (@ARGV) {
- process($file, 'fh00');
+ process($file, 'fh00');
}
sub process {
- my($filename, $input) = @_;
- $input++; # this is a string increment
- unless (open($input, $filename)) {
- print STDERR "Can't open $filename: $!\n";
- return;
- }
-
- local $_;
- while (<$input>) { # note use of indirection
- if (/^#include "(.*)"/) {
- process($1, $input);
- next;
- }
- #... # whatever
- }
+ my($filename, $input) = @_;
+ $input++; # this is a string increment
+ unless (open($input, $filename)) {
+ print STDERR "Can't open $filename: $!\n";
+ return;
+ }
+
+ local $_;
+ while (<$input>) { # note use of indirection
+ if (/^#include "(.*)"/) {
+ process($1, $input);
+ next;
+ }
+ #... # whatever
+ }
}
See L<perliol> for detailed info on PerlIO.
You may also, in the Bourne shell tradition, specify an EXPR beginning
with C<< '>&' >>, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
-duped (as L<dup(2)>) and opened. You may use C<&> after C<< > >>,
+duped (as C<dup(2)>) and opened. You may use C<&> after C<< > >>,
C<<< >> >>>, C<< < >>, C<< +> >>, C<<< +>> >>>, and C<< +< >>.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
open STDOUT, '>', "foo.out" or die "Can't redirect STDOUT: $!";
open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!";
- select STDERR; $| = 1; # make unbuffered
- select STDOUT; $| = 1; # make unbuffered
+ select STDERR; $| = 1; # make unbuffered
+ select STDOUT; $| = 1; # make unbuffered
- print STDOUT "stdout 1\n"; # this works for
- print STDERR "stderr 1\n"; # subprocesses too
+ print STDOUT "stdout 1\n"; # this works for
+ print STDERR "stderr 1\n"; # subprocesses too
open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!";
open STDERR, ">&OLDERR" or die "Can't dup OLDERR: $!";
If you specify C<< '<&=X' >>, where C<X> is a file descriptor number
or a filehandle, then Perl will do an equivalent of C's C<fdopen> of
-that file descriptor (and not call L<dup(2)>); this is more
+that file descriptor (and not call C<dup(2)>); this is more
parsimonious of file descriptors. For example:
# open for input, reusing the fileno of $fd
will have exactly the opposite restrictions.
-If you want a "real" C C<open> (see L<open(2)> on your system), then you
+If you want a "real" C C<open> (see C<open(2)> on your system), then you
should use the C<sysopen> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()). This is
use IO::Handle;
sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL)
- or die "sysopen $path: $!";
+ or die "sysopen $path: $!";
$oldfh = select(HANDLE); $| = 1; select($oldfh);
print HANDLE "stuff $$\n";
seek(HANDLE, 0, 0);
use IO::File;
#...
sub read_myfile_munged {
- my $ALL = shift;
- my $handle = IO::File->new;
- open($handle, "myfile") or die "myfile: $!";
- $first = <$handle>
- or return (); # Automatically closed here.
- mung $first or die "mung failed"; # Or here.
- return $first, <$handle> if $ALL; # Or here.
- $first; # Or here.
+ my $ALL = shift;
+ my $handle = IO::File->new;
+ open($handle, "myfile") or die "myfile: $!";
+ $first = <$handle>
+ or return (); # Automatically closed here.
+ mung $first or die "mung failed"; # Or here.
+ return $first, <$handle> if $ALL; # Or here.
+ $first; # Or here.
}
See L</seek> for some details about mixing reading and writing.
behavior holds:
package Foo;
- our $bar; # declares $Foo::bar for rest of lexical scope
+ our $bar; # declares $Foo::bar for rest of lexical scope
$bar = 20;
package Bar;
- print $bar; # prints 20, as it refers to $Foo::bar
+ print $bar; # prints 20, as it refers to $Foo::bar
Multiple C<our> declarations with the same name in the same lexical
scope are allowed if they are in different packages. If they happen
use warnings;
package Foo;
- our $bar; # declares $Foo::bar for rest of lexical scope
+ our $bar; # declares $Foo::bar for rest of lexical scope
$bar = 20;
package Bar;
- our $bar = 30; # declares $Bar::bar for rest of lexical scope
- print $bar; # prints 30
+ our $bar = 30; # declares $Bar::bar for rest of lexical scope
+ print $bar; # prints 30
- our $bar; # emits warning but has no other effect
- print $bar; # still prints 30
+ our $bar; # emits warning but has no other effect
+ print $bar; # still prints 30
An C<our> declaration may also have a list of attributes associated
with it.
an integer may be represented by a sequence of 4 bytes that will be
converted to a sequence of 4 characters.
+See L<perlpacktut> for an introduction to this function.
+
The TEMPLATE is a sequence of characters that give the order and type
of values, as follows:
- a A string with arbitrary binary data, will be null padded.
- A A text (ASCII) string, will be space padded.
- Z A null terminated (ASCIZ) string, will be null padded.
+ a A string with arbitrary binary data, will be null padded.
+ A A text (ASCII) string, will be space padded.
+ Z A null terminated (ASCIZ) string, will be null padded.
- b A bit string (ascending bit order inside each byte, like vec()).
- B A bit string (descending bit order inside each byte).
- h A hex string (low nybble first).
- H A hex string (high nybble first).
+ b A bit string (ascending bit order inside each byte, like vec()).
+ B A bit string (descending bit order inside each byte).
+ h A hex string (low nybble first).
+ H A hex string (high nybble first).
- c A signed char (8-bit) value.
- C An unsigned char (octet) value.
+ c A signed char (8-bit) value.
+ C An unsigned char (octet) value.
W An unsigned char value (can be greater than 255).
- s A signed short (16-bit) value.
- S An unsigned short value.
+ s A signed short (16-bit) value.
+ S An unsigned short value.
- l A signed long (32-bit) value.
- L An unsigned long value.
+ l A signed long (32-bit) value.
+ L An unsigned long value.
- q A signed quad (64-bit) value.
- Q An unsigned quad value.
- (Quads are available only if your system supports 64-bit
- integer values _and_ if Perl has been compiled to support those.
+ q A signed quad (64-bit) value.
+ Q An unsigned quad value.
+ (Quads are available only if your system supports 64-bit
+ integer values _and_ if Perl has been compiled to support those.
Causes a fatal error otherwise.)
- i A signed integer value.
- I A unsigned integer value.
- (This 'integer' is _at_least_ 32 bits wide. Its exact
+ i A signed integer value.
+ I A unsigned integer value.
+ (This 'integer' is _at_least_ 32 bits wide. Its exact
size depends on what a local C compiler calls 'int'.)
- n An unsigned short (16-bit) in "network" (big-endian) order.
- N An unsigned long (32-bit) in "network" (big-endian) order.
- v An unsigned short (16-bit) in "VAX" (little-endian) order.
- V An unsigned long (32-bit) in "VAX" (little-endian) order.
+ n An unsigned short (16-bit) in "network" (big-endian) order.
+ N An unsigned long (32-bit) in "network" (big-endian) order.
+ v An unsigned short (16-bit) in "VAX" (little-endian) order.
+ V An unsigned long (32-bit) in "VAX" (little-endian) order.
j A Perl internal signed integer value (IV).
J A Perl internal unsigned integer value (UV).
- f A single-precision float in the native format.
- d A double-precision float in the native format.
+ f A single-precision float in the native format.
+ d A double-precision float in the native format.
- F A Perl internal floating point value (NV) in the native format
- D A long double-precision float in the native format.
- (Long doubles are available only if your system supports long
- double values _and_ if Perl has been compiled to support those.
+ F A Perl internal floating point value (NV) in the native format
+ D A long double-precision float in the native format.
+ (Long doubles are available only if your system supports long
+ double values _and_ if Perl has been compiled to support those.
Causes a fatal error otherwise.)
- p A pointer to a null-terminated string.
- P A pointer to a structure (fixed-length string).
+ p A pointer to a null-terminated string.
+ P A pointer to a structure (fixed-length string).
- u A uuencoded string.
- U A Unicode character number. Encodes to a character in character mode
+ u A uuencoded string.
+ U A Unicode character number. Encodes to a character in character mode
and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in byte mode.
- w A BER compressed integer (not an ASN.1 BER, see perlpacktut for
- details). 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.
+ w A BER compressed integer (not an ASN.1 BER, see perlpacktut for
+ details). 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.
- x A null byte.
- X Back up a byte.
- @ Null fill or truncate to absolute position, counted from the
+ x A null byte.
+ X Back up a byte.
+ @ Null fill or truncate to absolute position, counted from the
start of the innermost ()-group.
. Null fill or truncate to absolute position specified by value.
- ( Start of a ()-group.
+ ( Start of a ()-group.
One or more of the modifiers below may optionally follow some letters in the
TEMPLATE (the second column lists the letters for which the modifier is
may be larger. This is an issue mainly in 64-bit platforms. You can
see whether using C<!> makes any difference by
- print length(pack("s")), " ", length(pack("s!")), "\n";
- print length(pack("l")), " ", length(pack("l!")), "\n";
+ print length(pack("s")), " ", length(pack("s!")), "\n";
+ print length(pack("l")), " ", length(pack("l!")), "\n";
C<i!> and C<I!> also work but only because of completeness;
they are identical to C<i> and C<I>.
4-byte integer 0x12345678 (305419896 decimal) would be ordered natively
(arranged in and handled by the CPU registers) into bytes as
- 0x12 0x34 0x56 0x78 # big-endian
- 0x78 0x56 0x34 0x12 # little-endian
+ 0x12 0x34 0x56 0x78 # big-endian
+ 0x78 0x56 0x34 0x12 # little-endian
Basically, the Intel and VAX CPUs are little-endian, while everybody
else, for example Motorola m68k/88k, PPC, Sparc, HP PA, Power, and
Some systems may have even weirder byte orders such as
- 0x56 0x78 0x12 0x34
- 0x34 0x12 0x78 0x56
+ 0x56 0x78 0x12 0x34
+ 0x34 0x12 0x78 0x56
You can see your system's preference with
- print join(" ", map { sprintf "%#02x", $_ }
+ print join(" ", map { sprintf "%#02x", $_ }
unpack("W*",pack("L",0x12345678))), "\n";
The byteorder on the platform where Perl was built is also available
via L<Config>:
- use Config;
- print $Config{byteorder}, "\n";
+ use Config;
+ print $Config{byteorder}, "\n";
Byteorders C<'1234'> and C<'12345678'> are little-endian, C<'4321'>
and C<'87654321'> are big-endian.
# "@utmp1" eq "@utmp2"
sub bintodec {
- unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
+ unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
$foo = pack('sx2l', 12, 34);
The same template may generally also be used in unpack().
-=item package NAMESPACE
-X<package> X<module> X<namespace>
+=item package NAMESPACE VERSION
+X<package> X<module> X<namespace> X<version>
-=item package
+=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
C<$::sail> is equivalent to C<$main::sail> (as well as to C<$main'sail>,
still seen in older code).
+If VERSION is provided, C<package> also sets the C<$VERSION> variable in the
+given namespace. VERSION must be be a numeric literal or v-string; it is
+parsed exactly the same way as a VERSION argument to C<use MODULE VERSION>.
+C<$VERSION> should only be set once per package.
+
See L<perlmod/"Packages"> for more information about packages, modules,
and classes. See L<perlsub> for other scoping issues.
print { $files[$i] } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";
+Printing to a closed pipe or socket will generate a SIGPIPE signal. See
+L<perlipc> for more on signal handling.
+
=item printf FILEHANDLE FORMAT, LIST
X<printf>
LIST. Has the same effect as
for $value (LIST) {
- $ARRAY[++$#ARRAY] = $value;
+ $ARRAY[++$#ARRAY] = $value;
}
but is more efficient. Returns the number of elements in the array following
=item qq/STRING/
-=item qr/STRING/
-
=item qx/STRING/
=item qw/STRING/
-Generalized quotes. See L<perlop/"Regexp Quote-Like Operators">.
+Generalized quotes. See L<perlop/"Quote-Like Operators">.
+
+=item qr/STRING/
+
+Regexp-like quote. See L<perlop/"Regexp Quote-Like Operators">.
=item quotemeta EXPR
X<quotemeta> X<metacharacter>
If EXPR is omitted, uses C<$_>.
+quotemeta (and C<\Q> ... C<\E>) are useful when interpolating strings into
+regular expressions, because by default an interpolated variable will be
+considered a mini-regular expression. For example:
+
+ my $sentence = 'The quick brown fox jumped over the lazy dog';
+ my $substring = 'quick.*?fox';
+ $sentence =~ s{$substring}{big bad wolf};
+
+Will cause C<$sentence> to become C<'The big bad wolf jumped over...'>.
+
+On the other hand:
+
+ my $sentence = 'The quick brown fox jumped over the lazy dog';
+ my $substring = 'quick.*?fox';
+ $sentence =~ s{\Q$substring\E}{big bad wolf};
+
+Or:
+
+ my $sentence = 'The quick brown fox jumped over the lazy dog';
+ my $substring = 'quick.*?fox';
+ my $quoted_substring = quotemeta($substring);
+ $sentence =~ s{$quoted_substring}{big bad wolf};
+
+Will both leave the sentence as is. Normally, when accepting string input from
+the user, quotemeta() or C<\Q> must be used.
+
=item rand EXPR
X<rand> X<random>
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
closedir $dh;
+As of Perl 5.11.2 you can use a bare C<readdir> in a C<while> loop,
+which will set C<$_> on every iteration.
+
+ opendir(my $dh, $some_dir) || die;
+ while(readdir $dh) {
+ print "$some_dir/$_\n";
+ }
+ closedir $dh;
+
=item readline EXPR
=item readline
Reads from the filehandle whose typeglob is contained in EXPR (or from
*ARGV if EXPR is not provided). In scalar context, each call reads and
returns the next line, until end-of-file is reached, whereupon the
-subsequent call returns undef. In list context, reads until end-of-file
+subsequent call returns C<undef>. In list context, 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>, when readline() is in scalar
+When C<$/> is set to C<undef>, when C<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.
operator is discussed in more detail in L<perlop/"I/O Operators">.
$line = <STDIN>;
- $line = readline(*STDIN); # same thing
+ $line = readline(*STDIN); # same thing
-If readline encounters an operating system error, C<$!> will be set with the
-corresponding error message. It can be helpful to check C<$!> when you are
-reading from filehandles you don't trust, such as a tty or a socket. The
-following example uses the operator form of C<readline>, and takes the necessary
-steps to ensure that C<readline> was successful.
+If C<readline> encounters an operating system error, C<$!> will be set
+with the corresponding error message. It can be helpful to check
+C<$!> when you are reading from filehandles you don't trust, such as a
+tty or a socket. The following example uses the operator form of
+C<readline> and dies if the result is not defined.
- for (;;) {
- undef $!;
- unless (defined( $line = <> )) {
- die $! if $!;
- last; # reached EOF
+ while ( ! eof($fh) ) {
+ defined( $_ = <$fh> ) or die "readline failed: $!";
+ ...
+ }
+
+Note that you have can't handle C<readline> errors that way with the
+C<ARGV> filehandle. In that case, you have to open each element of
+C<@ARGV> yourself since C<eof> handles C<ARGV> differently.
+
+ foreach my $arg (@ARGV) {
+ open(my $fh, $arg) or warn "Can't open $arg: $!";
+
+ while ( ! eof($fh) ) {
+ defined( $_ = <$fh> )
+ or die "readline failed for $arg: $!";
+ ...
}
- # ...
}
=item readlink EXPR
# a simpleminded Pascal comment stripper
# (warning: assumes no { or } in strings)
LINE: while (<STDIN>) {
- while (s|({.*}.*){.*}|$1 |) {}
- s|{.*}| |;
- if (s|{.*| |) {
- $front = $_;
- while (<STDIN>) {
- if (/}/) { # end of comment?
- s|^|$front\{|;
- redo LINE;
- }
- }
- }
- print;
+ while (s|({.*}.*){.*}|$1 |) {}
+ s|{.*}| |;
+ if (s|{.*| |) {
+ $front = $_;
+ while (<STDIN>) {
+ if (/}/) { # end of comment?
+ s|^|$front\{|;
+ redo LINE;
+ }
+ }
+ }
+ print;
}
C<redo> cannot be used to retry a block which returns a value such as
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";
+ print "r is a reference to a hash.\n";
}
unless (ref($r)) {
- print "r is not a reference at all.\n";
+ print "r is not a reference at all.\n";
}
The return value C<LVALUE> indicates a reference to an lvalue that is not
versions of Perl that do not support this syntax. The equivalent numeric
version should be used instead.
- require v5.6.1; # run time version check
- require 5.6.1; # ditto
- require 5.006_001; # ditto; preferred for backwards compatibility
+ require v5.6.1; # run time version check
+ require 5.6.1; # ditto
+ require 5.006_001; # ditto; preferred for backwards compatibility
Otherwise, C<require> demands that a library file be included if it
hasn't already been included. The file is included via the do-FILE
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:
then this subroutine is expected to generate one line of source code per
call, writing the line into C<$_> and returning 1, then returning 0 at
"end of file". If there is a filehandle, then the subroutine will be
-called to act a simple source filter, with the line as read in C<$_>.
+called to act as a simple source filter, with the line as read in C<$_>.
Again, return 1 for each valid line, and 0 after all lines have been
returned.
push @INC, \&my_sub;
sub my_sub {
- my ($coderef, $filename) = @_; # $coderef is \&my_sub
- ...
+ my ($coderef, $filename) = @_; # $coderef is \&my_sub
+ ...
}
or:
push @INC, [ \&my_sub, $x, $y, ... ];
sub my_sub {
- my ($arrayref, $filename) = @_;
- # Retrieve $x, $y, ...
- my @parameters = @$arrayref[1..$#$arrayref];
- ...
+ my ($arrayref, $filename) = @_;
+ # Retrieve $x, $y, ...
+ my @parameters = @$arrayref[1..$#$arrayref];
+ ...
}
If the hook is an object, it must provide an INC method that will be
package Foo;
sub new { ... }
sub Foo::INC {
- my ($self, $filename) = @_;
- ...
+ my ($self, $filename) = @_;
+ ...
}
# In the main program
- push @INC, new Foo(...);
+ push @INC, Foo->new(...);
Note that these hooks are also permitted to set the %INC entry
corresponding to the files they have loaded. See L<perlvar/%INC>.
those letters are reset to their pristine state. If the expression is
omitted, one-match searches (C<?pattern?>) are reset to match again. Resets
only variables or searches in the current package. Always returns
-1. Examples:
+1. Examples:
- reset 'X'; # reset all X variables
- reset 'a-z'; # reset lower case variables
- reset; # just reset ?one-time? searches
+ reset 'X'; # reset all X variables
+ reset 'a-z'; # reset lower case variables
+ 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
elements of LIST and returns a string value with all characters
in the opposite order.
- print reverse <>; # line tac, last line first
+ print join(", ", reverse "world", "Hello"); # Hello, world
- undef $/; # for efficiency of <>
- print scalar reverse <>; # character tac, last line tsrif
+ print scalar reverse "dlrow ,", "olleH"; # Hello, world
Used without arguments in scalar context, reverse() reverses C<$_>.
+ $_ = "dlrow ,olleH";
+ print reverse; # No output, list context
+ print scalar reverse; # Hello, world
+
+Note that reversing an array to itself (as in C<@a = reverse @a>) will
+preserve non-existent elements whenever possible, i.e. for non magical
+arrays or tied arrays with C<EXISTS> and C<DELETE> methods.
+
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, such as from a DBM file.
- %by_name = reverse %by_address; # Invert the hash
+ %by_name = reverse %by_address; # Invert the hash
=item rewinddir DIRHANDLE
X<rewinddir>
=item s///
-The substitution operator. See L<perlop>.
+The substitution operator. See L<perlop/"Regexp Quote-Like Operators">.
=item say FILEHANDLE LIST
X<say>
The following single statement:
- print uc(scalar(&foo,$bar)),$baz;
+ print uc(scalar(&foo,$bar)),$baz;
is the moral equivalent of these two:
- &foo;
- print(uc($bar),$baz);
+ &foo;
+ print(uc($bar),$baz);
See L<perlop> for more details on unary operators and the comma operator.
cantankerous), then you may need something more like this:
for (;;) {
- for ($curpos = tell(FILE); $_ = <FILE>;
+ for ($curpos = tell(FILE); $_ = <FILE>;
$curpos = tell(FILE)) {
- # search for some stuff and put it into files
- }
- sleep($for_a_while);
- seek(FILE, $curpos, 0);
+ # search for some stuff and put it into files
+ }
+ sleep($for_a_while);
+ seek(FILE, $curpos, 0);
}
=item seekdir DIRHANDLE,POS
subroutine:
sub fhbits {
- my(@fhlist) = split(' ',$_[0]);
- my($bits);
- for (@fhlist) {
- vec($bits,fileno($_),1) = 1;
- }
- $bits;
+ my(@fhlist) = split(' ',$_[0]);
+ my($bits);
+ for (@fhlist) {
+ vec($bits,fileno($_),1) = 1;
+ }
+ $bits;
}
$rin = fhbits('STDIN TTY SOCK');
disables the file descriptor in any forked copies in other
processes.
+Returns C<1> for success. In the case of error, returns C<undef> if
+the first argument is not a valid filehandle, or returns C<0> and sets
+C<$!> for any other failure.
+
=item sin EXPR
X<sin> X<sine> X<asin> X<arcsine>
=item sleep
Causes the script to sleep for EXPR seconds, or forever if no EXPR.
+Returns the number of seconds actually slept.
+
May be interrupted if the process receives a signal such as C<SIGALRM>.
-Returns the number of seconds actually slept. You probably cannot
-mix C<alarm> and C<sleep> calls, because C<sleep> is often implemented
-using C<alarm>.
+
+ eval {
+ local $SIG{ALARM} = sub { die "Alarm!\n" };
+ sleep;
+ };
+ die $@ unless $@ eq "Alarm!\n";
+
+You probably cannot mix C<alarm> and C<sleep> calls, because C<sleep>
+is often implemented using C<alarm>.
On some older systems, it may sleep up to a full second less than what
you requested, depending on how it counts seconds. Most modern systems
limited control of the sort. Its rather blunt control of the
underlying algorithm may not persist into future Perls, but the
ability to characterize the input or output in implementation
-independent ways quite probably will. See L<sort>.
+independent ways quite probably will. See L<the sort pragma|sort>.
Examples:
# sort lexically
@articles = sort @files;
-
+
# same thing, but with explicit sort routine
@articles = sort {$a cmp $b} @files;
-
+
# now case-insensitively
@articles = sort {uc($a) cmp uc($b)} @files;
-
+
# same thing in reversed order
@articles = sort {$b cmp $a} @files;
-
+
# sort numerically ascending
@articles = sort {$a <=> $b} @files;
-
+
# sort numerically descending
@articles = sort {$b <=> $a} @files;
-
+
# this sorts the %age hash by value instead of key
# using an in-line function
@eldest = sort { $age{$b} <=> $age{$a} } keys %age;
-
+
# sort using explicit subroutine name
sub byage {
- $age{$a} <=> $age{$b}; # presuming numeric
+ $age{$a} <=> $age{$b}; # presuming numeric
}
@sortedclass = sort byage @class;
-
+
sub backwards { $b cmp $a }
@harry = qw(dog cat x Cain Abel);
@george = qw(gone chased yz Punished Axed);
print sort @harry;
- # prints AbelCaincatdogx
+ # prints AbelCaincatdogx
print sort backwards @harry;
- # prints xdogcatCainAbel
+ # prints xdogcatCainAbel
print sort @george, 'to', @harry;
- # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
+ # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
# inefficiently sort by descending numeric compare using
# the first integer after the first = sign, or the
# whole record case-insensitively otherwise
- @new = sort {
- ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]
- ||
- uc($a) cmp uc($b)
+ my @new = sort {
+ ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]
+ ||
+ uc($a) cmp uc($b)
} @old;
# same thing, but much more efficiently;
# we'll build auxiliary indices instead
# for speed
- @nums = @caps = ();
+ my @nums = @caps = ();
for (@old) {
- push @nums, /=(\d+)/;
- push @caps, uc($_);
+ push @nums, ( /=(\d+)/ ? $1 : undef );
+ push @caps, uc($_);
}
- @new = @old[ sort {
- $nums[$b] <=> $nums[$a]
- ||
- $caps[$a] cmp $caps[$b]
- } 0..$#old
- ];
+ my @new = @old[ sort {
+ $nums[$b] <=> $nums[$a]
+ ||
+ $caps[$a] cmp $caps[$b]
+ } 0..$#old
+ ];
# same thing, but without any temps
@new = map { $_->[0] }
sort { $b->[1] <=> $a->[1]
- ||
- $a->[2] cmp $b->[2]
- } map { [$_, /=(\d+)/, uc($_)] } @old;
+ ||
+ $a->[2] cmp $b->[2]
+ } map { [$_, /=(\d+)/, uc($_)] } @old;
# using a prototype allows you to use any comparison subroutine
# as a sort subroutine (including other package's subroutines)
package other;
- sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here
-
+ sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here
+
package main;
@new = sort other::backwards @old;
-
+
# guarantee stability, regardless of algorithm
use sort 'stable';
@new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
-
+
# force use of mergesort (not portable outside Perl 5.8)
use sort '_mergesort'; # note discouraging _
@new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
+Warning: syntactical care is required when sorting the list returned from
+a function. If you want to sort the list returned by the function call
+C<find_records(@key)>, you can use:
+
+ @contact = sort { $a cmp $b } find_records @key;
+ @contact = sort +find_records(@key);
+ @contact = sort &find_records(@key);
+ @contact = sort(find_records(@key));
+
+If instead you want to sort the array @key with the comparison routine
+C<find_records()> then you can use:
+
+ @contact = sort { find_records() } @key;
+ @contact = sort find_records(@key);
+ @contact = sort(find_records @key);
+ @contact = sort(find_records (@key));
+
If you're using strict, you I<must not> declare $a
and $b as lexicals. They are package globals. That means
-if you're in the C<main> package and type
+that if you're in the C<main> package and type
@articles = sort {$b <=> $a} @files;
The following equivalences hold (assuming C<< $[ == 0 and $#a >= $i >> )
- push(@a,$x,$y) splice(@a,@a,0,$x,$y)
- pop(@a) splice(@a,-1)
- shift(@a) splice(@a,0,1)
- unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
- $a[$i] = $y splice(@a,$i,1,$y)
+ push(@a,$x,$y) splice(@a,@a,0,$x,$y)
+ pop(@a) splice(@a,-1)
+ shift(@a) splice(@a,0,1)
+ unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
+ $a[$i] = $y splice(@a,$i,1,$y)
Example, assuming array lengths are passed before arrays:
- sub aeq { # compare two list values
- my(@a) = splice(@_,0,shift);
- my(@b) = splice(@_,0,shift);
- return 0 unless @a == @b; # same len?
- while (@a) {
- return 0 if pop(@a) ne pop(@b);
- }
- return 1;
+ sub aeq { # compare two list values
+ my(@a) = splice(@_,0,shift);
+ my(@b) = splice(@_,0,shift);
+ return 0 unless @a == @b; # same len?
+ while (@a) {
+ return 0 if pop(@a) ne pop(@b);
+ }
+ return 1;
}
if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
default, empty leading fields are preserved, and empty trailing ones are
deleted. (If all fields are empty, they are considered to be trailing.)
-In scalar context, returns the number of fields found and splits into
-the C<@_> array. Use of split in scalar context is deprecated, however,
-because it clobbers your subroutine arguments.
+In scalar context, returns the number of fields found.
If EXPR is omitted, splits the C<$_> string. If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace). Anything
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way. For example:
- print join(':', split(/ */, 'hi there'));
+ print join(':', split(/ */, 'hi there')), "\n";
produces the output 'h:i:t:h:e:r:e'.
of C<//> to mean "the last successful pattern match". So, for C<split>,
the following:
- print join(':', split(//, 'hi there'));
+ print join(':', split(//, 'hi there')), "\n";
produces the output 'h:i: :t:h:e:r:e'.
when LIMIT is given and is not 0), regardless of the length of the match.
For example:
- print join(':', split(//, 'hi there!', -1));
- print join(':', split(/\W/, 'hi there!', -1));
+ print join(':', split(//, 'hi there!', -1)), "\n";
+ print join(':', split(/\W/, 'hi there!', -1)), "\n";
produce the output 'h:i: :t:h:e:r:e:!:' and 'hi:there:', respectively,
both with an empty trailing field.
If you had the entire header of a normal Unix email message in $header,
you could split it up into fields and their values this way:
- $header =~ s/\n\s+/ /g; # fix continuation lines
+ $header =~ s/\n(?=\s)//g; # fix continuation lines
%hdrs = (UNIX_FROM => split /^(\S*?):\s*/m, $header);
The pattern C</PATTERN/> may be replaced with an expression to specify
chomp;
($login, $passwd, $uid, $gid,
$gcos, $home, $shell) = split(/:/);
- #...
+ #...
}
As with regular pattern matching, any capturing parentheses that are not
Returns a string formatted by the usual C<printf> conventions of the C
library function C<sprintf>. See below for more details
-and see L<sprintf(3)> or L<printf(3)> on your system for an explanation of
+and see C<sprintf(3)> or C<printf(3)> on your system for an explanation of
the general principles.
For example:
Perl's C<sprintf> permits the following universally-known conversions:
- %% a percent sign
- %c a character with the given number
- %s a string
- %d a signed integer, in decimal
- %u an unsigned integer, in decimal
- %o an unsigned integer, in octal
- %x an unsigned integer, in hexadecimal
- %e a floating-point number, in scientific notation
- %f a floating-point number, in fixed decimal notation
- %g a floating-point number, in %e or %f notation
+ %% a percent sign
+ %c a character with the given number
+ %s a string
+ %d a signed integer, in decimal
+ %u an unsigned integer, in decimal
+ %o an unsigned integer, in octal
+ %x an unsigned integer, in hexadecimal
+ %e a floating-point number, in scientific notation
+ %f a floating-point number, in fixed decimal notation
+ %g a floating-point number, in %e or %f notation
In addition, Perl permits the following widely-supported conversions:
- %X like %x, but using upper-case letters
- %E like %e, but using an upper-case "E"
- %G like %g, but with an upper-case "E" (if applicable)
- %b an unsigned integer, in binary
- %B like %b, but using an upper-case "B" with the # flag
- %p a pointer (outputs the Perl value's address in hexadecimal)
- %n special: *stores* the number of characters output so far
+ %X like %x, but using upper-case letters
+ %E like %e, but using an upper-case "E"
+ %G like %g, but with an upper-case "E" (if applicable)
+ %b an unsigned integer, in binary
+ %B like %b, but using an upper-case "B" with the # flag
+ %p a pointer (outputs the Perl value's address in hexadecimal)
+ %n special: *stores* the number of characters output so far
into the next variable in the parameter list
Finally, for backward (and we do mean "backward") compatibility, Perl
permits these unnecessary but widely-supported conversions:
- %i a synonym for %d
- %D a synonym for %ld
- %U a synonym for %lu
- %O a synonym for %lo
- %F a synonym for %f
+ %i a synonym for %d
+ %D a synonym for %ld
+ %U a synonym for %lu
+ %O a synonym for %lo
+ %F a synonym for %f
Note that the number of exponent digits in the scientific notation produced
by C<%e>, C<%E>, C<%g> and C<%G> for numbers with the modulus of the
one or more of:
- space prefix positive number with a space
- + prefix positive number with a plus sign
+ space prefix non-negative number with a space
+ + prefix non-negative number with a plus sign
- left-justify within the field
0 use zeros, not spaces, to right-justify
# ensure the leading "0" for any octal,
or Perl was specifically compiled to support quads.) You can find out
whether your Perl supports quads via L<Config>:
- use Config;
- ($Config{use64bitint} eq 'define' || $Config{longsize} >= 8) &&
- print "quads\n";
+ use Config;
+ ($Config{use64bitint} eq 'define' || $Config{longsize} >= 8) &&
+ print "quads\n";
For floating point conversions (C<e f g E F G>), numbers are usually assumed
to be the default floating point size on your platform (double or long double),
platform supports them. You can find out whether your Perl supports long
doubles via L<Config>:
- use Config;
- $Config{d_longdbl} eq 'define' && print "long doubles\n";
+ use Config;
+ $Config{d_longdbl} eq 'define' && print "long doubles\n";
You can find out whether Perl considers 'long double' to be the default
floating point size to use on your platform via L<Config>:
Here are some more examples - beware that when using an explicit
index, the C<$> may need to be escaped:
- printf "%2\$d %d\n", 12, 34; # will print "34 12\n"
- printf "%2\$d %d %d\n", 12, 34; # will print "34 12 34\n"
- printf "%3\$d %d %d\n", 12, 34, 56; # will print "56 12 34\n"
- printf "%2\$*3\$d %d\n", 12, 34, 3; # will print " 34 12\n"
+ printf "%2\$d %d\n", 12, 34; # will print "34 12\n"
+ printf "%2\$d %d %d\n", 12, 34; # will print "34 12 34\n"
+ printf "%3\$d %d %d\n", 12, 34, 56; # will print "56 12 34\n"
+ printf "%2\$*3\$d %d\n", 12, 34, 3; # will print " 34 12\n"
=back
last C<stat>, C<lstat>, or filetest are returned. Example:
if (-x $file && (($d) = stat(_)) && $d < 0) {
- print "$file is executable NFS file\n";
+ print "$file is executable NFS file\n";
}
(This works on machines only for which the device number is negative
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;
+ $filename, $sb->size, $sb->mode & 07777,
+ scalar localtime $sb->mtime;
You can import symbolic mode constants (C<S_IF*>) and functions
(C<S_IS*>) from the Fcntl module:
and the C<S_IF*> functions are
- S_IMODE($mode) the part of $mode containing the permission bits
- and the setuid/setgid/sticky bits
+ S_IMODE($mode) the part of $mode containing the permission bits
+ and the setuid/setgid/sticky bits
- S_IFMT($mode) the part of $mode containing the file type
- which can be bit-anded with e.g. S_IFREG
+ S_IFMT($mode) the part of $mode containing the file type
+ which can be bit-anded with e.g. S_IFREG
or with the following functions
# The operators -f, -d, -l, -b, -c, -p, and -S.
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;
+ study;
+ print ".IX foo\n" if /\bfoo\b/;
+ print ".IX bar\n" if /\bbar\b/;
+ print ".IX blurfl\n" if /\bblurfl\b/;
+ # ...
+ print;
}
In searching for C</\bfoo\b/>, only those locations in C<$_> that contain C<f>
$search = 'while (<>) { study;';
foreach $word (@words) {
- $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n";
+ $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n";
}
$search .= "}";
@ARGV = @files;
undef $/;
- eval $search; # this screams
- $/ = "\n"; # put back to normal input delimiter
+ eval $search; # this screams
+ $/ = "\n"; # put back to normal input delimiter
foreach $file (sort keys(%seen)) {
- print $file, "\n";
+ print $file, "\n";
}
=item sub NAME BLOCK
many characters off the end of the string.
my $s = "The black cat climbed the green tree";
- my $color = substr $s, 4, 5; # black
- my $middle = substr $s, 4, -11; # black cat climbed the
- my $end = substr $s, 14; # climbed the green tree
- my $tail = substr $s, -4; # tree
- my $z = substr $s, -4, 2; # tr
+ my $color = substr $s, 4, 5; # black
+ my $middle = substr $s, 4, -11; # black cat climbed the
+ my $end = substr $s, 14; # climbed the green tree
+ my $tail = substr $s, -4; # tree
+ my $z = substr $s, -4, 2; # tr
You can use the substr() function as an lvalue, in which case EXPR
must itself be an lvalue. If you assign something shorter than LENGTH,
Here's an example showing the behavior for boundary cases:
my $name = 'fred';
- substr($name, 4) = 'dy'; # $name is now 'freddy'
- my $null = substr $name, 6, 2; # returns '' (no warning)
- my $oops = substr $name, 7; # returns undef, with warning
- substr($name, 7) = 'gap'; # fatal error
+ substr($name, 4) = 'dy'; # $name is now 'freddy'
+ my $null = substr $name, 6, 2; # returns '' (no warning)
+ my $oops = substr $name, 7; # returns undef, with warning
+ substr($name, 7) = 'gap'; # fatal error
An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument. This allows you to replace
just as you can with splice().
my $s = "The black cat climbed the green tree";
- my $z = substr $s, 14, 7, "jumped from"; # climbed
+ my $z = substr $s, 14, 7, "jumped from"; # climbed
# $s is now "The black cat jumped from the green tree"
Note that the lvalue returned by the 3-arg version of substr() acts as
$x = '1234';
for (substr($x,1,2)) {
- $_ = 'a'; print $x,"\n"; # prints 1a4
- $_ = 'xyz'; print $x,"\n"; # prints 1xyz4
+ $_ = 'a'; print $x,"\n"; # prints 1a4
+ $_ = 'xyz'; print $x,"\n"; # prints 1xyz4
$x = '56789';
- $_ = 'pq'; print $x,"\n"; # prints 5pq9
+ $_ = 'pq'; print $x,"\n"; # prints 5pq9
}
Prior to Perl version 5.9.1, the result of using an lvalue multiple times was
numeric context, you may need to add C<0> to them to force them to look
like numbers. This emulates the C<syswrite> function (or vice versa):
- require 'syscall.ph'; # may need to run h2ph
+ require 'syscall.ph'; # may need to run h2ph
$s = "hi there\n";
syscall(&SYS_write, fileno(STDOUT), $s, length $s);
from the Fcntl module. Use of the constants is also more portable
than relying on 0, 1, and 2. For example to define a "systell" function:
- use Fcntl 'SEEK_CUR';
- sub systell { sysseek($_[0], 0, SEEK_CUR) }
+ use Fcntl 'SEEK_CUR';
+ sub systell { sysseek($_[0], 0, SEEK_CUR) }
Returns the new position, or the undefined value on failure. A position
of zero is returned as the string C<"0 but true">; thus C<sysseek> returns
indicates a failure to start the program or an error of the wait(2) system
call (inspect $! for the reason).
+If you'd like to make C<system> (and many other bits of Perl) die on error,
+have a look at the L<autodie> pragma.
+
Like C<exec>, C<system> allows you to lie to a program about its name if
you use the C<system PROGRAM LIST> syntax. Again, see L</exec>.
@args = ("command", "arg1", "arg2");
system(@args) == 0
- or die "system @args failed: $?"
+ or die "system @args failed: $?"
-You can check all the failure possibilities by inspecting
-C<$?> like this:
+If you'd like to manually inspect C<system>'s failure, you can check all
+possible failure modes by inspecting C<$?> like this:
if ($? == -1) {
- print "failed to execute: $!\n";
+ print "failed to execute: $!\n";
}
elsif ($? & 127) {
- printf "child died with signal %d, %s coredump\n",
- ($? & 127), ($? & 128) ? 'with' : 'without';
+ printf "child died with signal %d, %s coredump\n",
+ ($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
- printf "child exited with value %d\n", $? >> 8;
+ printf "child exited with value %d\n", $? >> 8;
}
Alternatively you might inspect the value of C<${^CHILD_ERROR_NATIVE}>
use NDBM_File;
tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
while (($key,$val) = each %HIST) {
- print $key, ' = ', unpack('L',$val), "\n";
+ print $key, ' = ', unpack('L',$val), "\n";
}
untie(%HIST);
=item tr///
-The transliteration operator. Same as C<y///>. See L<perlop>.
+The transliteration operator. Same as C<y///>. See
+L<perlop/"Quote and Quote-like Operators">.
=item truncate FILEHANDLE,LENGTH
X<truncate>
=item uc
Returns an uppercased version of EXPR. 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>
-and L<perlunicode> for more details about locale and Unicode support.
+implementing the C<\U> escape in double-quoted strings.
It does not attempt to do titlecase mapping on initial letters. See
C<ucfirst> for that.
If EXPR is omitted, uses C<$_>.
+This function behaves the same way under various pragma, such as in a locale,
+as L</lc> does.
+
=item ucfirst EXPR
X<ucfirst> X<uppercase>
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> and L<perlunicode>
-for more details about locale and Unicode support.
+the C<\u> escape in double-quoted strings.
If EXPR is omitted, uses C<$_>.
+This function behaves the same way under various pragma, such as in a locale,
+as L</lc> does.
+
=item umask EXPR
X<umask>
=item unlink
-Deletes a list of files. Returns the number of files successfully
-deleted.
+Deletes a list of files. On success, it returns the number of files
+it successfully deleted. On failure, it returns false and sets C<$!>
+(errno):
- $cnt = unlink 'a', 'b', 'c';
+ my $unlinked = unlink 'a', 'b', 'c';
unlink @goners;
- unlink <*.bak>;
+ unlink glob "*.bak";
+
+On error, C<unlink> will not tell you which files it could not remove.
+If you want to know which files you could not remove, try them one
+at a time:
+
+ foreach my $file ( @goners ) {
+ unlink $file or warn "Could not unlink $file: $!";
+ }
-Note: C<unlink> will not attempt to delete directories unless you are superuser
-and the B<-U> flag is supplied to Perl. Even if these conditions are
-met, be warned that unlinking a directory can inflict damage on your
-filesystem. Finally, using C<unlink> on directories is not supported on
-many operating systems. Use C<rmdir> instead.
+Note: C<unlink> will not attempt to delete directories unless you are
+superuser and the B<-U> flag is supplied to Perl. Even if these
+conditions are met, be warned that unlinking a directory can inflict
+damage on your filesystem. Finally, using C<unlink> on directories is
+not supported on many operating systems. Use C<rmdir> instead.
-If LIST is omitted, uses C<$_>.
+If LIST is omitted, C<unlink> uses C<$_>.
=item unpack TEMPLATE,EXPR
X<unpack>
and expands it out into a list of values.
(In scalar context, it returns merely the first value produced.)
-If EXPR is omitted, unpacks the C<$_> string.
+If EXPR is omitted, unpacks the C<$_> string. for an introduction to this function.
+
+See L<perlpacktut> for an introduction to this function.
The string is broken into chunks described by the TEMPLATE. Each chunk
is converted separately to a value. Typically, either the string is a result
Here's a subroutine that does substring:
sub substr {
- my($what,$where,$howmuch) = @_;
- unpack("x$where a$howmuch", $what);
+ my($what,$where,$howmuch) = @_;
+ unpack("x$where a$howmuch", $what);
}
and then there's
computes the same number as the System V sum program:
$checksum = do {
- local $/; # slurp!
- unpack("%32W*",<>) % 65535;
+ local $/; # slurp!
+ unpack("%32W*",<>) % 65535;
};
The following efficiently counts the number of set bits in a bit vector:
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax. The equivalent numeric version should be used instead.
- use v5.6.1; # compile time version check
- use 5.6.1; # ditto
- use 5.006_001; # ditto; preferred for backwards compatibility
+ use v5.6.1; # compile time version check
+ use 5.6.1; # ditto
+ use 5.006_001; # ditto; preferred for backwards compatibility
This is often useful if you need to check the current Perl version before
C<use>ing library modules that won't work with older versions of Perl.
Also, if the specified perl version is greater than or equal to 5.9.5,
C<use VERSION> will also load the C<feature> pragma and enable all
features available in the requested version. See L<feature>.
+Similarly, if the specified perl version is greater than or equal to
+5.11.0, strictures are enabled lexically as with C<use strict> (except
+that the F<strict.pm> file is not actually loaded).
The C<BEGIN> forces the C<require> and C<import> to happen at compile time. The
C<require> makes sure the module is loaded into memory if it hasn't been
which import symbols into the current package (which are effective
through the end of the file).
+Because C<use> takes effect at compile time, it doesn't respect the
+ordinary flow control of the code being compiled. In particular, putting
+a C<use> inside the false branch of a conditional doesn't prevent it
+from being processed. If a module or pragma needs to be loaded only
+conditionally, this can be done using the L<if> pragma:
+
+ use if $] < 5.008, "utf8";
+ use if WANT_WARNINGS, warnings => qw(all);
+
There's a corresponding C<no> command that unimports meanings imported
by C<use>, i.e., it calls C<unimport Module LIST> instead of C<import>.
It behaves exactly as C<import> does with respect to VERSION, an
Note that the values are not copied, which means modifying them will
modify the contents of the hash:
- for (values %hash) { s/foo/bar/g } # modifies %hash values
+ for (values %hash) { s/foo/bar/g } # modifies %hash values
for (@hash{keys %hash}) { s/foo/bar/g } # same
See also C<keys>, C<each>, and C<sort>.
in the same way on big-endian or little-endian machines.
my $foo = '';
- vec($foo, 0, 32) = 0x5065726C; # 'Perl'
+ vec($foo, 0, 32) = 0x5065726C; # 'Perl'
# $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
- print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
-
- vec($foo, 2, 16) = 0x5065; # 'PerlPe'
- vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
- 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, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
- vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
- vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
- # 'l' is "\x6c"
+ print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
+
+ vec($foo, 2, 16) = 0x5065; # 'PerlPe'
+ vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
+ 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, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
+ vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
+ vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
+ # 'l' is "\x6c"
To transform a bit vector into a string or list of 0's and 1's, use these:
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 returned in C<$?>
-and C<{^CHILD_ERROR_NATIVE}>.
+and C<${^CHILD_ERROR_NATIVE}>.
Note that a return value of C<-1> could mean that child processes are
being automatically reaped, as described in L<perlipc>.
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<$?> and C<{^CHILD_ERROR_NATIVE}>. If you say
+The status is returned in C<$?> and C<${^CHILD_ERROR_NATIVE}>. If you say
use POSIX ":sys_wait_h";
#...
do {
- $kid = waitpid(-1, WNOHANG);
+ $kid = waitpid(-1, WNOHANG);
} while $kid > 0;
then you can do a non-blocking wait for all pending zombie processes.
looking for a scalar. Returns the undefined value if the context is
looking for no value (void context).
- return unless defined wantarray; # don't bother doing more
+ return unless defined wantarray; # don't bother doing more
my @a = complex_calculation();
return wantarray ? @a : "@a";
not end in a newline, it appends the same file/line number text as C<die>
does.
-If LIST is empty and C<$@> already contains a value (typically from a
+If the output is empty and C<$@> already contains a value (typically from a
previous eval) that value is used after appending C<"\t...caught">
to C<$@>. This is useful for staying almost, but not entirely similar to
C<die>.
=item y///
-The transliteration operator. Same as C<tr///>. See L<perlop>.
+The transliteration operator. Same as C<tr///>. See
+L<perlop/"Quote and Quote-like Operators">.
=back