Perl Basic Syntax
Perl borrows features from C, sed, awk, shell scripts, and many other programming languages; its syntax is somewhat similar to these languages, but it also has its own characteristics.
A Perl program consists of declarations and statements. The program executes from top to bottom, including loops and conditional control, and each statement ends with a semicolon ;.
The Perl language has no strict formatting rules; you can indent according to your preferred style.
The first perl program
Interactive programming
You can use the -e option on the command line to enter statements to execute code, as shown in the following example:
$ perl -e 'print "Hello World\n"'
After entering the above command and pressing Enter, the output is:
Hello World
Script programming
We put the following code into hello.pl file:
Example
#!/usr/bin/perl
Output "Hello, World"
"
Hello, world
\n
"
;
in the code /usr/bin/perl is the path to the perl interpreter. Before executing the script, make sure the file has executable permission; we can first change the file permission to 0755:
$ chmod 0755 hello.pl
$ ./hello.pl
Hello, world # 输出结果
print can also use parentheses to output strings; the following two statements produce the same result:
print("Hello, world\n");
print "Hello, world\n";
Script file
perl code can be written in a text file with .pl or .PL as the extension.
A file name can contain numbers, symbols, and letters, but not spaces; you can use an underscore (_) to replace spaces.
A simple Perl file name:
run_oob.pl
Notes
Using comments to make your program easy to read is a good programming habit.
The method for perl comments is to use the character # at the beginning of a statement, for example:
# This line is a comment in perl
perl also supports multi-line comments; the most common method is to use POD (Plain Old Documentations) for multi-line comments. The method is as follows:
Example
#!/usr/bin/perl
This is a single-line comment
"
Hello, world
\n
"
;
=pod
Comment This is a multi-line comment This is a multi-line comment This is a multi-line comment This is a multi-line comment
=cut
Running the above program, the output is:
Hello, world
Note:
Whitespace in Perl
The Perl interpreter does not care how much whitespace there is; the following program also runs normally:
Example
#!/usr/bin/perl
"
Hello, world
\n
"
;
Running the above program, the output is:
Hello, world
However, if spaces and line breaks appear inside a string, they will be output as-is:
Example
#!/usr/bin/perl
Line breaks will be output
"
Hello world
\n
"
;
Running the above program, the output is:
Hello
world
All types of whitespace, such as spaces, tab, blank lines, etc., are ignored by the interpreter if outside quotes, and output as-is if inside quotes.
Single Quotes and Double Quotes
perl can output strings using single quotes and double quotes, as shown below:
Example
#!/usr/bin/perl
"
Hello, world
\n
"
;
Double quote
'
Hello, world\n
'
;
Single quote
The output is as follows:
Hello, world
Hello, world\n
From the result we can see that double quotes \n output a newline, while single quotes do not.
The difference between double quotes and single quotes in Perl: double quotes can normally parse some escape characters and variables, while single quotes cannot parse them and output them as-is.
Example
#!/usr/bin/perl
$a
=
10
;
"
a =
$a
\n
"
;
'
a = $a\n
'
;
The output is as follows:
a = 10
a = $a\n
Here Document
A Here document is also called heredoc, hereis, here-string, or here-script; it is a method of defining a string in command-line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as Perl, PHP, Python, and Ruby).
Usage overview:
- 1. It must be followed by a semicolon, otherwise compilation will fail.
- 2. EOF can be replaced by any other character, as long as the ending marker matches the starting marker.
- 3. The ending marker must occupy a line by itself starting at column one (that is, it must start at the beginning of the line, with no whitespace or characters before or after it).
- 4. The starting marker can be unquoted or enclosed in single or double quotes. Unquoted and double-quoted have the same effect, interpreting embedded variables and escape sequences, while single quotes do not interpret embedded variables and escape sequences.
- 5. When the content needs embedded quotes (single or double), no escape character is needed; it escapes single and double quotes itself, which is equivalent to the use of q and qq.
Example
#!/usr/bin/perl
$a
=
10
;
$var
= <<
"
EOF
"
; This is a
Here
document example using double quotes. You can enter strings and variables here. For example:
a
=
$a
EOF
"
$var
\n
"
;
$var
= <<
'
EOF
'
; This is a
Here
document example using single quotes. For example:
a
=
$a
EOF
"
$var
\n
"
;
The output of the above program is:
这是一个 Here 文档实例,使用双引号。
可以在这输入字符串和变量。
例如:a = 10
这是一个 Here 文档实例,使用单引号。
例如:a = $a
Escape character
If we need to output a special character, we can use a backslash (\) to escape it, for example, to output a dollar sign ($):
Example
#!/usr/bin/perl
$result
=
"
Runoob Tutorial
"
runoob
"
"
;
"
$result
\n
"
;
"
$
result
\n
"
;
The output of the above program is:

Perl Identifiers
Perl identifiers are names used by users when programming; variable names, constant names, function names, statement block names, etc., used in a program are collectively called identifiers.
- Identifier components: English letters (a~z, A~Z), digits (0~9), and underscores (_).
- An identifier starts with an English letter or an underscore.
- Identifiers are case-sensitive; $runoob and $Runoob represent two different variables.