Perl basic syntax

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, and the program executes from top to bottom, including loops, condition controls, and each statement is executed with a ;) End.

The Perl language doesn't have strict formatting specifications, and you can indent it according to your favorite style.


The first perl program

Interactive programming

You can use the -e option on the command line to enter statements to execute code, as follows:

$ perl -e 'print "Hello World\n"'

Enter the above command, after entering the car, the output is:

Hello World

Scripted programming

Let's put the following code hello.pl file:

#!/usr/bin/perl

# 输出 "Hello, World"
print "Hello, world\n";

The /usr/bin/perl in the code is the path to the perl interpreter. To make sure that the file has executable permissions before executing the script, we can modify the file permissions to 0755:

$ chmod 0755 hello.pl 
$ ./hello.pl 
Hello, world                   # 输出结果

Print can also use parentheses to output strings, and the following two statements output the same result:

print("Hello, world\n");
print "Hello, world\n";

The script file

Perl code can be written in a text file with .pl, .PL as suffixes.

The file name can contain numbers, symbols, and letters, but it cannot contain spaces, which can be replaced by underscores.

A simple Perl file name:

w3c_school.pl

Comments

Using annotations to make your program easy to read is a good programming habit.

The perl comment is made by using the character at the beginning of the statement, such as:

# 这一行是 perl 中的注释

Perl also supports multi-line annotations, the most common method of which is to use Plain Old Documentations for multi-line annotations. H ere's how:

#!/usr/bin/perl

# 这是一个单行注释
print "Hello, world\n";

=pdo 注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
=cut

The above procedure is performed and the output is:

Hello, world

Attention:

  • The pod, the cut can only be at the top of the line.
  • It starts with , ends with, and ends with, cut.
  • The following is followed by a character, which can be used after the cut.

White space in Perl

The Perl interpreter doesn't care how many blanks there are, and the following programs work:

#!/usr/bin/perl

print       "Hello, world\n";

The above procedure is performed and the output is:

Hello, world

But if spaces and branches appear in the string, he outputs as is:

#!/usr/bin/perl

# 会输出分行
print "Hello
          world\n";

The above procedure is performed and the output is:

Hello
          world

All types of white spaces such as: spaces, tabs, empty lines, etc. if the interpreter ignores it outside the quotation marks, and output as is in the quotation marks.


Single and double quotes

Perl output strings can be used in single and double quotes, as follows:

#!/usr/bin/perl

print "Hello, world\n";    # 双引号
print 'Hello, world\n';    # 单引号

The output is as follows:

Hello, world
Hello, world\n

As we can see from the results, double quotes are output with line changes, whereless quotes are not.

The difference between Perl double quotes and single quotes: Double quotes resolve some escape characters and variables normally, while single quotes cannot resolve the output as is.

#!/usr/bin/perl

$a = 10;
print "a = $a\n";
print 'a = $a\n';

The output is as follows:

a = 10
a = $a\n

Here document

Here documents, also known as hereoc, hereis, here-strings, or here-scripts, are a way to define a string of words in command-line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and program languages (such as Perl, PHP, Python, and Ruby).

Overview of the use:

  • 1. You must follow the sign, otherwise the compilation will not pass.
  • 2.END can be replaced by any other character, just to ensure that the end identity is consistent with the start identity.
  • 3. The end identification must top a single line (i.e. it must start at the beginning of the line and not bridge any blanks and characters before and after).
  • 4. The start identification can be without quotation marks or single double quotation marks, without quotation marks and double quotes effect consistent, interpretation of embedded variables and escape symbols, with single quotes do not explain embedded variables and escape symbols.
  • 5. When the content needs to be inline quotation marks (single or double quotes), there is no need to escape, itself to a single double quote escape, here is quite similar to the use of q and qq.
#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
这是一个 Here 文档实例,使用双引号。
可以在这输如字符串和变量。
例如:a = $a
EOF
print "$var\n";

$var = <<'EOF';
这是一个 Here 文档实例,使用单引号。
例如:a = $a
EOF
print "$var\n";

The output of the above program is:

这是一个 Here 文档实例,使用双引号。
可以在这输如字符串和变量。
例如:a = 10

这是一个 Here 文档实例,使用单引号。
例如:a = $a

Escape characters

If we need to output a special character, we can use an anti-slash to escape, such as the output dollar sign ($):

#!/usr/bin/perl

$result = "W3Cschool教程 \"w3cschool\"";
print "$result\n";
print "\$result\n";

The output of the above program is:

Perl basic syntax


Perl identifier

Perl identifiers are the names used by users when programming, and variable names, constant names, function names, statement block names, and so on are collectively referred to as identifiers.

  • Identifier components: Letters (a-z, A-Z), numbers (0-9) and underscores .
  • The identifier begins with an English letter or underscore.
  • The identifier is case sensitive, $w 3cschool and $W 3cschool represent two different variables.