Perl 简明教程
Perl - Environment
在开始编写 Perl 程序之前,让我们了解如何设置 Perl 环境。Perl 可在各种平台上使用 −
Before we start writing our Perl programs, let’s understand how to setup our Perl environment. Perl is available on a wide variety of platforms −
-
Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX etc.)
-
Win 9x/NT/2000/
-
WinCE
-
Macintosh (PPC, 68K)
-
Solaris (x86, SPARC)
-
OpenVMS
-
Alpha (7.2 and later)
-
Symbian
-
Debian GNU/kFreeBSD
-
MirOS BSD
-
And many more…
您的系统很可能已经安装了 Perl。只需在 $ 提示符下输入以下命令即可 −
This is more likely that your system will have perl installed on it. Just try giving the following command at the $ prompt −
$perl -v
如果您已经在机器上安装了 Perl,您将收到如下消息 −
If you have perl installed on your machine, then you will get a message something as follows −
This is perl 5, version 16, subversion 2 (v5.16.2) built for i686-linux
Copyright 1987-2012, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
如果您还没有安装 Perl,请继续进行下一部分。
If you do not have perl already installed, then proceed to the next section.
Getting Perl Installation
最新的源代码、二进制文件、文档、新闻等可在 Perl 的官方网站上找到。
The most up-to-date and current source code, binaries, documentation, news, etc. are available at the official website of Perl.
Perl Official Website − https://www.perl.org/
Perl Official Website − https://www.perl.org/
您可以从以下网站下载 Perl 文档。
You can download Perl documentation from the following site.
Perl Documentation Website − https://perldoc.perl.org
Perl Documentation Website − https://perldoc.perl.org
Install Perl
Perl 发行版适用于各种平台。您只需下载适用于您平台的二进制代码并安装 Perl。
Perl distribution is available for a wide variety of platforms. You need to download only the binary code applicable for your platform and install Perl.
如果你的平台没有二进制代码,你需要 C 编译器来手动编译源代码。编译源代码在安装中所需特性的选择方面提供了更大的灵活性。
If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.
以下是关于在各种平台上安装 Perl 的快速概览。
Here is a quick overview of installing Perl on various platforms.
Unix and Linux Installation
下面是有关在 Unix/Linux 计算机上安装 Perl 的简单步骤。
Here are the simple steps to install Perl on Unix/Linux machine.
-
Open a Web browser and go to https://www.perl.org/get.html.
-
Follow the link to download zipped source code available for Unix/Linux.
-
Download perl-5.x.y.tar.gz file and issue the following commands at $ prompt.
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
NOTE −此处 $ 是 Unix 提示符,您可在此处键入命令,因此请确保在键入上述命令时您没有键入 $。
NOTE − Here $ is a Unix prompt where you type your command, so make sure you are not typing $ while typing the above mentioned commands.
在标准位置 /usr/local/bin 中安装 Perl,并将其库安装在 /usr/local/lib/perlXX 中,其中 XX 是您正在使用的 Perl 版本。
This will install Perl in a standard location /usr/local/bin and its libraries are installed in /usr/local/lib/perlXX, where XX is the version of Perl that you are using.
在发出 make 命令后,将会花费一些时间来编译源代码。安装完成后,您可以在 $ 提示符处发出 perl -v 命令来检查 Perl 安装。如果一切都正常,那么它将显示如上所示的消息。
It will take a while to compile the source code after issuing the make command. Once installation is done, you can issue perl -v command at $ prompt to check perl installation. If everything is fine, then it will display message like we have shown above.
Windows Installation
以下是有关在 Windows 计算机上安装 Perl 的步骤。
Here are the steps to install Perl on Windows machine.
-
Follow the link for the Strawberry Perl installation on Windows http://strawberryperl.com
-
Download either 32bit or 64bit version of installation.
-
Run the downloaded file by double-clicking it in Windows Explorer. This brings up the Perl install wizard, which is really easy to use. Just accept the default settings, wait until the installation is finished, and you’re ready to roll!
Macintosh Installation
要构建自己的 Perl 版本,您需要“make”,它是苹果开发者工具的一部分,通常随 Mac OS 安装 DVD 一起提供。无需最新的 Xcode 版本(现在要收费)才能安装 make。
In order to build your own version of Perl, you will need 'make', which is part of the Apples developer tools usually supplied with Mac OS install DVDs. You do not need the latest version of Xcode (which is now charged for) in order to install make.
以下是有关在 Mac OS X 计算机上安装 Perl 的简单步骤。
Here are the simple steps to install Perl on Mac OS X machine.
-
Open a Web browser and go to https://www.perl.org/get.html.
-
Follow the link to download zipped source code available for Mac OS X.
-
Download perl-5.x.y.tar.gz file and issue the following commands at $ prompt.
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
在标准位置 /usr/local/bin 中安装 Perl,并将其库安装在 /usr/local/lib/perlXX 中,其中 XX 是您正在使用的 Perl 版本。
This will install Perl in a standard location /usr/local/bin and its libraries are installed in /usr/local/lib/perlXX, where XX is the version of Perl that you are using.
Running Perl
以下是启动 Perl 的不同方法。
The following are the different ways to start Perl.
Interactive Interpreter
您可以输入 perl ,并通过从命令行启动它来在交互式解释器中立即开始编码。您可以从 Unix、DOS 或任何其他系统执行此操作,这些系统将为您提供命令行解释器或外壳窗口。
You can enter perl and start coding right away in the interactive interpreter by starting it from the command line. You can do this from Unix, DOS, or any other system, which provides you a command-line interpreter or shell window.
$perl -e <perl code> # Unix/Linux
or
C:>perl -e <perl code> # Windows/DOS
以下是所有可用命令行选项的列表:
Here is the list of all the available command line options −
Sr.No. |
Option & Description |
1 |
-d[:debugger] Runs program under debugger |
2 |
-Idirectory Specifies @INC/#include directory |
3 |
-T Enables tainting checks |
4 |
-t Enables tainting warnings |
5 |
-U Allows unsafe operations |
6 |
-w Enables many useful warnings |
7 |
-W Enables all warnings |
8 |
-X Disables all warnings |
9 |
-e program Runs Perl script sent in as program |
10 |
file Runs Perl script from a given file |
Script from the Command-line
Perl 脚本是一个文本文件,其中包含 Perl 代码,可以在命令行中调用应用程序上的解释器来执行,如下所示 −
A Perl script is a text file, which keeps perl code in it and it can be executed at the command line by invoking the interpreter on your application, as in the following −
$perl script.pl # Unix/Linux
or
C:>perl script.pl # Windows/DOS
Integrated Development Environment
您也可以从图形用户界面 (GUI) 环境中运行 Perl。您只需在支持 Perl 的系统上使用 GUI 应用程序即可。您可以下载 Padre, the Perl IDE 。如果您熟悉 Eclipse,还可以使用 Eclipse 插件 EPIC - Perl Editor and IDE for Eclipse 。
You can run Perl from a graphical user interface (GUI) environment as well. All you need is a GUI application on your system that supports Perl. You can download Padre, the Perl IDE. You can also use Eclipse Plugin EPIC - Perl Editor and IDE for Eclipse if you are familiar with Eclipse.
在进入下一章之前,请确保您的环境已正确设置且运行正常。如果您无法正确设置环境,则可以向系统管理员寻求帮助。
Before proceeding to the next chapter, make sure your environment is properly setup and working perfectly fine. If you are not able to setup the environment properly then you can take help from your system admininstrator.
后续章节给出的所有示例均使用 CentOS 发行版 Linux 上可用的 v5.16.2 版本执行。
All the examples given in subsequent chapters have been executed with v5.16.2 version available on the CentOS flavor of Linux.