perlvar.pod
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 1,720 行 · 第 1/4 页
POD
1,720 行
=head1 NAMEperlvar - Perl predefined variables=head1 DESCRIPTION=head2 Predefined NamesThe following names have special meaning to Perl. Most punctuation names have reasonable mnemonics, or analogs in theshells. Nevertheless, if you wish to use long variable names,you need only say use English;at the top of your program. This aliases all the short names to the longnames in the current package. Some even have medium names, generallyborrowed from B<awk>. In general, it's best to use the use English '-no_match_vars';invocation if you don't need $PREMATCH, $MATCH, or $POSTMATCH, as it avoidsa certain performance hit with the use of regular expressions. SeeL<English>.Variables that depend on the currently selected filehandle may be set bycalling an appropriate object method on the IO::Handle object, althoughthis is less efficient than using the regular built-in variables. (Summarylines below for this contain the word HANDLE.) First you must say use IO::Handle;after which you may use either method HANDLE EXPRor more safely, HANDLE->method(EXPR)Each method returns the old value of the IO::Handle attribute.The methods each take an optional EXPR, which, if supplied, specifies thenew value for the IO::Handle attribute in question. If not supplied,most methods do nothing to the current value--except forautoflush(), which will assume a 1 for you, just to be different.Because loading in the IO::Handle class is an expensive operation, you shouldlearn how to use the regular built-in variables.A few of these variables are considered "read-only". This means that ifyou try to assign to this variable, either directly or indirectly througha reference, you'll raise a run-time exception.You should be very careful when modifying the default values of mostspecial variables described in this document. In most cases you wantto localize these variables before changing them, since if you don't,the change may affect other modules which rely on the default valuesof the special variables that you have changed. This is one of thecorrect 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 thedefault "line mode", so if the code we have just presented has beenexecuted, the global value of C<$/> is now changed for any other coderunning inside the same Perl interpreter.Usually when a variable is localized you want to make sure that thischange affects the shortest scope possible. So unless you are alreadyinside some short C<{}> block, you should create one yourself. Forexample: 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 5but instead you get: 5 5 5 5 5Why? Because nasty_break() modifies C<$_> without localizing itfirst. The fix is to add local(): local $_ = 5;It's easy to notice the problem in such a short example, but in morecomplicated code you are looking for trouble if you don't localizechanges to the special variables.The following list is ordered by scalar variables first, then thearrays, then the hashes.=over 8=item $ARG=item $_X<$_> X<$ARG>The default input and pattern-searching space. The following pairs areequivalent: while (<>) {...} # equivalent only in while! while (defined($_ = <>)) {...} /^Subject:/ $_ =~ /^Subject:/ tr/a-z/A-Z/ $_ =~ tr/a-z/A-Z/ chomp chomp($_)Here are the places where Perl will assume $_ even if youdon't use it:=over 3=item *Various unary functions, including functions like ord() and int(), as wellas the all file tests (C<-f>, C<-d>) except for C<-t>, which defaults toSTDIN.=item *Various list functions like print() and unlink().=item *The pattern matching operations C<m//>, C<s///>, and C<tr///> when usedwithout an C<=~> operator.=item *The default iterator variable in a C<foreach> loop if no othervariable 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<< <FH> >>operation's result is tested by itself as the sole criterion of a C<while>test. Outside a C<while> test, this will not happen.=backAs C<$_> is a global variable, this may lead in some cases to unwantedside-effects. As of perl 5.9.1, you can now use a lexical version ofC<$_> by declaring it in a file or in a block with C<my>. Moreover,declaring C<our $_> restores the global C<$_> in the current scope.(Mnemonic: underline is understood in certain operations.)=back=over 8=item $a=item $bX<$a> X<$b>Special package variables when using sort(), see L<perlfunc/sort>.Because of this specialness $a and $b don't need to be declared(using use vars, or our()) even when using the C<strict 'vars'> pragma.Don't lexicalize them with C<my $a> or C<my $b> if you want to beable to use them in the sort() comparison block or function.=back=over 8=item $<I<digits>>X<$1> X<$2> X<$3>Contains the subpattern from the corresponding set of capturingparentheses from the last pattern match, not counting patternsmatched in nested blocks that have been exited already. (Mnemonic:like \digits.) These variables are all read-only and dynamicallyscoped to the current BLOCK.=item $MATCH=item $&X<$&> X<$MATCH>The string matched by the last successful pattern match (not countingany matches hidden within a BLOCK or eval() enclosed by the currentBLOCK). (Mnemonic: like & in some editors.) This variable is read-onlyand dynamically scoped to the current BLOCK.The use of this variable anywhere in a program imposes a considerableperformance penalty on all regular expression matches. See L</BUGS>.See L</@-> for a replacement.=item ${^MATCH}X<${^MATCH}>This is similar to C<$&> (C<$POSTMATCH>) except that it does not incur theperformance penalty associated with that variable, and is only guaranteedto return a defined value when the pattern was compiled or executed withthe C</p> modifier.=item $PREMATCH=item $`X<$`> X<$PREMATCH>The string preceding whatever was matched by the last successfulpattern match (not counting any matches hidden within a BLOCK or evalenclosed by the current BLOCK). (Mnemonic: C<`> often precedes a quotedstring.) This variable is read-only.The use of this variable anywhere in a program imposes a considerableperformance penalty on all regular expression matches. See L</BUGS>.See L</@-> for a replacement.=item ${^PREMATCH}X<${^PREMATCH}>This is similar to C<$`> ($PREMATCH) except that it does not incur theperformance penalty associated with that variable, and is only guaranteedto return a defined value when the pattern was compiled or executed withthe C</p> modifier.=item $POSTMATCH=item $'X<$'> X<$POSTMATCH>The string following whatever was matched by the last successfulpattern match (not counting any matches hidden within a BLOCK or eval()enclosed by the current BLOCK). (Mnemonic: C<'> often follows a quotedstring.) Example: local $_ = 'abcdefghi'; /def/; print "$`:$&:$'\n"; # prints abc:def:ghiThis variable is read-only and dynamically scoped to the current BLOCK.The use of this variable anywhere in a program imposes a considerableperformance penalty on all regular expression matches. See L</BUGS>.See L</@-> for a replacement.=item ${^POSTMATCH}X<${^POSTMATCH}>This is similar to C<$'> (C<$POSTMATCH>) except that it does not incur theperformance penalty associated with that variable, and is only guaranteedto return a defined value when the pattern was compiled or executed withthe C</p> modifier.=item $LAST_PAREN_MATCH=item $+X<$+> X<$LAST_PAREN_MATCH>The text matched by the last bracket of the last successful search pattern.This is useful if you don't know which one of a set of alternative patternsmatched. For example: /Version: (.*)|Revision: (.*)/ && ($rev = $+);(Mnemonic: be positive and forward looking.)This variable is read-only and dynamically scoped to the current BLOCK.=item $LAST_SUBMATCH_RESULT=item $^NX<$^N>The text matched by the used group most-recently closed (i.e. the groupwith the rightmost closing parenthesis) of the last successful searchpattern. (Mnemonic: the (possibly) Nested parenthesis that mostrecently closed.)This is primarily used inside C<(?{...})> blocks for examining textrecently matched. For example, to effectively capture text to a variable(in addition to C<$1>, C<$2>, etc.), replace C<(...)> with (?:(...)(?{ $var = $^N }))By setting and then using C<$var> in this way relieves you from having toworry about exactly which numbered set of parentheses they are.This variable is dynamically scoped to the current BLOCK.=item @LAST_MATCH_END=item @+X<@+> X<@LAST_MATCH_END>This array holds the offsets of the ends of the last successfulsubmatches in the currently active dynamic scope. C<$+[0]> isthe offset into the string of the end of the entire match. Thisis the same value as what the C<pos> function returns when calledon the variable that was matched against. The I<n>th elementof this array holds the offset of the I<n>th submatch, soC<$+[1]> is the offset past where $1 ends, C<$+[2]> the offsetpast where $2 ends, and so on. You can use C<$#+> to determinehow many subgroups were in the last successful match. See theexamples given for the C<@-> variable.=item %+X<%+>Similar to C<@+>, the C<%+> hash allows access to the named capturebuffers, should they exist, in the last successful match in thecurrently active dynamic scope.For example, C<$+{foo}> is equivalent to C<$1> after the following match: 'foo' =~ /(?<foo>foo)/;The keys of the C<%+> hash list only the names of buffers that havecaptured (and that are thus associated to defined values).The underlying behaviour of C<%+> is provided by theL<Tie::Hash::NamedCapture> module.B<Note:> C<%-> and C<%+> are tied views into a common internal hashassociated with the last successful regular expression. Therefore mixingiterative access to them via C<each> may have unpredictable results.Likewise, if the last successful match changes, then the results may besurprising.=item HANDLE->input_line_number(EXPR)=item $INPUT_LINE_NUMBER=item $NR=item $.X<$.> X<$NR> X<$INPUT_LINE_NUMBER> X<line number>Current line number for the last filehandle accessed.Each filehandle in Perl counts the number of lines that have been readfrom it. (Depending on the value of C<$/>, Perl's idea of whatconstitutes a line may not match yours.) When a line is read from afilehandle (via readline() or C<< <> >>), or when tell() or seek() iscalled on it, C<$.> becomes an alias to the line counter for thatfilehandle.You can adjust the counter by assigning to C<$.>, but this will notactually move the seek pointer. I<Localizing C<$.> will not localizethe filehandle's line count>. Instead, it will localize perl's notionof which filehandle C<$.> is currently aliased to.C<$.> is reset when the filehandle is closed, but B<not> when an openfilehandle is reopened without an intervening close(). For moredetails, see L<perlop/"IE<sol>O Operators">. Because C<< <> >> never doesan explicit close, line numbers increase across ARGV files (but seeexamples in L<perlfunc/eof>).You can also use C<< HANDLE->input_line_number(EXPR) >> to access theline counter for a given filehandle without having to worry aboutwhich handle you last accessed.(Mnemonic: many programs use "." to mean the current line number.)=item IO::Handle->input_record_separator(EXPR)=item $INPUT_RECORD_SEPARATOR=item $RS=item $/X<$/> X<$RS> X<$INPUT_RECORD_SEPARATOR>The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like B<awk>'s RSvariable, including treating empty lines as a terminator if set tothe null string. (An empty line cannot contain any spacesor tabs.) You may set it to a multi-character string to match amulti-character terminator, or to C<undef> to read through the endof file. Setting it to C<"\n\n"> means something slightlydifferent than setting to C<"">, if the file contains consecutiveempty lines. Setting to C<""> will treat two or more consecutiveempty lines as a single empty line. Setting to C<"\n\n"> willblindly assume that the next input character belongs to the nextparagraph, even if it's a newline. (Mnemonic: / delimitsline boundaries when quoting poetry.) local $/; # enable "slurp" mode local $_ = <FH>; # whole file now here s/\n[ \t]+/ /g;Remember: the value of C<$/> is a string, not a regex. B<awk> has to bebetter for something. :-)Setting C<$/> to a reference to an integer, scalar containing an integer, or
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?