⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlvar.pod

📁 ARM上的如果你对底层感兴趣
💻 POD
📖 第 1 页 / 共 3 页
字号:
=head1 NAME

perlvar - Perl predefined variables

=head1 DESCRIPTION

=head2 Predefined Names

The following names have special meaning to Perl.  Most 
punctuation names have reasonable mnemonics, or analogues in one of
the shells.  Nevertheless, if you wish to use 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 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 (and preferably) 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 more safely,

    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.

The following list is ordered by scalar variables first, then the
arrays, then the hashes (except $^M was added in the wrong place).
This is somewhat obscured by the fact that %ENV and %SIG are listed as
$ENV{expr} and $SIG{expr}.


=over 8

=item $ARG

=item $_

The default input and pattern-searching space.  The following pairs are
equivalent:

    while (<>) {...}	# equivalent in only while!
    while (defined($_ = <>)) {...}

    /^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.)

=back

=over 8

=item $E<lt>I<digits>E<gt>

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 \digits.)
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: C<`> 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: C<'> 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 multi-line 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
influences the interpretation of only "C<^>" and "C<$>".  A literal newline can
be searched for even when C<$* == 0>.

Use of "C<$*>" is deprecated in modern Perls, supplanted by 
the C</s> and C</m> modifiers on pattern matching.

=item input_line_number HANDLE EXPR

=item $INPUT_LINE_NUMBER

=item $NR

=item $.

The current input line number for the last file handle from
which you read (or performed a C<seek> or C<tell> on).  An
explicit close on a filehandle resets the line number.  Because
"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 empty lines as delimiters if set to the
null string.  (Note: An empty line cannot contain any spaces or tabs.)
You may set it to a multi-character string to match a multi-character
delimiter, or to C<undef> to read to end of file.  Note that setting it
to C<"\n\n"> means something slightly different than setting it to
C<"">, if the file contains consecutive empty lines.  Setting it to
C<""> will treat two or more consecutive empty lines as a single empty
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;

Remember: the value of $/ is a string, not a regexp.  AWK has to be
better for something :-)

Setting $/ to a reference to an integer, scalar containing an integer, or
scalar that's convertable 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);
    $_ = <FILE>;

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
record-oriented files), then you'll likely get a full chunk of data with
every read. If a record is larger than the record size you've set, you'll
get the record back in pieces.

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
likely not a problem, as any file you'd want to read in record mode is
proably usable in line mode) Non-VMS systems perform normal I/O, so
it's safe to mix record and non-record reads of a file.

=item autoflush HANDLE EXPR

=item $OUTPUT_AUTOFLUSH

=item $|

If set to nonzero, forces a flush right away and after every write or print on the
currently selected output channel.  Default is 0 (regardless of whether
the channel is actually buffered by the system or not; C<$|> tells you
only whether you've asked Perl explicitly to flush after each write).
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.  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.
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 C<$/>, 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 multidimensional array emulation.  If you
refer to a hash element as

    $foo{$a,$b,$c}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -