also exempt from C<strict 'vars'> errors. A few other names are also
exempt in these ways:
- ENV STDIN
- INC STDOUT
- ARGV STDERR
- ARGVOUT
- SIG
+ ENV STDIN
+ INC STDOUT
+ ARGV STDERR
+ ARGVOUT
+ SIG
In particular, the special C<${^_XYZ}> variables are always taken
to be in package C<main>, regardless of any C<package> declarations
names have reasonable mnemonics, or analogs in the shells.
Nevertheless, if you wish to use long variable names, you need only say:
- use English;
+ use English;
at the top of your program. This aliases all the short names to the long
names in the current package. Some even have medium names, generally
C<$PREMATCH>, C<$MATCH>, or C<$POSTMATCH> it's best to use the C<English>
module without them:
- use English '-no_match_vars';
+ use English '-no_match_vars';
Before you continue, note the sort order for variables. In general, we
first list the variables in case-insensitive, almost-lexigraphical
The default input and pattern-searching space. The following pairs are
equivalent:
- while (<>) {...} # equivalent only in while!
- while (defined($_ = <>)) {...}
+ while (<>) {...} # equivalent only in while!
+ while (defined($_ = <>)) {...}
- /^Subject:/
- $_ =~ /^Subject:/
+ /^Subject:/
+ $_ =~ /^Subject:/
- tr/a-z/A-Z/
- $_ =~ tr/a-z/A-Z/
+ tr/a-z/A-Z/
+ $_ =~ tr/a-z/A-Z/
- chomp
- chomp($_)
+ chomp
+ chomp($_)
Here are the places where Perl will assume C<$_> even if you don't use it:
string or a similar context such as C</.../>, its elements are
separated by this value. Default is a space. For example, this:
- print "The array is: @array\n";
+ print "The array is: @array\n";
is equivalent to this:
- print "The array is: " . join($", @array) . "\n";
+ print "The array is: " . join($", @array) . "\n";
Mnemonic: works in double-quoted context.
invaluable for some testing purposes. It will be reset automatically
across C<fork()> calls.
-Note for Linux users: on Linux, the C functions C<getpid()> and
-C<getppid()> return different values from different threads. In order to
-be portable, this behavior is not reflected by C<$$>, whose value remains
-consistent across threads. If you want to call the underlying C<getpid()>,
-you may use the CPAN module C<Linux::Pid>.
+Note for Linux and Debian GNU/kFreeBSD users: Before Perl v5.16.0 perl
+would emulate POSIX semantics on Linux systems using LinuxThreads, a
+partial implementation of POSIX Threads that has since been superseded
+by the Native POSIX Thread Library (NPTL).
+
+LinuxThreads is now obsolete on Linux, and and caching C<getpid()>
+like this made embedding perl unnecessarily complex (since you'd have
+to manually update the value of $$), so now C<$$> and C<getppid()>
+will always return the same values as the underlying C library.
+
+Debian GNU/kFreeBSD systems also used LinuxThreads up until and
+including the 6.0 release, but after that moved to FreeBSD thread
+semantics, which are POSIX-like.
+
+To see if your system is affected by this discrepancy check if
+C<getconf GNU_LIBPTHREAD_VERSION | grep -q NPTL> returns a false
+value. NTPL threads preserve the POSIX semantics.
Mnemonic: same as shells.
The effective uid of this process. For example:
- $< = $>; # set real to effective uid
- ($<,$>) = ($>,$<); # swap real and effective uids
+ $< = $>; # set real to effective uid
+ ($<,$>) = ($>,$<); # swap real and effective uids
You can change both the effective uid and the real uid at the same
time by using C<POSIX::setuid()>. Changes to C<< $> >> require a check
The subscript separator for multidimensional array emulation. If you
refer to a hash element as
- $foo{$a,$b,$c}
+ $foo{$a,$b,$c}
it really means
- $foo{join($;, $a, $b, $c)}
+ $foo{join($;, $a, $b, $c)}
But don't put
- @foo{$a,$b,$c} # a slice--note the @
+ @foo{$a,$b,$c} # a slice--note the @
which means
- ($foo{$a},$foo{$b},$foo{$c})
+ ($foo{$a},$foo{$b},$foo{$c})
Default is "\034", the same as SUBSEP in B<awk>. If your keys contain
binary data there might not be any safe value for C<$;>.
you should use the C<use lib> pragma to get the machine-dependent
library properly loaded also:
- use lib '/mypath/libdir/';
- use SomeMod;
+ use lib '/mypath/libdir/';
+ use SomeMod;
You can also insert hooks into the file inclusion system by putting Perl
code directly into C<@INC>. Those hooks may be subroutine references,
were compiled with C<-DPERL_EMERGENCY_SBRK> and used Perl's malloc.
Then
- $^M = 'a' x (1 << 16);
+ $^M = 'a' x (1 << 16);
would allocate a 64K buffer for use in an emergency. See the
F<INSTALL> file in the Perl distribution for information on how to
The hash C<%SIG> contains signal handlers for signals. For example:
- sub handler { # 1st argument is signal name
- my($sig) = @_;
- print "Caught a SIG$sig--shutting down\n";
- close(LOG);
- exit(0);
- }
+ sub handler { # 1st argument is signal name
+ my($sig) = @_;
+ print "Caught a SIG$sig--shutting down\n";
+ close(LOG);
+ exit(0);
+ }
- $SIG{'INT'} = \&handler;
- $SIG{'QUIT'} = \&handler;
- ...
- $SIG{'INT'} = 'DEFAULT'; # restore default action
- $SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT
+ $SIG{'INT'} = \&handler;
+ $SIG{'QUIT'} = \&handler;
+ ...
+ $SIG{'INT'} = 'DEFAULT'; # restore default action
+ $SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT
Using a value of C<'IGNORE'> usually has the effect of ignoring the
signal, except for the C<CHLD> signal. See L<perlipc> for more about
Here are some other examples:
- $SIG{"PIPE"} = "Plumber"; # assumes main::Plumber (not recommended)
- $SIG{"PIPE"} = \&Plumber; # just fine; assume current Plumber
- $SIG{"PIPE"} = *Plumber; # somewhat esoteric
- $SIG{"PIPE"} = Plumber(); # oops, what did Plumber() return??
+ $SIG{"PIPE"} = "Plumber"; # assumes main::Plumber (not
+ # recommended)
+ $SIG{"PIPE"} = \&Plumber; # just fine; assume current
+ # Plumber
+ $SIG{"PIPE"} = *Plumber; # somewhat esoteric
+ $SIG{"PIPE"} = Plumber(); # oops, what did Plumber()
+ # return??
Be sure not to use a bareword as the name of a signal handler,
lest you inadvertently call it.
use this to save warnings in a variable, or turn warnings into fatal
errors, like this:
- local $SIG{__WARN__} = sub { die $_[0] };
- eval $proggie;
+ local $SIG{__WARN__} = sub { die $_[0] };
+ eval $proggie;
As the C<'IGNORE'> hook is not supported by C<__WARN__>, you can
disable warnings using the empty subroutine:
- local $SIG{__WARN__} = sub {};
+ local $SIG{__WARN__} = sub {};
The routine indicated by C<$SIG{__DIE__}> is called when a fatal
exception is about to be thrown. The error message is passed as the
first argument. When a C<__DIE__> hook routine returns, the exception
processing continues as it would have in the absence of the hook,
-unless the hook routine itself exits via a C<goto>, a loop exit, or a
-C<die()>. The C<__DIE__> handler is explicitly disabled during the
-call, so that you can die from a C<__DIE__> handler. Similarly for
-C<__WARN__>.
+unless the hook routine itself exits via a C<goto &sub>, a loop exit,
+or a C<die()>. The C<__DIE__> handler is explicitly disabled during
+the call, so that you can die from a C<__DIE__> handler. Similarly
+for C<__WARN__>.
Due to an implementation glitch, the C<$SIG{__DIE__}> hook is called
even inside an C<eval()>. Do not use this to rewrite a pending
segfault. This means that warnings or errors that result from parsing
Perl should be used with extreme caution, like this:
- require Carp if defined $^S;
- Carp::confess("Something wrong") if defined &Carp::confess;
- die "Something wrong, but could not load Carp to give backtrace...
- To see backtrace try starting Perl with -MCarp switch";
+ require Carp if defined $^S;
+ Carp::confess("Something wrong") if defined &Carp::confess;
+ die "Something wrong, but could not load Carp to give "
+ . "backtrace...\n\t"
+ . "To see backtrace try starting Perl with -MCarp switch";
Here the first line will load C<Carp> I<unless> it is the parser who
called the handler. The second line will print backtrace and die if
C<$^V> can be used to determine whether the Perl interpreter executing
a script is in the right range of versions. For example:
- warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1
+ warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1
To convert C<$^V> into its string representation use C<sprintf()>'s
C<"%vd"> conversion:
- printf "version is v%vd\n", $^V; # Perl's version
+ printf "version is v%vd\n", $^V; # Perl's version
See the documentation of C<use VERSION> and C<require VERSION>
for a convenient way to fail if the running Perl interpreter is too old.
You usually can use the value of C<$^X> to re-invoke an independent
copy of the same perl that is currently running, e.g.,
- @first_run = `$^X -le "print int rand 100 for 1..100"`;
+ @first_run = `$^X -le "print int rand 100 for 1..100"`;
But recall that not all operating systems support forking or
capturing of the output of commands, so this complex statement
a command. To convert the value of C<$^X> to a path name, use the
following statements:
- # Build up a set of file names (not command names).
- use Config;
- my $this_perl = $^X;
- if ($^O ne 'VMS') {
- $this_perl .= $Config{_exe}
- unless $this_perl =~ m/$Config{_exe}$/i;
- }
+ # Build up a set of file names (not command names).
+ use Config;
+ my $this_perl = $^X;
+ if ($^O ne 'VMS') {
+ $this_perl .= $Config{_exe}
+ unless $this_perl =~ m/$Config{_exe}$/i;
+ }
Because many operating systems permit anyone with read access to
the Perl program file to make a copy of it, patch the copy, and
this goal, and produce a pathname that can be invoked as a
command or referenced as a file.
- use Config;
- my $secure_perl_path = $Config{perlpath};
- if ($^O ne 'VMS') {
- $secure_perl_path .= $Config{_exe}
- unless $secure_perl_path =~ m/$Config{_exe}$/i;
- }
+ use Config;
+ my $secure_perl_path = $Config{perlpath};
+ if ($^O ne 'VMS') {
+ $secure_perl_path .= $Config{_exe}
+ unless $secure_perl_path =~ m/$Config{_exe}$/i;
+ }
=back
effects. Perl sets these variables when it has a successful match, so
you should check the match result before using them. For instance:
- if( /P(A)TT(ER)N/ ) {
- print "I found $1 and $2\n";
- }
+ if( /P(A)TT(ER)N/ ) {
+ print "I found $1 and $2\n";
+ }
These variables are read-only and dynamically-scoped, unless we note
otherwise.
their value is limited to the block that they are in, as demonstrated
by this bit of code:
- my $outer = 'Wallace and Grommit';
- my $inner = 'Mutt and Jeff';
+ my $outer = 'Wallace and Grommit';
+ my $inner = 'Mutt and Jeff';
- my $pattern = qr/(\S+) and (\S+)/;
+ my $pattern = qr/(\S+) and (\S+)/;
- sub show_n { print "\$1 is $1; \$2 is $2\n" }
+ sub show_n { print "\$1 is $1; \$2 is $2\n" }
- {
- OUTER:
- show_n() if $outer =~ m/$pattern/;
+ {
+ OUTER:
+ show_n() if $outer =~ m/$pattern/;
- INNER: {
- show_n() if $inner =~ m/$pattern/;
- }
+ INNER: {
+ show_n() if $inner =~ m/$pattern/;
+ }
- show_n();
- }
+ show_n();
+ }
The output shows that while in the C<OUTER> block, the values of C<$1>
and C<$2> are from the match against C<$outer>. Inside the C<INNER>
C<$2> return to the values for the match against C<$outer> even though
we have not made another match:
- $1 is Wallace; $2 is Grommit
- $1 is Mutt; $2 is Jeff
- $1 is Wallace; $2 is Grommit
+ $1 is Wallace; $2 is Grommit
+ $1 is Mutt; $2 is Jeff
+ $1 is Wallace; $2 is Grommit
Due to an unfortunate accident of Perl's implementation, C<use
English> imposes a considerable performance penalty on all regular
English>. For that reason, saying C<use English> in libraries is
strongly discouraged unless you import it without the match variables:
- use English '-no_match_vars'
+ use English '-no_match_vars'
The C<Devel::NYTProf> and C<Devel::FindAmpersand>
modules can help you find uses of these
pattern match (not counting any matches hidden within a BLOCK or C<eval()>
enclosed by the current BLOCK). Example:
- local $_ = 'abcdefghi';
- /def/;
- print "$`:$&:$'\n"; # prints abc:def:ghi
+ local $_ = 'abcdefghi';
+ /def/;
+ print "$`:$&:$'\n"; # prints abc:def:ghi
The use of this variable anywhere in a program imposes a considerable
performance penalty on all regular expression matches.
This is useful if you don't know which one of a set of alternative patterns
matched. For example:
- /Version: (.*)|Revision: (.*)/ && ($rev = $+);
+ /Version: (.*)|Revision: (.*)/ && ($rev = $+);
This variable is read-only and dynamically-scoped.
recently matched. For example, to effectively capture text to a variable
(in addition to C<$1>, C<$2>, etc.), replace C<(...)> with
- (?:(...)(?{ $var = $^N }))
+ (?:(...)(?{ $var = $^N }))
By setting and then using C<$var> in this way relieves you from having to
worry about exactly which numbered set of parentheses they are.
For example, C<$+{foo}> is equivalent to C<$1> after the following match:
- 'foo' =~ /(?<foo>foo)/;
+ 'foo' =~ /(?<foo>foo)/;
The keys of the C<%+> hash list only the names of buffers that have
captured (and that are thus associated to defined values).
my $ary = $-{$bufname};
foreach my $idx (0..$#$ary) {
print "\$-{$bufname}[$idx] : ",
- (defined($ary->[$idx]) ? "'$ary->[$idx]'" : "undef"),
+ (defined($ary->[$idx])
+ ? "'$ary->[$idx]'"
+ : "undef"),
"\n";
}
}
would print out:
- $-{A}[0] : '1'
- $-{A}[1] : '3'
- $-{B}[0] : '2'
- $-{B}[1] : '4'
+ $-{A}[0] : '1'
+ $-{A}[1] : '3'
+ $-{B}[0] : '2'
+ $-{B}[1] : '4'
The keys of the C<%-> hash correspond to all buffer names found in
the regular expression.
variables. (Summary lines below for this contain the word HANDLE.)
First you must say
- use IO::Handle;
+ use IO::Handle;
after which you may use either
- method HANDLE EXPR
+ method HANDLE EXPR
or more safely,
- HANDLE->method(EXPR)
+ HANDLE->method(EXPR)
Each method returns the old value of the C<IO::Handle> attribute. The
methods each take an optional EXPR, which, if supplied, specifies the
of the special variables that you have changed. This is one of the
correct ways to read the whole file at once:
- open my $fh, "<", "foo" or die $!;
- local $/; # enable localized slurp mode
- my $content = <$fh>;
- close $fh;
+ open my $fh, "<", "foo" or die $!;
+ local $/; # enable localized slurp mode
+ my $content = <$fh>;
+ close $fh;
But the following code is quite bad:
- open my $fh, "<", "foo" or die $!;
- undef $/; # enable slurp mode
- my $content = <$fh>;
- close $fh;
+ open my $fh, "<", "foo" or die $!;
+ undef $/; # enable slurp mode
+ my $content = <$fh>;
+ close $fh;
since some other module, may want to read data from some file in the
default "line mode", so if the code we have just presented has been
inside some short C<{}> block, you should create one yourself. For
example:
- my $content = '';
- open my $fh, "<", "foo" or die $!;
- {
- local $/;
- $content = <$fh>;
- }
- close $fh;
+ my $content = '';
+ open my $fh, "<", "foo" or die $!;
+ {
+ local $/;
+ $content = <$fh>;
+ }
+ close $fh;
Here is an example of how your own code can go broken:
- for ( 1..3 ){
- $\ = "\r\n";
- nasty_break();
- print "$_";
- }
+ for ( 1..3 ){
+ $\ = "\r\n";
+ nasty_break();
+ print "$_";
+ }
- sub nasty_break {
+ sub nasty_break {
$\ = "\f";
# do something with $_
- }
+ }
You probably expect this code to print the equivalent of
return. The fix is to add C<local()> so the value doesn't leak out of
C<nasty_break()>:
- local $\ = "\f";
+ local $\ = "\f";
It's easy to notice the problem in such a short example, but in more
complicated code you are looking for trouble if you don't localize
set, you'll get the record back in pieces. Trying to set the record
size to zero or less will cause reading in the (rest of the) whole file.
-On VMS, record reads are done with the equivalent of C<sysread>,
-so it's best not to mix record and non-record reads on the same
-file. (This is unlikely to be a problem, because any file you'd
-want to read in record mode is probably unusable in line mode.)
-Non-VMS systems do normal I/O, so it's safe to mix record and
-non-record reads of a file.
+On VMS only, record reads bypass PerlIO layers and any associated
+buffering,so you must not mix record and non-record reads on the
+same filehandle. Record mode mixes with line mode only when the
+same buffering layer is in use for both modes.
+
+If you perform a record read on a FILE with an encoding layer such as
+C<:encoding(latin1)> or C<:utf8>, you may get an invalid string as a
+result, may leave the FILE positioned between characters in the stream
+and may not be reading the number of bytes from the underlying file
+that you specified. This behaviour may change without warning in a
+future version of perl.
See also L<perlport/"Newlines">. Also see L</$.>.
execution of this statement, perl may have set all four special error
variables:
- eval q{
- open my $pipe, "/cdrom/install |" or die $!;
- my @res = <$pipe>;
- close $pipe or die "bad pipe: $?, $!";
- };
+ eval q{
+ open my $pipe, "/cdrom/install |" or die $!;
+ my @res = <$pipe>;
+ close $pipe or die "bad pipe: $?, $!";
+ };
When perl executes the C<eval()> expression, it translates the
C<open()>, C<< <PIPE> >>, and C<close> calls in the C run-time library
X<${^WARNING_BITS}>
The current set of warning checks enabled by the C<use warnings> pragma.
-See the documentation of C<warnings> for more details.
+It has the same scoping as the C<$^H> and C<%^H> variables. The exact
+values are considered internal to the L<warnings> pragma and may change
+between versions of Perl.
-This variable was added in Perl 5.10.
+This variable was added in Perl 5.6.
=item $OS_ERROR
The contents should be an integer; different bits of it are used for
different pragmatic flags. Here's an example:
- sub add_100 { $^H |= 0x100 }
+ sub add_100 { $^H |= 0x100 }
- sub foo {
- BEGIN { add_100() }
- bar->baz($boon);
- }
+ sub foo {
+ BEGIN { add_100() }
+ bar->baz($boon);
+ }
Consider what happens during execution of the BEGIN block. At this point
the BEGIN block has already been compiled, but the body of C<foo()> is still
Substitution of C<BEGIN { add_100() }> block with:
- BEGIN { require strict; strict->import('vars') }
+ BEGIN { require strict; strict->import('vars') }
demonstrates how C<use strict 'vars'> is implemented. Here's a conditional
version of the same lexical pragma:
- BEGIN { require strict; strict->import('vars') if $condition }
+ BEGIN {
+ require strict; strict->import('vars') if $condition
+ }
This variable was added in Perl 5.003.
Mnemonic: Is this version of perl in the right bracket?
-Deprecated in Perl 5.6.
-
=back
=cut