Special variables in Perl

Source: Internet
Author: User
Special variables in Perl

1. $&, $ ', $ ' used in pattern matching

$& is used to hold the value in the match
$ ' used to hold all previous characters in match
' Used to hold all characters after the match

Such as:
If " Hello good There,neigbor hello "=~/S (w+),/)
Print " That actually matched ' $& '. ";
Print $ '. "   ";
Print $ ' ." ";

The results of the execution are:

That actually matched
' There, '.
Hello Good
Neigbor Hello

Other commonly used variable @_
@_ is a private variable of a subroutine; if there is a global variable @_, it will be stored before the subroutine call, and when the subroutine call completes, its early values are returned to the @_. This means that when you pass a parameter to a subroutine, you don't have to worry about it affecting the value of the variable @_ the other subroutine in the program. When nested subroutine calls, the value of the @_ is similar to the above. Even when this subroutine is called recursively, each call gets a new @_, so the subroutine calls it gets its own argument list.

Unless the calling subroutine has a & followed by no parentheses (or no arguments), @_ is obtained from this caller's context. This is usually not a good idea, but sometimes it's

2. Perl-$_ and @_

Perl ' s a great language for special variables-variables that are set up without the programmer has to intervene and P Roviding information ranging from the number of lines read from the Current input file ($.)Through the Current process ID ($$)and the operating System ($^O). Other special variables effect certain operations are performed ( $| controlling output buffering/flushing, for example), or are fundamental in the operation of certain facilities-no more so than and $_.

Lets clear a misconception. $_and @_are different variables. In Perl, you can have a list and a scalar of the same name, and they refer to unrelated pieces of memory.

$_ is known as the ' default input and pattern matching spaces '. In the ' other words ', if you are read in from a file handle at the top of a whileLoop, or run a foreachLoop and don ' t name a loop variable, $_ is set up to you. Then any regular expression matches, ChopS (and LCS and many more) without a parameter, and even Prints assume you want to work on $_. Thus:
while ($line = <FH>) {
if ($line =~/perl/) {
Print FHO $line;
Print UC $line;

Shortens to:
while (<FH>) {
Print FHO;
Print UC;

@_ is the list of incoming parameters to a sub.So if you are write a sub, you refer to the the ' the ' the ' the ' the ' the ' the ' parameter $_[0], the second parameter as $_[1]And so on. and can refer to $_#As the index number of the last parameter:
Sub Demo {
Print "Called with", $ #_ +1, "params/n";
Print "A param was $_[0]/n";

Note that the 中文版Module adds in the "ability to refer to" special variables by other longer, but easier to remember, names such as @ARG F or @_ and $PID for $$.  But use 中文版; Can have a detrimental performance effect if you ' re matching regular the expressions against long incoming strings.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.