Special built-in variables in Perl detailed introduction _ Application Tips

Source: Internet
Author: User

Built-in variable $_:

Let's take a look at an example:

Copy Code code as follows:

#!/usr/bin/perl-w
@array = QW (a b c d);
foreach (@array) {
Print $_, "";
}

The role of an example is to define an array and print out the elements, and here you need to be aware of the Foreach loop, which is the standard format for the Foreach Loop:
Copy Code code as follows:
foreach $element (@array) {
......
}

Where the array @array assigns the elements to $element, but in the above program I did not, and in the program I used a special variable built into Perl $_
In the fifth line of the program, foreach (@array) is actually equal to the foreach $_ (@array), where $_ is the default input/output, so if there are similar variable positions in the program that are not explicitly declared, it is possible to use $_ instead

Built-in variable $$:

Copy Code code as follows:
Perl-e "Sleep (5); Print QQ (the PID of Process Perl.exe is: $$) ";

This is a Perl program that is used on the command line to print out a phrase: "Print qq" (The PID of Process Perl.exe is: $$), where there is a special variable $$;
$$ is the process ID of the current Perl parser (that is, PID), you can write the section program on the command line, the program will be running in the Perl parser 5 seconds after printing out the Perl parser process ID, you can see the program during the run by looking at the list of processes to determine whether the printed results are correct.

Built-in variable $!:


Next is a special variable that returns an error message (or an error number).
For the sake of convenience, do not write the program, or to a command line?

Copy Code code as follows:
Perl-e "Opendir FH, ' c:one ' or Die QQ (can ' t open:$!);";

This sentence is to open the C disk under a none directory (and actually my C disk does not have this directory, the reason for this writing is to cause a bug in the program), if not open, will perform the following Die QQ (can ' t open:$!)
Here, $! indicates the contents of the error message, and the command line returns an error message:
Copy Code code as follows:
Can ' t open:no such file or directory AT-E line 1.

$! is used more in open or opendir (whether it's open file or pipe or other).

For example, say an interesting special built-in variable

Copy Code code as follows:
$text = "C:\test.txt";
{
Open FH, $text or die "can ' t open:$!";
my $line =;
Close FH;
Print $line;
}
print "#===========================#";
{
Undef $/;
Open FH, $text or die "can ' t open:$!";
my $line =;
Close FH;
Print $line;
}

=================================
The contents of the Test.txt under the C disk are:
111111111111111111111111111
222222222222222222222222222
333333333333333333333333333
=================================
Well, the result of the program's operation is:
111111111111111111111111111
#===========================#
111111111111111111111111111
222222222222222222222222222
333333333333333333333333333

The procedure is not much nonsense, the function is to read the file, the key part in the "$line =", the angle bracket operator (ie,<>) function is to read a line of file content (believe in most tutorials or books are written in this), but, the line here is actually a standard, That is, when Perl encounters a newline character, the edge is considered a row, and the newline character is by default due to $/this particular variable, which is the default value.

In other words, Perl every time using the <> operator to read the file, it will first get a "delimiter" from the $/, and read the file with the delimiter as a marker, if the default $/is a newline character, so, by default,,<> operator to read one line of text at a time.


In the example given, there is such a sentence: Undef $/, that is, the $/set to an undefined value, so that $/no longer work, so you can see that the second time the same operation, the <> operator is no longer a read-only line but the contents of the file read out.

Let's look at a built-in variable that will be affected by $/or an example:

Copy Code code as follows:
$text = "C:\test.txt";
Open FH, $text or die "can ' t open:$!";
while () {
Print "line $. Is:$_ ";
}
Close FH;

First Look in the loop: print "line $." Is:$_ "
The function of the program is to read the file, and to assign the file to the $_ (because only the <> operator is not specified in the assignment variable, so the default is to assign the value to the $_);
But here in addition to $_, there is also a variable "$."
Let's take a look at the results:
Copy Code code as follows:
Line 1 is:111111111111111111111111111
Line 2 is:222222222222222222222222222
Line 3 is:333333333333333333333333333

Visible, the variable $. function is a similar counter to something, but, why did I say $. will be affected by the $/variable?

Make a slight change to the example above, and then look at the following:

Copy Code code as follows:
$text = "C:\test.txt";
Undef $/; # Pay attention, add this line
Open FH, $text or die "can ' t open:$!";
while () {
Print "line $. Is:$_ ";
}
Close FH;

The second line added a sentence of undef $/; , which has already been said to be useful, look at the results of this program:

Copy Code code as follows:
Line 1 is:111111111111111111111111111
222222222222222222222222222
333333333333333333333333333

The text, which should have been three lines, was not only printed at once, but also indicated "line 1" (it appears that the program thinks the text is only one row).

This is because the $. variable is not a simple line counter, to be exact, it can be said $. is a pair of $/counter, we look at a program will probably understand:

Copy Code code as follows:
$text = "C:\a.txt";
$/ = ";"; # Notice here, here I'm going to set the $/to undefined, but assign it to a semicolon
Open FH, $text or die "can ' t open:$!";
while () {
Print "line $. Is:$_ ";
}
Close FH;

Take a look at the contents of the A.txt under C disk:

Copy Code code as follows:
ddd;bbb;ccc;fff;eee;

It's as simple as that.
Look at the results of the operation:
Copy Code code as follows:
Line 1 is:ddd;
Line 2 is:bbb;
Line 3 IS:CCC;
Line 4 is:fff;
Line 5 is:eee;

See this procedure should understand it.

All right, $. I've talked about it, and I'm going to talk about one of its features, and still be looking at the program to speak:

Copy Code code as follows:
$text = "C:\test.txt";
Open FH, $text or die "can ' t open:$!";
while () {
Print "line $. Is:$_ ";
}
Print "", $.;
Close FH;
Print "", $.;

This program should note that the last three lines of the two print "", $.; (one is before the file is closed, and the other is after the file is closed).
Look at the execution results:
Copy Code code as follows:
Line 1 is:111111111111111111111111111
Line 2 is:222222222222222222222222222
Line 3 is:333333333333333333333333333
3
0

The first three lines of the result are already known, and this is not our concern, we should be concerned about the last two lines of results (that is, two print "", $.; Results);
$. A variable is memory (from the principle, it is a package variable), so printing before closing the file will find that its value has not changed (the value and the last time the file read the value is 3);
After the file is closed, the variable is reinitialized to 0.
This feature can be noticed by everyone, because the principles involved in the scope of Perl and some of the knowledge of the package, it is not easy to make it clear, so it is not said, if the package and scope of the reader to understand this part even if I do not say they should be able to understand.

Built-in variables: $^o:

Say the last one.

To say a simple judgment on the type of operating system, look at an example:

Copy Code code as follows:
C:>perl-e "Print $^o;";
MSWin32

Note that this special variable $^o the last character is the letter O, and uppercase.
If it is under Linux, the result is not MSWin32, but Linux;
This is a variable to judge the environment, simple and practical.

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.