注意: 这里不会详述 Perl 的很多细节,只会教你如何开始使用 Perl 语言写一段程序并使它开始工作。
Note: This will not teach you everything about Perl but it should give you a good idea of how the language works, and how to program in Perl.
运行 Perl 程序相对来说比较简单。例如,你写完 Perl 程序后(文件名是 filename.pl ),由 Perl 的解释程序调用执行即可,像下面这样:
Running a Perl program is relatively simple. Once you have a program written out, all you have to do is to invoke the Perl interpreter, along with the filename.
perl filename.pl
如果你正在 unix 系统下运行,你应该用 chmod +rx filename.pl 命令使 filename.pl 具有执行的权限,同时你还应该在你的 perl 程序的首行加入:
If you are running on a unix system, you can make the file executable using the chmod +rx function call. In order to do this, you must have the following line as the first line in your Perl program.
#! /usr/local/bin/perl
“usr/local/bin/perl” 是 Perl 解释程序的所在路径,你可以在命令行状态下键入 which perl :
Where " /usr/local/bin/perl " is the path of your Perl interpreter. In order to find this path on UNIX just type:
which perl
on the command line.
让我们试着写一段小程序,在任意一个文本编辑中敲入下面的代码:
Let's practice writing a small program at our console. Using any editor type the following code:
#! /usr/local/bin/perl
print "Hello World!//n/";
保存为一个扩展名为 .pl 文件,在这里保存为 hello.pl ,在命令行下键入:
Now save your program with a .pl extension. Let's call it hello.pl. From the command line then type:
perl hello.pl
你应该能够看到下面的输出:
You should then see the following output:
Hello World!
Perl 的字面值被表现后就不容改变( 译注:许多动态语言--如 Javascript, Python, PHP 等的共有特点:即数据类型依照具体的表现内容而定 ),字面值被考虑用硬性编码进一步转化程序。 Perl 支持的字面值有下面两种:
Perl literals are values that are represented as-is. The value is considered to be hard-coded into the program. Perl supports literals of the following two types:
• Numbers (数值)
• Strings (字符串)
数值字面值 表现为数值, 字符串字面值 用于名称,文本或消息,通常用一对单引号将其括起来,这将意味着一些变量名(下面将会讲述)出现在一对单引号内部时,变量是不会被替换为相应实际值的。字符串字面值同样支持双引号,一对双引号内部的变量名将由其相应的实际值替换。反向单引号中的字符串字面值同样被支持,这将具有执行系统命令功能(你不用考虑在哪个操作系统运行该命令)。
译注:反向单引号的字符串具有执行系统命令的功能,看下面的 Perl 程序:
print `dir *.txt`;
在当前目录下所有 .txt 后缀名的文件将被列出并显示。
Numeric literals represent numbers. Strings literals are used to identify names, text, or messages that are displayed in the program. String literals are usually enclosed in single quotes, which means that any variable names (more on variables below) appearing in them are not interpolated. Double quoted string literals are also supported, and they support what is called variable interpolation , meaning that variable names are substituted for their real values. Single backquoted string literals are also supported. These normally allow you to run command line options and return the output to your program.
Perl 支持添加注释语句,注释语句用一个 # 字符表示,在 # 字符后直到行结束为止,这其中的语句就是注释语句。
Perl also supports comments in your code. A perl comment is signified by a # sign. When you see a # sign, it means that everything after that is a comment up to the end of the line.
所有程序语言均支持变量。变量与字符串字面值相类似,除此之外它们还有一些特殊值。 Perl 支持三种变量设置:
All programming languages support variables. Variables are like string literals, except that they hold a specific value or values. Perl supports three sets of variables:
• 标量 标量变量仅保存一个值,它可以是一个数值字面值,或一个字符串字面值。标量变量名称始终用一个 $ 字符开始。
• Scalars The scalar variables hold only one value, which may be a numeric literal, or a string literal. Scalar variable names always begin with a $ sign.
• 数组 一个数组变量可以保存多个数值。要保存的值可以是数值或字符串,每个值有其唯一的下标,并且按顺序存放在数组变量中。数组变量名称用一个 @ 字符开始。
• Arrays An array variable can hold a list, or an array of values. The values can be numbers or strings. A key or subscript value is automatically assigned, and the values are kept in the order entered. Array variable names start with an @ sign.
• 关联数组 关联数组与哈希相同,它们均又有些类似于数组,但是又有所不同,程序员通常用关键字和值组成哈希。哈希并不保证被保存数值的有序存放,哈希变量名称始终用 % 符号开始表示。
• Associative Arrays Associative arrays or hashes as they're also known, are similar to arrays. But the major difference is that you, the programmer, assign the key and value to the hash, while an array automatically assigns a key. Hashes do not keep the entered items in the same order as they were entered. Hash variables always start with the % sign.
上面提及标量值经常用于保存一个数值信息,在 Perl 中会经常用到,因此必须要熟悉和使用它。
As mentioned above, scalar values are used to track a single pieces of information. Perl's most commonly used variable is $_ . This special variable name is called the default variable for Perl's many functions, so become very familiar with it.
下面的示例分配一个标量值:
We can see a value being assigned to a scalar value in the following example along with interpolation.
$var="my Scalar Variable";
print "This is $var";
上面的代码最后输出: This is my Scalar Variable
The output of the above code would be: This is my Scalar Variable
数组保存多个变量值并且可以用多种不同的方法进行处理。你可以在数组变量初始化的时候为其赋值:
Arrays hold a list of variables and are handled in a variety of different ways. You can give the values to the array during initialization:
@array=('one','two','three');
上面的数组已分配了三个数值,各个数值间用逗号分隔。数组的第一个下标始终为 0 ,当然你也可以用 $[ 访问并改变它,一般情况下,你最好不要这样做,因为这会使其他人在阅读你的代码时感到糊涂和不解。为了读其中某一个值,你可以用数组名和下标相结合的方式访问,例如,你想访问第二个数值:
The above array assigns three values into the array. The values are separated by commas. The first subscript of an array is always 0, unless you change it with special variable $[ . This is generally not good practice, because you can confuse people reading your code. In order to read the value, you would refer to the array name and the subscript that you want. For example, if we wanted the value 'two' we would say:
print $array[1];
We say $array because we are referring to only one value, followed by an open bracket, the subscript that we want, and then the close bracket. This would return the second value in your array.
哈希有些类似于数组,看下面:
Hashes can be initialized in a similar manner to arrays.
%hash=('key1','value1','key2','value2','key3','value3');
哈希是按照键-值的次序放置。第一个值是第一个键;第二值是与第一个键相关的值;第三值是第二个键;第四个值是与第二个键相关的值。从哈希中得到值的方式与数组类似,除了可以用 []( 方括号 ) 和下标访问数组中的某个值,我们还可以用 {}( 大括号 ) 和键相结合来得到值。例如,如果我们想得到 'value 2' ,我们应该用下面的方式:
The major difference is that you have to place the values in a key-value order. So the first value you type would be the first key; the second, the associated value; the third, the second key; and the fourth, the value associated with that key. The process of retrieving the value from a hash, is similar to an array, except that instead of using [ ] (square brackets) we would use { } (braces), and instead of the subscript number, we would use the key. If we wanted to retrieve 'value2', we would use the following:
print $hash{'key2'};
应该知道的是 Perl 中的变量名称对大小写敏感, $me , $Me , $mE 和 $ME 是四个不同的变量。类似地,如果你有一个标量变量 $me ,一个数组变量 @me ,一个哈希 %me ,它们将按三种不同的变量来区分并加以解释。
Be aware that Perl variables are case-sensitive, so $me, $Me, $mE and $ME are four separate variables. Similarly, if you had a scalar variable called $me, an array called @me, and a hash called %me, they would be interpreted as three separate variables, not the same one.