65.9K
CodeProject 正在变化。 阅读更多。
Home

Perl 和 Perl 编程基础

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4投票s)

2010年7月6日

CPOL

16分钟阅读

viewsIcon

61955

Perl 基础编程 - 它是如何工作的

摘要

学习编程可能很困难,而且也许最好通过阅读编写良好的程序来学习。Perl 是一种强大的编程语言,在其社区中以开发安全工具而闻名。编程语言通常是编译的,但 Perl(Practical Extraction and Report Language,实用提取和报告语言)是通过 Perl 解释器进行解释的。Perl 具有许多独特的特性:任何接触过编程语言的人都处理过变量。变量是一种数据类型,通常在初始化(或赋值)之前声明,但在 Perl 中不必声明。Perl 是一种面向对象的、高级编程语言,这意味着它的类是抽象的。对象成员,其名称反映了命名类的类型,是具体定义的。一个类被实例化,这意味着使用库定义的类来实例化其使用。在 Perl 中,这些称为模块。对象成员构成了类,并定义了这些成员处理的方法和数据成员。Perl 的独特之处在于,我们不必声明我们使用的数据的确切类型(整数、字符、字符字符串、算术或逻辑度量单位),我们只需将数据定义为标量、数组或关联数组(哈希)。Perl 拥有许多内置函数和机制来增强其功能,要使用其中任何一个(例如内置加密),您必须首先理解这些数据类型的工作原理。因此,我们将通过示例来理解,看看我们能获得多大的理解。在 technet.microsoft.com 的脚本存储库中有大量的可下载 Perl 脚本。

那么我如何开始呢?

Perl 解释器可以从 www.perl.orghttp://www.activestate.com/ 等处下载。安装后,您可以使用此命令设置其路径的环境变量(我们假设您使用的是 Win32 或 Microsoft Windows 命令提示符)

C:\Scripts\> set PATH=%PATH%;.;C:\Perl\bin 

当我们设置了路径后,用于解释 Perl 脚本的“perl”命令就可以在命令行中的任何位置使用了。但在开始之前,我们应该定义一些术语。Perl 可以使用 7 位 ASCII 编写。一个键盘字符被认为是 8 位,或一个字节。然而,当进行数据压缩时,打印字符实际上是 7 位。但 Perl 也可以使用 Unicode 编写,其中字符是两个字节。但这究竟意味着什么呢?嗯,Java 语言的开发是为了使用两字节字符,当时在全球计算行业中,全球翻译是必不可少的。不同的字符,特别是阿拉伯和远东国家的字符,无法简单地翻译成一个字节字符。将字符转换为 Unicode 或 UTF-8(两字节编码方案的任何变体)使得这些翻译成为可能,在大多数情况下,转换是自动的。

Perl 具有 `perldoc`、`pod2html` 等命令,用于读取生成的站点信息和 pod 文档。例如

C:\> perldoc  Perl.pm           
will open the document with the .pm file extension.
Pod2html will take a .pod document and convert into an HTML web page. 

Perl 脚本演练

这是一个 Perl 脚本的示例。它看起来会很奇怪,因为我们将要学习的技术尚未定义,但它们将变得更加清晰

print "What is your name? ";
chomp($name = ); # Program waits for user input from keyboard
print "Hello, $name, do you want to learn Perl right now? ";
chomp($response = );
$response=lc($response); # response is converted to lowercase
if($response eq "yes" or $response eq "y"){
print "Good Decision! We'll start with this example.\n";
}
else{
print "O.K. Try again later.\n";
}
$now = localtime; # Use a Perl function to get the date and time
print "$name, you ran this script on $now.\n";

我们将此脚本命名为 *Script1.pl*。首先,请注意 *.pl* 文件扩展名。现在我们在命令行中运行脚本。我们首先注意到脚本正在请求用户输入(使脚本“交互式”)

c:\Scripts>perl Script1.pl
What is your name? Joe
Hello, Joe, do you want to learn Perl right now? yes
Good Decision! We'll start with this example.
Joe, you ran this script on Mon Jul  5 19:44:09 2010.

Perl 语句始终以分号结尾。哈希标记 `#` 表示作者的注释,解释器会忽略它。我们使用“print”函数,但请注意没有换行转义序列。计算机命令行的屏幕上有一个闪烁的光标,指示下一条语句将出现在何处。如果光标已到达屏幕边缘,或者用户按下了 Enter(返回)键,则光标会向下移动一行并移到最左边。这称为回车符-换行符。然而,在 C 等编程语言中,会使用像“\n”这样的转义序列

“Hello, what is your name?\n” # 这会在屏幕上打印“Hello, what is your name?”。我们不必在此处使用此转义序列,因为在 Perl 中,您通常可以使用 `chop()` 或 `chomp()` 等函数来处理字符串。

注意

大多数计算机编程语言都包含三个文件句柄。它们被称为标准输入、标准输出和标准错误。文件句柄仅仅是对文件的引用,以便打开该文件。标准输入(STDIN)表示任何用户输入机制:键盘、鼠标等。标准输出表示计算机的信息如何输出给用户:控制台屏幕、打印机、发送到数据库等。标准错误不属于本主题的范围。

现在考虑这个简短的片段

C:\> print “Enter your name:”  ;
        $name = <> ;
        print “Enter your age: “  ;
        $age = <>;
        print “$name”;
        print “$age”;

输出将是:

Enter your name:  Joe
Enter your age: 44
Joe
44

现在是时候介绍标量了,它是 Perl 中使用的三种数据类型之一。

标量

片段 `print “Hello, World\n”` 将在屏幕或标准输出上打印 `Hello, World`。`print` 这个词调用 `print` 函数,该函数将引号内的文本显示在屏幕上。正如我们所见,`\n` 符号化了新行或回车符。现在我们介绍一个标量

$myscalar = ‘Hello, World\n’
print  “$myscalar” ;  # prints the value of $myscalar

标量通过使用 `$` 符号后跟变量名来声明。第一行将引号内的文本馈送到名为 `$myscalar` 的标量中。那么,我为什么在第一行使用单引号,在第二行使用双引号呢?因为 Perl 在双引号内执行变量插值,这意味着它会用变量的值替换变量名。在接下来的示例中,这一点将变得清楚

$myscalar = ‘Hello\n’ ; 
# variable $myscalar has the value Hello\n
print  ‘$myscalar’  ;  
#because I used single quotes there is no variable interpolation. 

输出将是:

$myscalar

以下是变量插值的示例

$myscalar = ‘Hello’ ;
print  “$myscalar”

输出将是:

Hello

这就是单引号和双引号的区别。通过使用菱形运算符 `<>` 来获取用户输入。它基本上从用户(标准输入)获取输入,以使程序更具交互性。考虑以下示例

print ‘Enter your name:’ ;
$username = <> ;  # the user will enter a text which will be fed into the scalar
print “Hi, $username” ;
The output will be:
Enter your name:  Dave
Hi Dave

程序将在屏幕上打印文本“Enter your name:”,并等待用户输入。用户输入的文本将被馈送到标量 `$username`。然后程序将打印 `Hi`,后跟用户输入的文本。现在考虑以下代码

print  “Enter your name:” ;
$name = <>;
Print “Enter your age:” ;
$age = <>;
print “$name”  ;
print “$age”;
The output will be:
Enter your name:  Joe
Enter your age: 44
Joe
44

但 Perl 为什么会在单独的行上打印 `Joe` 和 `44` 呢?程序中(`\n`)没有换行符。发生的情况是,当向用户提供输入提示时,他需要输入一些内容。Perl 会继续接受用户输入,直到光标向下移动并移到最左边(用户提供结束运算符 - 回车符或 Enter)。当用户提供结束运算符时,Perl 会停止接受用户输入,并将用户输入的已输入数据分配给指定的变量,包括回车符。这意味着标量 `$name` 的值为 `Joe`,后跟回车符或 `\n`。当我们打印标量 `$name` 时,将打印 `$name` 的值,后跟一个回车符或 `\n`。为了避免这个问题,我们使用 `chop()` 或 `chomp()`。这两个函数之间的区别将在以下示例中变得更加清楚

$variable = “Dave”;
chop($variable);
print $variable
The output will be:
Dav

在此示例中:

$variable = “Dave”
chomp($variable);
print $variable ;
The output will be:
Dave 

`chop()` 和 `chomp()` 之间的区别在于,`chop()` 会删除字符串的最后一个字符,无论它是什么;而 `chomp()` 仅当最后一个字符是换行符(`\n`)时才将其删除。因此,如果我们把这些事实放在一起,我们就可以编写一个稍微复杂一些的 Perl 脚本

 print "Hello there, and what is your name? ";
 $name = ;
 print "$name is a very high class name.\n";
 chop($name); # Removes the last character no matter what it is.
 print "$name is a very high class name.\n\n";
 chop($name);
 print "$name has been chopped a little too much.\n";
 print "What is your age? ";
 chomp($age=); # Removes the last character if
# it is the newline.
 chomp($age); # The last character is not removed
# unless a newline.
 print "For $age, you look so young!\n"; 

到目前为止,我们可以看到 Perl 是一种自由格式的语言,这意味着您可以在行的任何位置放置语句,甚至可以跨行。空白符指的是制表符、空格和换行符。因此,我们知道换行符表示为“\n”,并且必须用引号括起来。空白符用于分隔单词。符号和单词之间允许有任意数量的空格。包含在单引号或双引号中的空白符会被保留;否则,它将被忽略。以下表达式是相同的

5+4*2 与 `5 + 4 * 2;` 相同

并且以下两个语句都是正确的,尽管输出将显示空白符在被引用时会保留

print "This is a Perl statement.";
print "This
is
also
a Perl
statement.";

The output will be:

This is a Perl statement. This
is
also
a Perl
statement.

请记住,与 UNIX shell 不同,Win32 DOS 提示符 shell 有自己的命令行解析方式。由于您的大部分编程将在脚本文件中完成,因此您很少需要担心 shell 的交互,但当脚本与操作系统交互时,如果您不了解您拥有的命令以及它如何代表您执行这些命令,就会出现问题。例如,假设我们了解如何使用 Perl 模块,即可下载模块和随附的 Perl 解释器下载的标准模块。考虑下一个脚本

 use File::Find;
 use Win32::File;
# Works on both FAT and NTFS file systems.
 &File::Find::find(\&wanted,"C:\\program files", "C:\\windows");
 sub wanted{
 (Win32::File::GetAttributes($_,$attr)) &&
($attr & DIRECTORY) &&
print "$File::Find::name\n";
}

那么我们如何使用 Win32 命令行呢?这个脚本将与文件系统交互,并以分层形式输出目录的内容。我们只需使用记事本或键入 `C:\> type con > ThisScript.pl`。

这里没有光标,只有一个空白屏幕,所以将脚本复制并粘贴到屏幕上,按 Control-Z,您就会回到命令提示符。输出将是

C:\program files
C:\program files/Adobe
C:\program files/Adobe/Acrobat_com
C:\program files/Adobe/Acrobat_com/locale
C:\program files/Adobe/Acrobat_com/locale/de_DE
C:\program files/Adobe/Acrobat_com/locale/de_DE/landingPages
C:\program files/Adobe/Acrobat_com/locale/de_DE/landingPages/images
C:\program files/Adobe/Acrobat_com/locale/en_GB
C:\program files/Adobe/Acrobat_com/locale/en_US
C:\program files/Adobe/Acrobat_com/META-INF
   and  so on ….
C:\windows/winsxs/x86_wwf-system.workflow.componentmodel_31bf3856ad364e35_6.0.6002.18005_none_8dcf92850b377416
C:\windows/winsxs/x86_wwf-system.workflow.runtime_31bf3856ad364e35_6.0.6002.18005_none_670656a59d28c52a
C:\windows/winsxs/x86_xcbda.inf.resources_31bf3856ad364e35_6.0.6001.18000_en-us_f5090c76a778b031
C:\windows/winsxs/x86_xnacc.inf.resources_31bf3856ad364e35_6.0.6001.18000_en-us_8f20a3842ebca522C:\windows\..etc

这里发生了什么?`File::Find` 模块从标准的 Perl 库加载。`find()` 的第一个参数是对名为 `wanted` 的子例程的引用,后跟要查找的两个目录。`wanted` 函数将检查每个名称是否为目录,并列出找到的所有子目录的完整路径名。`$_` 被赋值为搜索中当前目录的名称。

结束说明:条件语句

与 C 语言一样,Perl 会在某个条件为 `true` 时执行一个操作。如果某个条件为真,则执行此操作。如果不是,则执行另一个操作。此外,请注意用于数字的运算符与用于字符串的运算符的工作方式不同。考虑以下示例

print "Enter your Name:";
$name = <>;
chomp $name ;
if ( $name eq 'Harry') {
print "Hi Harry";
}
else {
print "You do not have permission to use this computer";
}
Enter your Name: John
You  do not have permission to use this computer

while 循环不仅可以用于重复代码片段,还可以让我们验证用户输入并根据验证结果执行预定义的任务。通过这种方式,while 循环会重复一段代码,只要条件为 `true`。第一个示例将展示其基本用法,第二个示例将演示无限循环(由于条件为 true 而持续执行)

while ($count < 10) {
$count += 2;
print "count is now $count\n"; # Gives values 2 4 6 8 10
}

输出是:

count is now 2
count is now 4
count is now 6
count is now 8
count is now 10

下一个示例用于验证用户输入,但会创建无限循环。只需按 Control-C 即可退出循环

print 'Username:' ;
$user = <>;
chomp $user;
while ( $user eq "admin" ) {
print "System info goes here:";
}

输出是:

ere:System info goes here:System info goes here:System info goes here:System inf
o goes here:System info goes here:System info goes here:System info goes here:Sy
stem info goes here:System info goes here:System info goes here:System info goes
 here:System info goes here:System info goes here:System info goes here:System i
nfo goes here:System info goes here:System info goes here:System info goes here:
System info goes here:System info goes here:System info goes here:System info go
es here:System info goes here:System info goes here:System info goes here:System
 info goes here:System info goes here:System info goes here:System info goes her
e:System info goes here:System info goes here:System info goes here:System info
goes here:System info goes here:System info goes here:System info goes here:Syst
em info goes here:System info goes here:System info goes here:System info goes h
ere:System info goes here:System info goes here:System info goes here:System inf
o goes here:System info goes here:System info goes here:System info goes here:Sy
stem info goes here:System info goes here:System info goes here:System info goes
here:System info goes here:System info goes here:System info goes here:System i
nfo goes here:System info goes here:System info goes here:System info goes here:

数组

下一个程序介绍了数组。`opendir` 函数打开目录结构并将其分配给目录文件句柄 `DIR`。打开 ..(父)目录以供读取。`readdir` 函数将目录中的所有其余条目分配给数组 `@parentfiles`。`closedir` 函数关闭目录。文件按其在目录结构中的存储顺序打印(或写入系统,写入屏幕)。这可能与使用 DOS 中的 `dir` 命令不同

 opendir(DIR, "C:\\Windows") || die "Can't open: $!\n";
 # Open parent directory
 @parentfiles=readdir(DIR);
 # Gets a list of the directory contents
 closedir(DIR); # Closes the filehandle
 foreach $file ( @parentfiles )
 # Prints each element of the array
 { 
 print "$file\n"; }

输出如预期:

.
..
AppPatch
assembly
bfsvc.exe
Boot
bootstat.dat
Branding
Cursors
Debug
DigitalLocker
Downloaded Program Files
DPINST.LOG
DtcInstall.log
en-US
explorer.exe
Fonts
fveupdate.exe
Globalization
Help
HelpPane.exe
hh.exe
ie8_main.log
iis7.log
IME
inf
Installer
L2Schemas
LiveKernelReports
Logs
Media
mib.bin
Microsoft.NET
and  so on ..
SHELLNEW
SoftwareDistribution
Speech
SysMsiCache
system
system.ini
System32
tapi
Tasks
Temp
tracing
TSSysprep.log
Users
etc . . .

我们为什么要使用 `@` 符号?因为我们使用了第二种数据类型。如果标量是单数,那么列表和数组就是复数。列表是标量的有序集合。数组是包含列表的变量。在 Perl 中,有时这两个术语可以互换使用。然而,列表是数据,数组是变量。您可以拥有一个不是数组的列表值,但每个数组都包含一个列表。请注意下图。数组或列表的每个元素都是一个单独的标量变量,具有独立的标量值。这些值是有序的,也就是说,它们从第一个元素到最后一个元素都有一个特定的序列。数组或列表的元素由从零开始并按一递增的整数索引,因此任何数组或列表的第一个元素始终是元素零。

当您有一系列相似的数据元素时,使用数组比为每个元素创建单独的变量更容易。数组名允许您将单个变量名与一系列数据元素关联起来。列表中的每个元素都通过其名称和下标(也称为索引)来引用。Perl 与 C 类语言和托管代码不同,它不检查数组的元素是否属于同一数据类型。它们可以是数字和字符串的混合。对于 Perl 来说,数组是一个命名的列表,包含有序的标量集合。数组名以 `@` 符号开头。下标跟在数组名后面,并用方括号 `[]` 括起来。下标是简单的整数,从零开始。因此,让我们 examination 这个脚本

@grades = (90,89,78,100,87);
print "The original array is: @grades\n";
print "The number of the last index is $#grades\n";
print "The array is truncated to 4 elements: @grades\n";
@grades=();
print "The array is completely truncated: @grades\n";

输出将是:

The original array is: 90 89 78 100 87
The number of the last index is 4
The array is truncated to 4 elements: 90 89 78 100 87
The array is completely truncated:

数组 `@grades` 被赋值为一个包含五个数字的列表。我们获取数组最后一个元素的下标(索引)值。数组中的最后一个下标已被缩短为 `3`(请记住,数组通常用括号 `[]` 表示,并且再次初始化为零:`[4]` 是 `0 1 2 3`)。使用空列表会导致数组被截断为空列表。由于数组从零开始计数,第一个元素称为零。这个例子应该更清楚。它的目的是清晰输出,因此它使用了换行符

@array1 = ('I am first', 'I am second', 'I am third', 'I am fourth');
$var1=$array1[0];
$var2=$array1[1];
$var3=$array1[2];
$var4=$array1[3];
print "$var1\n";
print "$var2\n";
print "$var3\n";
print "$var4\n";

输出将是

I am first
I am second
I am third
I am fourth

让我们看另一个例子。这里 `@names` 数组初始化为三个字符串:`John`、`Joe` 和 `Jake`。整个数组被打印到标准输出(`STDOUT`)。元素之间的空格不会被打印。数组的每个元素都会被打印,从索引号零开始。标量变量 `$number` 被赋值为数组 `@names`。赋给它的值是数组 `@names` 中的元素数量。数组 `@names` 中的元素数量被打印

# Populating an array and printing its values
 @names=('John', 'Joe', 'Jake'); # @names=qw/John Joe Jake/;
 print @names, "\n"; # prints without the separator
 print "Hi $names[0], $names[1], and $names[2]!\n";
 $number=@names; # The scalar is assigned the number
 # of elements in the array
 print "There are $number elements in the \@names array.\n";
 print "The last element of the array is $names[$number - 1].\n";
 print "The last element of the array is $names[$#names].\n";
# Remember, the array index starts at zero!!
 @fruit = qw(apples pears peaches plums);
 print "The first element of the \@fruit array is $fruit[0];
the second element is $fruit[1].\n";
print "Starting at the end of the array; @fruit[-1, -3]\n";

输出将是:

JohnJoeJake
Hi John, Joe, and Jake!
There are 3 elements in the @names array.
The last element of the array is Jake.
The last element of the array is Jake.
The first element of the @fruit array is apples;
the second element is pears.
Starting at the end of the array; plums pears

简要了解 WMI

Windows Management Instrumentation 是一个命名空间层次结构,主要用作增强 VBScript 的工具。关于系统状态、页面文件大小、内存利用率(虚拟和物理)、CPU 使用率、IP 设置等信息。下面的 Perl 脚本返回 Windows Installer 应用程序使用的二进制信息(如位图、图标、可执行文件等)的名称和产品代码。它来自 TechNet,可在 Activestate 上找到。请注意,标量被设置为一个具有 *\root\cimv2\* 命名空间的方法。此变量用于形成(项目)对象的集合,这对于任何 Windows 编程都是标准的。要使用 ActiveState 解释器运行此程序,您必须首先安装 `Win32:OLE` 模块

 c:\Perl\> ppm install Win32::OLE

现在模块将在程序内执行其例程

use Win32::OLE('in');
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;

$computer = ".";
$objWMIService = Win32::OLE->GetObject
    ("winmgmts:\\\\$computer\\root\\CIMV2") or die "WMI connection failed.\n";
$colItems = $objWMIService->ExecQuery
    ("SELECT * FROM Win32_Binary","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);

foreach my $objItem (in $colItems)
{
      print "Caption: $objItem->{Caption}\n";
      print "Data: $objItem->{Data}\n";
      print "Description: $objItem->{Description}\n";
      print "Name: $objItem->{Name}\n";
      print "Product Code: $objItem->{ProductCode}\n";
      print "Setting ID: $objItem->{SettingID}\n";
      print "\n";
}

输出是:

Caption: AbortMsiCA.dll
Data: 
Description: AbortMsiCA.dll
Name: AbortMsiCA.dll
Product Code: {90120000-00A1-0409-0000-0000000FF1CE}
Setting ID: 

Caption: BIN_File_46001
Data: 
Description: BIN_File_46001
Name: BIN_File_46001
Product Code: {90120000-00A1-0409-0000-0000000FF1CE}
Setting ID: 

Caption: BIN_File_107602
Data: 
Description: BIN_File_107602
Name: BIN_File_107602
Product Code: {90120000-00A1-0409-0000-0000000FF1CE}
Setting ID: 

Caption: BIN_File_46002
Data: 
Description: BIN_File_46002
Name: BIN_File_46002
Product Code: {90120000-00A1-0409-0000-0000000FF1CE}
Setting ID: 

Caption: OCFXCA
Data: 
Description: OCFXCA
Name: OCFXCA
Product Code: {90120000-00A1-0409-0000-0000000FF1CE}
Setting ID:
and so on . . . 

数组续

多维数组有时被称为表或矩阵。它们由行和列组成,可以用多个下标表示。在二维数组中,第一个下标表示行,第二个下标表示列。在 Perl 中,二维数组中的每一行都用方括号括起来。行是一个未命名的列表。未命名的列表称为匿名数组,并包含其自身的元素。箭头运算符,也称为中缀运算符,可用于获取数组的单个元素。相邻括号之间有一个隐含的 `->`。考虑以下示例。数组 `@matrix` 被赋值为四个未命名或匿名的数组。每个数组有三个值。打印四个匿名数组的地址。要访问匿名数组的单个元素,必须使用双下标或箭头运算符。`@matrix` 数组中第一个匿名数组的第一个元素被打印。`->` 被称为箭头或中缀运算符。它用于解引用数组和哈希引用。`$matrix[0][0]` 或 `$matrix[0]->[0]` 是第一行的第一个元素,其中下标从零开始。打印 `@matrix` 的第二行第一个元素。`$matrix[1]->[0]` 是 `$matrix[1][0]` 的另一种说法。外部 `for` 循环将迭代每一行,从第零行开始。循环第一次迭代后,进入第二个 `for` 循环。内部 `for` 循环比外部循环迭代得更快。打印一行的每个元素,然后控制返回到外部 `for` 循环。接下来,打印矩阵中的每个元素。第一个索引表示行,第二个索引表示列。

@matrix=( [ 3 , 4, 10 ], # Each row is an unnamed list
[ 2, 7, 12 ],
[ 0, 3, 4 ],
[ 6, 5, 9 ],
) ;
 print "@matrix\n";
 print "Row 0, column 0 is $matrix[0][0].\n";
# can also be written - $matrix[0]->[0]
 print "Row 1, column 0 is $matrix[1][0].\n";
# can also be written - $matrix[1]->[0]
 for($i=0; $i < 4; $i++){
 for($x=0; $x < 3; $x++){
print "$matrix[$i][$x] ";
}
print "\n";
}

输出将是:

ARRAY(0x1b6f5c) ARRAY(0x1b6e3c) ARRAY(0x23b064c) ARRAY(0x23b07fc)
Row 0, column 0 is 3.
Row 1, column 0 is 2.
3 4 10
2 7 12
0 3 4
6 5 9

哈希

关联数组,更常称为哈希,由一个或多个标量对组成,这些标量可以是字符串、数字或布尔值。第一组标量与第二组标量相关联。一对字符串中的第一个字符串称为键,第二个字符串称为值。数组是有序列表,以数字 0 开始的索引,而哈希是无序列表,以随机分布的字符串索引。控制台输出通常不会按您键入的方式显示。因此,哈希被定义为键/值对的无序列表,类似于一个表,其中键在左侧,与这些键相关联的值在右侧。哈希名称前面带有 `%` 符号。请注意,此哈希使用了括号

% pet = ( "Name" => "Sneaky",
"Type" => "Cat",
"Owner" => "Carol",
"Color" => "yellow",);

访问哈希的元素需要理解如何访问数组的元素。这意味着我们必须理解处理数组时使用的函数。 examination 以下代码。哈希切片是哈希键的列表,其对应的键值被赋给另一个键列表。列表由后跟 `@` 符号的哈希名称组成。哈希键列表用花括号括起来

# Hash slices
 %officer= ("NAME"=> "John Doe",
"SSN" => "510-22-3456",
"DOB" => "05/19/66"
);
 @info=qw(Marine Captain 50000);
@officer{'BRANCH', 'TITLE', 'SALARY'}=@info;
# This is a hash slice
 @sliceinfo=@officer{'NAME','BRANCH','TITLE'};
# This is also a hash slice
 print "The new values from the hash slice are: @sliceinfo\n\n";
print "The hash now looks like this:\n";
 foreach $key ('NAME', 'SSN', 'DOB', 'BRANCH', 'TITLE', 'SALARY'){
 printf "Key: %-10sValue: %-15s\n", $key, $officer{$key};
}

输出是:

Key: NAME      Value: John Doe
Key: SSN       Value: 510-22-3456
Key: DOB       Value: 05/19/66
Key: BRANCH    Value: Marine
Key: TITLE     Value: Captain
Key: SALARY    Value: 50000

总而言之,请知道互联网上有可以下载的工具,可以将 Perl 脚本转换为 Windows 可执行文件。Perl 在图形和开发桌面应用程序方面也非常强大。本文仅供希望了解 Perl 基本知识的读者参考,无论是否有编程经验,并引用了 **Ankit Farad** 的著作“The Unofficial Guide to Ethical Hacking”以及 **Larry Wall、Tom Christiansen & Jon Orwant** 的著作“Programming Perl”。

© . All rights reserved.