Perl 简明教程

Perl - Syntax Overview

Perl 从许多语言借用了语法和概念:awk、sed、C、Bourne Shell、Smalltalk、Lisp 甚至英语。但是,语言之间存在一些明确的差异。本章旨在让您快速了解 Perl 中预期的语法。

Perl borrows syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp and even English. However, there are some definite differences between the languages. This chapter is designd to quickly get you up to speed on the syntax that is expected in Perl.

Perl 程序包括一系列声明和语句,从上到下运行。循环、子例程和其他控制结构允许您在代码中跳转。每个简单语句都必须以分号 (;) 结尾。

A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code. Every simple statement must end with a semicolon (;).

Perl 是一种自由格式语言:您可以根据自己的喜好对其进行格式化和缩进。空白主要用于分隔标记,这与 Python 等语言不同,后者语法的重要组成部分,或者 Fortran,其中它不重要。

Perl is a free-form language: you can format and indent it however you like. Whitespace serves mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial.

First Perl Program

Interactive Mode Programming

您可以在命令行中使用 -e 选项使用 Perl 解释器,它允许您从命令行执行 Perl 语句。让我们在 $ 提示符下尝试如下操作 −

You can use Perl interpreter with -e option at command line, which lets you execute Perl statements from the command line. Let’s try something at $ prompt as follows −

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

此执行将产生以下结果 −

This execution will produce the following result −

Hello, world

Script Mode Programming

假设您已在 $ 提示符下,让我们使用 vi 或 vim 编辑器打开一个文本文件 hello.pl,并在文件中添加以下行。

Assuming you are already on $ prompt, let’s open a text file hello.pl using vi or vim editor and put the following lines inside your file.

#!/usr/bin/perl

# This will print "Hello, World"
print "Hello, world\n";

在此 /usr/bin/perl 是实际的 Perl 解释器二进制文件。在执行脚本之前,请确保更改脚本文件模式并给予执行权限,通常设置为 0755,最后执行上述脚本,如下所示 −

Here /usr/bin/perl is actual the perl interpreter binary. Before you execute your script, be sure to change the mode of the script file and give execution priviledge, generally a setting of 0755 works perfectly and finally you execute the above script as follows −

$chmod 0755 hello.pl
$./hello.pl

此执行将产生以下结果 −

This execution will produce the following result −

Hello, world

您可以根据个人喜好对函数参数使用圆括号,也可以省略圆括号。只有在需要澄清优先级问题时才偶尔需要它们。以下两个陈述产生相同的结果。

You can use parentheses for functions arguments or omit them according to your personal taste. They are only required occasionally to clarify the issues of precedence. Following two statements produce the same result.

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

Perl File Extension

Perl 脚本可以在任何普通纯文本编辑器程序中创建。每种类型的平台都有多种程序可用。网上有许多专为程序员设计的程序可供下载。

A Perl script can be created inside of any normal simple-text editor program. There are several programs available for every type of platform. There are many programs designd for programmers available for download on the web.

根据 Perl 约定,Perl 文件必须使用 .pl 或 .PL 文件扩展名保存,才能被识别为功能性 Perl 脚本。文件名可以包含数字、符号和字母,但不能包含空格。在空格处使用下划线 (_)。

As a Perl convention, a Perl file must be saved with a .pl or .PL file extension in order to be recognized as a functioning Perl script. File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces.

Comments in Perl

任何编程语言中的注释都是开发人员的朋友。注释可用于使程序对用户友好,并且解释器会简单地跳过这些注释,而不会影响代码功能。例如,在上面的程序中,以散列符号 # 开头的行是注释。

Comments in any programming language are friends of developers. Comments can be used to make program user friendly and they are simply skipped by the interpreter without impacting the code functionality. For example, in the above program, a line starting with hash # is a comment.

简单来说,Perl 中的注释以散列符号开头并一直运行到行的末尾 −

Simply saying comments in Perl start with a hash symbol and run to the end of the line −

# This is a comment in perl

以 = 开头的行解释为嵌入式文档(pod)部分的开头,并且编译器会忽略此后所有行,直到遇到下一个 =cut。以下示例 −

Lines starting with = are interpreted as the start of a section of embedded documentation (pod), and all subsequent lines until the next =cut are ignored by the compiler. Following is the example −

#!/usr/bin/perl

# This is a single line comment
print "Hello, world\n";

=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the
compiler until the next =cut is encountered.
=cut

这会产生以下结果 −

This will produce the following result −

Hello, world

Whitespaces in Perl

Perl 程序不关心空格。以下程序可以很好地运行:

A Perl program does not care about whitespaces. Following program works perfectly fine −

#!/usr/bin/perl

print       "Hello, world\n";

但如果空格在引号字符串内,它们将按原样打印。例如:

But if spaces are inside the quoted strings, then they would be printed as is. For example −

#!/usr/bin/perl

# This would print with a line break in the middle
print "Hello
          world\n";

这会产生以下结果 −

This will produce the following result −

Hello
          world

在引号之外使用时,所有类型的空格(如空格、制表符、换行符等)对解释器来说都是等效的。包含只包含空格的行(可能带有注释)被称为空白行,Perl 完全忽略它。

All types of whitespace like spaces, tabs, newlines, etc. are equivalent for the interpreter when they are used outside of the quotes. A line containing only whitespace, possibly with a comment, is known as a blank line, and Perl totally ignores it.

Single and Double Quotes in Perl

你可以按如下方式对文字字符串使用双引号或单引号:

You can use double quotes or single quotes around literal strings as follows −

#!/usr/bin/perl

print "Hello, world\n";
print 'Hello, world\n';

这会产生以下结果 −

This will produce the following result −

Hello, world
Hello, world\n$

在单引号和双引号中有一个重要的区别。只有双引号中的@ {s0}变量和换行符 \n 等特殊字符,而单引号不插入任何变量或特殊字符。在下面的示例中,我们将 $a 用作变量来存储一个值,然后打印该值:

There is an important difference in single and double quotes. Only double quotes interpolate variables and special characters such as newlines \n, whereas single quote does not interpolate any variable or special character. Check below example where we are using $a as a variable to store a value and later printing that value −

#!/usr/bin/perl

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

这会产生以下结果 −

This will produce the following result −

Value of a = 10
Value of a = $a\n$

"Here" Documents

你可以非常方便地存储或打印多行文本。你甚至可以在“此处”文档中使用变量。下面是一个简单的语法,请仔细检查 << 和标识符之间不能有空格。

You can store or print multiline text with a great comfort. Even you can make use of variables inside the "here" document. Below is a simple syntax, check carefully there must be no space between the << and the identifier.

标识符可以是裸词或我们下面用于 EOF 的一些引用文本。如果标识符被引用,你使用的引用类型决定了此处文档中文本的处理方式,就像在常规引用中一样。未引用的标识符的工作方式就像双引号。

An identifier may be either a bare word or some quoted text like we used EOF below. If identifier is quoted, the type of quote you use determines the treatment of the text inside the here docoment, just as in regular quoting. An unquoted identifier works like double quotes.

#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";

$var = <<'EOF';
This is case of single quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";

这会产生以下结果 −

This will produce the following result −

This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = 10

This is case of single quote so variable value will be
interpolated. For example value of a = $a

Escaping Characters

Perl 使用反斜杠 (\) 字符转义任何可能干扰我们代码的字符类型。让我们举一个例子,我们希望打印双引号和 $ 符号:

Perl uses the backslash (\) character to escape any type of character that might interfere with our code. Let’s take one example where we want to print double quote and $ sign −

#!/usr/bin/perl

$result = "This is \"number\"";
print "$result\n";
print "\$result\n";

这会产生以下结果 −

This will produce the following result −

This is "number"
$result

Perl Identifiers

Perl 标识符是一个用于标识变量、函数、类、模块或其他对象的名字。Perl 变量名称以 $、@ 或 % 开头,后跟零个或更多个字母、下划线和数字 (0 到 9)。

A Perl identifier is a name used to identify a variable, function, class, module, or other object. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9).

Perl 不允许标识符中出现标点符号,例如 @、$ 和 %。Perl 是一种@ {s1}编程语言。因此@ {s2}和@ {s3}是 Perl 中两个不同的标识符。

Perl does not allow punctuation characters such as @, $, and % within identifiers. Perl is a case sensitive programming language. Thus $Manpower and $manpower are two different identifiers in Perl.