SoFunction
Updated on 2025-04-14

Detailed introduction to special built-in variables in Perl

Built-in variable $_:

Let’s take a look at an example:

Copy the codeThe code is as follows:

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

The purpose of the example is to define an array and print out the elements. What you need to note here is the foreach loop part. The standard format of the foreach loop should be:
Copy the codeThe code is as follows:
foreach $element (@array){
 ......
}

The array @array assigns the elements in it to $element in turn, but in the above program, I did not do this. In the program, I used a special variable built in perl $_
In the fifth line of the program, foreach (@array) is actually equal to foreach $_ (@array). Here, $_ is the default input/output. Therefore, if there are similar variable positions in the program that are not explicitly declared, it is possible to use $_ instead of

Built-in variable $$:

Copy the codeThe code is as follows:
perl -e "sleep(5);  print qq (The PID of process is : $$)"; 
 
This is a perl program used on the command line, and its function is to print out a paragraph: print qq(The PID of process is: $$), where there is a special variable $$;
$$ is the current process ID of the perl parser (i.e., PID). You can write the segment program on the command line. The program will print out the process ID of the perl parser after the perl parser is run for 5 seconds. You can check the process list during the program run to determine whether the printing result is correct.

Built-in variable $!:


Next is a special variable that returns an error message (or error number).
To save trouble, I won’t write a program anymore, so I’d better use the command line

Copy the codeThe code is as follows:
perl -e "opendir FH,'c: one' or die qq (can't open:$!);";

This sentence is to open a none directory under the C drive (in fact, there is no such directory in my C drive. The reason why I wrote this is to cause error messages in a program). If it is not opened, the subsequent die qq(can't open:$!) will be executed.
Here, $! specifies the content of the error message, and the command line will return the error message:
Copy the codeThe code is as follows:
can't open:No such file or directory at -e line 1.

$! is used more frequently in open or opendir (whether it is opening a file, pipeline or other).

Let's take an example, let's talk about an interesting special built-in variable

Copy the codeThe code is as follows:
$text = "C:\";
{
 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 content under disk C is:
111111111111111111111111111
222222222222222222222222222
333333333333333333333333333
=================================
OK, the program runs:
111111111111111111111111111
#===========================#
111111111111111111111111111
222222222222222222222222222
333333333333333333333333333

The program doesn't talk much nonsense, it is to read the file. The key part is "$line =". The angle bracket operator (i.e. <>) is used to read the content of a line of file (I believe that most tutorials or books are written in this way). However, the line mentioned here actually has a standard, that is, when perl encounters a newline, the edge is considered to be a line, and the newline is defined by the special variable $/, which is the default value.

In other words, every time perl uses the <> operator to read a file, it will first obtain something called a "delimiter" from $/ and use the delimiter as a mark to read the file. If the default $/ is used, this splitter is a newline character, so when default, the <> operator will read one line of text each time.


In the example given, there is a sentence: undef $/, that is, set $/ to an undefined value. In this way, $/ no longer works. Therefore, you can see that the second time you perform the same operation, the <> operator no longer reads only one line but reads all the file contents.

Next, let’s look at a built-in variable that will be affected by $/. Let’s talk about the example first:

Copy the codeThe code is as follows:
$text = "C:\";
open FH,$text or die "can't open:$!";
while () {
 print "line $. is:$_";
}
close FH;

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

It can be seen that the function of the variable $. is a counter-like thing, but why did I say $. at the beginning to be affected by $/variable?

Make some modifications to the above example and take a look:

Copy the codeThe code is as follows:
$text = "C:\";
undef $/; # Note that this line has been added
open FH,$text or die "can't open:$!";
while () {
 print "line $. is:$_";
}
close FH;

There is an extra sentence undef $/; in the second line. The above has already mentioned its function. Let’s take a look at the results of the running of this program:

Copy the codeThe code is as follows:
line 1 is:111111111111111111111111111
222222222222222222222222222
333333333333333333333333333

It was originally a three-line text, not only was it printed out at one time, but it also pointed out "line 1" (it seems that the program believes that this text has only one line). Why?

This is because the $. variable is not a simple line counter. To be precise, it can be said that $. is a pair of $/ counter. Let's look at a program and you will probably understand:

Copy the codeThe code is as follows:
$text = "C:\";
$/ = ";"; # Note here, here I set $/ to undefined, but assign it a semicolon
open FH,$text or die "can't open:$!";
while () {
 print "line $. is:$_ ";
}
close FH;

Let’s take a look at the contents under the C drive:

Copy the codeThe code is as follows:
ddd;bbb;ccc;fff;eee;

It's that simple.
Check out the running results:
Copy the codeThe code is as follows:
line 1 is:ddd;
line 2 is:bbb;
line 3 is:ccc;
line 4 is:fff;
line 5 is:eee;

After reading this program, you should understand.

OK, $. has been almost finished. Let’s talk about its feature, which is still based on the program’s words:

Copy the codeThe code is as follows:
$text = "C:\";
open FH,$text or die "can't open:$!";
while () {
 print "line $. is:$_ ";
}
print " ",$.;
close FH;
print " ",$.;

What you need to pay attention to in this program is the two prints " ",$.; in the last three lines (one is before closing the file, and the other is after closing the file).
Check out the execution results:
Copy the codeThe code is as follows:
line 1 is:111111111111111111111111111
line 2 is:222222222222222222222222222
line 3 is:333333333333333333333333333
3
0

I just knew the first three lines of the result. This is not what we care about. What we should care about is the result of the last two lines (i.e. the result of the two print " ",$.;);
The $. variable is memorable (in principle, it is a package variable). Therefore, when printing before closing the file, you will find that its value has not changed yet (the value at this time and the value of the last read file are both 3);
After closing the file, the variable is re-initialized to 0.
Please pay attention to this feature, because the principle involves some knowledge of perl's scope and packages, and it is not easy to explain clearly, so I won't talk about it. If readers who have an understanding of packages and scopes should be able to understand this part even if I don't talk about them.

Built-in variable: $^O:

Let's talk about the last one

Let’s talk about a simple judging the operating system type, and see an example:

Copy the codeThe code is as follows:
C:>perl -e "print $^O;";
MSWin32

Note that the last character of this special variable $^O here is the letter O and is capitalized.
If it is under Linux, the result will not be MSWin32, but Linux;
This is a variable for judging the environment, simple and practical.