home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perlvar.pod
< prev
next >
Wrap
Text File
|
1996-06-28
|
21KB
|
696 lines
=head1 NAME
perlvar - Perl predefined variables
=head1 DESCRIPTION
=head2 Predefined Names
The following names have special meaning to Perl. Most of the
punctuational names have reasonable mnemonics, or analogues in one of
the shells. Nevertheless, if you wish to use the long variable names,
you just need to say
use English;
at the top of your program. This will alias all the short names to the
long names in the current package. Some of them even have medium names,
generally borrowed from B<awk>.
To go a step further, those variables that depend on the currently
selected filehandle may instead be set by calling an object method on
the FileHandle object. (Summary lines below for this contain the word
HANDLE.) First you must say
use FileHandle;
after which you may use either
method HANDLE EXPR
or
HANDLE->method(EXPR)
Each of the methods returns the old value of the FileHandle attribute.
The methods each take an optional EXPR, which if supplied specifies the
new value for the FileHandle attribute in question. If not supplied,
most of the methods do nothing to the current value, except for
autoflush(), which will assume a 1 for you, just to be different.
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.
=over 8
=item $ARG
=item $_
The default input and pattern-searching space. The following pairs are
equivalent:
while (<>) {...} # only equivalent in while!
while ($_ = <>) {...}
/^Subject:/
$_ =~ /^Subject:/
tr/a-z/A-Z/
$_ =~ tr/a-z/A-Z/
chop
chop($_)
Here are the places where Perl will assume $_ even if you
don't use it:
=over 3
=item *
Various unary functions, including functions like ord() and int(), as well
as the all file tests (C<-f>, C<-d>) except for C<-t>, which defaults to
STDIN.
=item *
Various list functions like print() and unlink().
=item *
The pattern matching operations C<m//>, C<s///>, and C<tr///> when used
without an C<=~> operator.
=item *
The default iterator variable in a C<foreach> loop if no other
variable is supplied.
=item *
The implicit iterator variable in the grep() and map() functions.
=item *
The default place to put an input record when a C<E<lt>FHE<gt>>
operation's result is tested by itself as the sole criterion of a C<while>
test. Note that outside of a C<while> test, this will not happen.
=back
(Mnemonic: underline is understood in certain operations.)
=item $<I<digit>>
Contains the subpattern from the corresponding set of parentheses in
the last pattern matched, not counting patterns matched in nested
blocks that have been exited already. (Mnemonic: like \digit.)
These variables are all read-only.
=item $MATCH
=item $&
The string matched by the last successful pattern match (not counting
any matches hidden within a BLOCK or eval() enclosed by the current
BLOCK). (Mnemonic: like & in some editors.) This variable is read-only.
=item $PREMATCH
=item $`
The string preceding whatever was matched by the last successful
pattern match (not counting any matches hidden within a BLOCK or eval
enclosed by the current BLOCK). (Mnemonic: ` often precedes a quoted
string.) This variable is read-only.
=item $POSTMATCH
=item $'
The string following whatever was matched by the last successful
pattern match (not counting any matches hidden within a BLOCK or eval()
enclosed by the current BLOCK). (Mnemonic: ' often follows a quoted
string.) Example:
$_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # prints abc:def:ghi
This variable is read-only.
=item $LAST_PAREN_MATCH
=item $+
The last bracket matched by the last search pattern. This is useful if
you don't know which of a set of alternative patterns matched. For
example:
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
(Mnemonic: be positive and forward looking.)
This variable is read-only.
=item $MULTILINE_MATCHING
=item $*
Set to 1 to do multiline matching within a string, 0 to tell Perl
that it can assume that strings contain a single line, for the purpose
of optimizing pattern matches. Pattern matches on strings containing
multiple newlines can produce confusing results when "C<$*>" is 0. Default
is 0. (Mnemonic: * matches multiple things.) Note that this variable
only influences the interpretation of "C<^>" and "C<$>". A literal newline can
be searched for even when C<$* == 0>.
Use of "C<$*>" is deprecated in Perl 5.
=item input_line_number HANDLE EXPR
=item $INPUT_LINE_NUMBER
=item $NR
=item $.
The current input line number of the last filehandle that was read. An
explicit close on the filehandle resets the line number. Since
"C<E<lt>E<gt>>" never does an explicit close, line numbers increase
across ARGV files (but see examples under eof()). Localizing C<$.> has
the effect of also localizing Perl's notion of "the last read
filehandle". (Mnemonic: many programs use "." to mean the current line
number.)
=item input_record_separator HANDLE EXPR
=item $INPUT_RECORD_SEPARATOR
=item $RS
=item $/
The input record separator, newline by default. Works like B<awk>'s RS
variable, including treating blank lines as delimiters if set to the
null string. You may set it to a multicharacter string to match a
multi-character delimiter. Note that setting it to C<"\n\n"> means
something slightly different than setting it to C<"">, if the file
contains consecutive blank lines. Setting it to C<""> will treat two or
more consecutive blank lines as a single blank line. Setting it to
C<"\n\n"> will blindly assume that the next input character belongs to the
next paragraph, even if it's a newline. (Mnemonic: / is used to
delimit line boundaries when quoting poetry.)
undef $/;
$_ = <FH>; # whole file now here
s/\n[ \t]+/ /g;
=item autoflush HANDLE EXPR
=item $OUTPUT_AUTOFLUSH
=item $|
If set to nonzero, forces a flush after every write or print on the
currently selected output channel. Default is 0. Note that STDOUT
will typically be line buffered if output is to the terminal and block
buffered otherwise. Setting this variable is useful primarily when you
are outputting to a pipe, such as when you are running a Perl script
under rsh and want to see the output as it's happening. This has no
effect on input buffering.
(Mnemonic: when you want your pipes to be piping hot.)
=item output_field_separator HANDLE EXPR
=item $OUTPUT_FIELD_SEPARATOR
=item $OFS
=item $,
The output field separator for the print operator. Ordinarily the
print operator simply prints out the comma separated fields you
specify. In order to get behavior more like B<awk>, set this variable
as you would set B<awk>'s OFS variable to specify what is printed
between fields. (Mnemonic: what is printed when there is a , in your
print statement.)
=item output_record_separator HANDLE EXPR
=item $OUTPUT_RECORD_SEPARATOR
=item $ORS
=item $\
The output record separator for the print operator. Ordinarily the
print operator simply prints out the comma separated fields you
specify, with no trailing newline or record separator assumed. In
order to get behavior more like B<awk>, set this variable as you would
set B<awk>'s ORS variable to specify what is printed at the end of the
print. (Mnemonic: you set "C<$\>" instead of adding \n at the end of the
print. Also, it's just like /, but it's what you get "back" from
Perl.)
=item $LIST_SEPARATOR
=item $"
This is like "C<$,>" except that it applies to array values interpolated
into a double-quoted string (or similar interpreted string). Default
is a space. (Mnemonic: obvious, I think.)
=item $SUBSCRIPT_SEPARATOR
=item $SUBSEP
=item $;
The subscript separator for multi-dimensional array emulation. If you
refer to a hash element as
$foo{$a,$b,$c}
it really means
$foo{join($;, $a, $b, $c)}
But don't put
@foo{$a,$b,$c} # a slice--note the @
which means
($foo{$a},$foo{$b},$foo{$c})
Default is "\034", the same as SUBSEP in B<awk>. Note that if your
keys contain binary data there might not be any safe value for "C<$;>".
(Mn