X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlvar.pod;h=6f9bd8d3a684048e020024b9df7b6d392fb80e11;hb=f1c09d8aea4b00e7799b7273c877df50e0fd657a;hp=cdbd53ffe840bfddd8fdaded2b7d38d35dc17798;hpb=d54b56d55c935aeda35fad4725273ab2eb5a71a5;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlvar.pod b/pod/perlvar.pod index cdbd53f..6f9bd8d 100644 --- a/pod/perlvar.pod +++ b/pod/perlvar.pod @@ -44,6 +44,71 @@ A few of these variables are considered "read-only". This means that if you try to assign to this variable, either directly or indirectly through a reference, you'll raise a run-time exception. +You should be very careful when modifying the default values of most +special variables described in this document. In most cases you want +to localize these variables before changing them, since if you don't, +the change may affect other modules which rely on the default values +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; + +But the following code is quite bad: + + 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 +executed, the global value of C<$/> is now changed for any other code +running inside the same Perl interpreter. + +Usually when a variable is localized you want to make sure that this +change affects the shortest scope possible. So unless you are already +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; + +Here is an example of how your own code can go broken: + + for (1..5){ + nasty_break(); + print "$_ "; + } + sub nasty_break { + $_ = 5; + # do something with $_ + } + +You probably expect this code to print: + + 1 2 3 4 5 + +but instead you get: + + 5 5 5 5 5 + +Why? Because nasty_break() modifies C<$_> without localizing it +first. The fix is to add local(): + + local $_ = 5; + +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 +changes to the special variables. + The following list is ordered by scalar variables first, then the arrays, then the hashes. @@ -167,7 +232,7 @@ pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK). (Mnemonic: C<'> often follows a quoted string.) Example: - $_ = 'abcdefghi'; + local $_ = 'abcdefghi'; /def/; print "$`:$&:$'\n"; # prints abc:def:ghi @@ -298,8 +363,8 @@ blindly assume that the next input character belongs to the next paragraph, even if it's a newline. (Mnemonic: / delimits line boundaries when quoting poetry.) - undef $/; # enable "slurp" mode - $_ = ; # whole file now here + local $/; # enable "slurp" mode + local $_ = ; # whole file now here s/\n[ \t]+/ /g; Remember: the value of C<$/> is a string, not a regex. B has to be @@ -310,9 +375,9 @@ scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer. So this: - $/ = \32768; # or \"32768", or \$var_containing_32768 - open(FILE, $myfile); - $_ = ; + local $/ = \32768; # or \"32768", or \$var_containing_32768 + open my $fh, $myfile or die $!; + local $_ = <$fh>; will read a record of no more than 32768 bytes from FILE. If you're not reading from a record-oriented file (or your OS doesn't have @@ -1030,6 +1095,17 @@ lexical scope. See L. The name that the Perl binary itself was executed as, from C's C. This may not be a full pathname, nor even necessarily in your path. +=item ARGV + +The special filehandle that iterates over command-line filenames in +C<@ARGV>. Usually written as the null filehandle in the angle operator +C<< <> >>. Note that currently C only has its magical effect +within the C<< <> >> operator; elsewhere it is just a plain filehandle +corresponding to the last file opened by C<< <> >>. In particular, +passing C<\*ARGV> as a parameter to a function that expects a filehandle +may not cause your function to automatically read the contents of all the +files in C<@ARGV>. + =item $ARGV contains the name of the current file when reading from <>. @@ -1080,6 +1156,12 @@ value is the location of the file found. The C operator uses this hash to determine whether a particular file has already been included. +If the file was loaded via a hook (e.g. a subroutine reference, see +L for a description of these hooks), this hook is +by default inserted into %INC in place of a filename. Note, however, +that the hook may have set the %INC entry by itself to provide some more +specific info. + =item %ENV =item $ENV{expr} @@ -1199,9 +1281,9 @@ To illustrate the differences between these variables, consider the following Perl expression, which uses a single-quoted string: eval q{ - open PIPE, "/cdrom/install |"; - @res = ; - close PIPE or die "bad pipe: $?, $!"; + open my $pipe, "/cdrom/install |" or die $!; + my @res = <$pipe>; + close $pipe or die "bad pipe: $?, $!"; }; After execution of this statement all 4 variables may have been set.