Perl borrows features from C, sed, awk, shell scripts, and many other programming languages. The syntax is somewhat similar to these languages, but it also has its own characteristics.
Perl programs are composed of declarations and statements. The program is executed from top to bottom, including loops and conditional controls. Each statement ends with a semicolon (;).
The Perl language does not have strict formatting specifications, and you can indent according to your favorite style.
You can use the -e option on the command line to enter statements to execute code. Examples are as follows:
$ perl -e 'print "Hello Worldn"'
Enter the above command and press Enter, the output result is:
Hello World
We put the following code into the hello.pl file:
In the code, /usr/bin/perl is the path of the perl interpreter. Before executing the script, make sure the file has executable permissions. We can first change the file permissions to 0755:
$ chmod 0755 hello.pl $ ./hello.pl Hello, world # Output results
print can also use parentheses to output a string. The following two statements output the same result:
print("Hello, worldn");print "Hello, worldn";
Perl code can be written in a text file, with .pl, .PL as the suffix.
File names can contain numbers, symbols, and letters, but cannot contain spaces. Underscores (_) can be used to replace spaces.
A simple Perl filename:
run_oob.pl
It is good programming practice to use comments to make your program more readable.
The way to comment in perl is to use the character # at the beginning of the statement, such as:
# This line is a comment in perl
Perl also supports multi-line comments. The most common method is to use POD (Plain Old Documentations) to make multi-line comments. The method is as follows:
Executing the above program, the output result is:
Hello, world
Notice:
=pod and =cut can only be used at the beginning of the line.
Start with = and end with =cut.
= must be followed by one character, and =cut does not need to be followed.
The Perl interpreter doesn't care how many whitespaces there are, and the following program will work fine:
Executing the above program, the output result is:
Hello, world
But if spaces and line breaks appear within the string, it will be output as is:
Executing the above program, the output result is:
Hello world
All types of whitespace such as spaces, tabs, blank lines, etc. will be ignored by the interpreter if they are outside quotation marks, and will be output as is if they are within quotation marks.
Perl output strings can use single quotes and double quotes, as shown below:
The output is as follows:
Hello, worldHello, worldn
From the results we can see that double quotes n output newlines, but single quotes do not.
The difference between Perl double quotes and single quotes: Double quotes can parse some escape characters and variables normally, while single quotes cannot be parsed and will be output as they are.
The output is as follows:
a = 10a = $an
A Here document, also known as a heredoc, hereis, here-string or here-script, is a document that is used in command-line shells (such as sh, csh, ksh, bash, PowerShell and zsh) and programming languages (such as Perl, PHP, Python and How to define a string in Ruby).
Usage overview:
1. It must be followed by a semicolon, otherwise the compilation will not pass.
2.END can be replaced by any other character, as long as the end identifier is consistent with the start identifier.
3. The end mark must occupy a line by itself at the top of the line (that is, it must start from the beginning of the line, and cannot be connected with any blanks or characters).
4. The start mark can be without quotation marks or with single or double quotation marks. Without quotation marks, the effect is the same as with double quotation marks. Embedded variables and escape symbols are interpreted. With single quotation marks, embedded variables and escape symbols are not interpreted.
5. When the content requires embedded quotation marks (single quotation marks or double quotation marks), there is no need to add escape characters. Single and double quotation marks are escaped by itself. This is equivalent to the usage of q and qq.
The output result of executing the above program is:
This is an example of a Here document, using double quotes. You can enter strings and variables here. For example: a = 10 This is a Here document instance, using single quotes. For example: a = $a
If we need to output a special character, we can use a backslash () to escape it, for example, output the dollar sign ($):
The output result of executing the above program is:
Perl identifiers are names used by users when programming. Variable names, constant names, function names, statement block names, etc. used in programs are collectively called identifiers.
Identifier components: English letters (a~z, A~Z), numbers (0~9) and underscores (_).
Identifiers start with an English letter or an underscore.
Identifiers are case-sensitive, $codercto and $Codercto represent two different variables.