Getting started...LOGIN
Getting started with Perl
author:php.cn  update time:2022-04-14 16:05:12

Perl basic syntax


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 (;).

Perl language does not have strict formatting specifications, you can indent 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 , the example is as follows:

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

Enter the above command and press Enter, the output result is:

Hello World

Script programming

We put the following code into hello. In the pl file:

#!/usr/bin/perl

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

In the code, /usr/bin/perl is the path to the perl interpreter. Before executing the script, make sure that the file has executable permissions. We can first change the file permissions to 0755:

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

print You can also use brackets to output a string. The following two statements output the same result:

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

Script file

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

The file name can contain numbers, symbols and letters, but cannot contain spaces. Underscores (_) can be used to replace spaces.

A simple Perl file name:

run_oob.pl

Comments

Using comments to make your program easier to read is a good programming practice.

perl comment method is to use the character # at the beginning of the statement, such as:

# 这一行是 perl 中的注释

perl also supports multi-line comments. The most common method is to use POD (Plain Old Documentations) to make multiple comments. Line comments. The method is as follows:

#!/usr/bin/perl

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

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

Execute the above program, the output result is:

Hello, world

Note:

  • =pod , =cut can only be used at the beginning of the line.

  • starts with = and ends with =cut.

  • = must be followed by one character, and =cut is not required.


Whitespace in Perl

The Perl interpreter doesn't care how many whitespaces there are, and the following program will work fine:

#!/usr/bin/perl

print       "Hello, world\n";

Execute the above program, the output result is:

Hello, world

But if spaces and lines appear in the string, it will output as it is:

#!/usr/bin/perl

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

Execute 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. If they are within quotation marks, they will be output as they are.


Single quotes and double quotes

perl output string can use single quotes and double quotes, as shown below:

#!/usr/bin/perl

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

The output result is as follows:

Hello, world
Hello, world\n

We can see from the results 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 is.

#!/usr/bin/perl

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

The output results are as follows:

a = 10
a = $a\n

Here document

Here document is also called heredoc, hereis, here-string or here-script, which is a kind of Method for 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 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 (that is, it must start from the beginning of the line, and cannot be connected with any blanks or characters before and after).

  • 4. The start mark can be without quotation marks or with single and double quotation marks. Without quotation marks, the effect is the same as with double quotation marks. Interpret embedded variables and escape symbols, 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. The single and double quotation marks will be escaped by itself. This is equivalent to q and Usage of 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 result of executing the above program is:

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

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

Escape characters

If we need to output a special character, we can use Use backslash (\) to escape, for example, output the dollar sign ($):

#!/usr/bin/perl

$result = "php中文网 \"php\"";
print "$result\n";
print "$result\n";

The output result of executing the above program is:


Perl identifier

Perl identifier is the name used by the user when programming. The variable names, constant names, function names, statement block names, etc. used in the program 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, $php and $php represent two different variables.


#

php.cn