Perl borrows the features of C, sed, awk, shell scripts, and many other programming languages, and the syntax is somewhat similar to these languages, but also has its own characteristics.
The Perl program consists of declarations and statements. The program executes from top to bottom, including loops, conditional controls, each statement with a semicolon (
;
) end.
The Perl language does not have a strict format specification, and you can indent it according to your favorite style. You can use it on the command line Enter the above command and enter, and the output result is as follows: We put the following code into In the code Perl code can be written in a text file to File names can contain numbers, symbols, and letters, but cannot contain spaces, and you can use underscores ( A simple Perl file name: It is a good programming habit to use comments to make your program readable. Perl comments are made by using the character # at the beginning of the statement, such as: Perl also supports multiline comments, and the most common method is to use POD (Plain Old Documentations) for multiline comments. The methods are as follows: Execute the above program, and the output is as follows: Note: To The Perl interpreter doesn’t care about how many blanks there are, and the following programs work: Execute the above program, and the output is as follows: But if spaces and branches appear in the string, it will output as is: Execute the above program, and the output is as follows: All types of blanks such as spaces Perl output strings can use single and double quotes, as follows: The output is as follows: As we can see from the results, double quotation marks The difference between Perl double quotes and single quotes: double quotes can normally parse some escaped characters and variables, while single quotes cannot be parsed and output as is. The output is as follows: Here documents, also known as heredoc, hereis, here- strings, or here- scripts, are a way to define a string in command-line shell (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as Perl, PHP, Python, and Ruby). Overview of usage: 1.It must be followed by a semicolon, otherwise the compilation will not pass. 2.END can be replaced with any other character, as long as the end identifier is consistent with the start identifier. 3.The closing logo must be on a separate line (that is, it must start at the beginning of the line and cannot connect any whitespace and characters before and after). 4.The initial identification can be without quotation marks or single and double quotation marks, and the effect without quotation marks is the same as that with double quotation marks, explaining embedded variables and escape symbols, while single quotation marks do not explain embedded variables and escape symbols. 5.When the content requires embedded quotation marks (single or double quotation marks), there is no need to escape the single and double quotationmarks, which is equivalent to the use of Q and qq. The output result of executing the above program is: If we need to output a special character, we can escape it with a backslash (), such as the output dollar sign ($): The output result of executing the above program is: Perl identifier is the name used by the user when programming. The variable name, constant name, function name and statement block name used in the program are collectively referred to as identifiers. Identifier components: English letters (a-z, A-Z)numbers (0-9), and underscores (_). The identifier begins with an English letter or underscore. Identifiers are case sensitive 5.4.1. The first perl program #
5.4.2. Interactive programming #
-e
option to enter a statement to execute the code, an example is as follows:$ perl -e 'print "Hello World\n"'
Hello World
5.4.3. Script programming #
hello.pl
file:Example #
#!/usr/bin/perl# output "Hello, World"print"Hello, world\\n";
/usr/bin/perl
is the path to the perl interpreter. To make sure that the file has executable permissions before executing the script, we can 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, and the followingtwo statements output the same result:print("Hello, world\n");
print "Hello, world\n";
5.4.4. Script file #
.pl
、
.PL
as a suffix.
_
) to replace spacesrun_oob.pl
5.4.5. Annotation #
# This line is a comment in Perl
Example #
#/ Usr/bin/perl # This is a single line comment print "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
Hello, world
=pod
、
=cut
only at the beginning of the line.
=
start with
=cut
the end.
=
is followed by a character.
=cut
doesn’t have to do it in the back. 5.4.6. Whitespace in Perl #
Example #
#!/usr/bin/perlprint"Hello, world\\n";
Hello, world
Example #
#!/usr/bin/perl# Export branchprint"Hello world\\n";
Hello
world
tab
blank lines, etc., if it is outside the quotation marks, the interpreter will ignore it, and if it is inquotation marks, it will be output as is. 5.4.7. Single and double quotation marks #
Example #
#!/usr/bin/perl
print"Hello, world\\n"; # Double quotation marks
print'Hello, world\n'; # Single quotation mark
Hello, world
Hello, world\n
\\n
output line breaks, but single quotation marks do not.Example #
#!/usr/bin/perl$a=10;print"a =$a\\n";print'a = $a\n';
a = 10
a = $a\n
5.4.8. Here document #
Example #
#/ Usr/bin/perl $a=10$ Var=<"EOF"; This is a Here document instance,
using double quotes.
You can input strings and variables here. For example: a=$aEOFprint
"$var n"$ Var=<'EOF ';
This is a Here document instance, using single quotes.
For example: a=$aEOFprint "$var n";
This is a Here document instance, using double quotes.
You can input strings and variables here.
For example: a=10
This is a Here document instance, using single quotes.
For example: a=$a
5.4.9. Escape character #
Example #
#!/usr/bin/perl$result="Novice Tutorial\\"runoob\\"";print"$result\\n";print"\\$result\\n";

5.4.10. Perl identifier #
$runoob
vs.
$Runoob
represents two different variables.