Linux Admin 简明教程

Linux Admin - Set Up Perl for CentOS Linux

Perl 已经存在很长时间。它最初被设计为一种用于解析文本文件的报告语言。随着流行程度的提高,Perl 增加了对模块支持、CPAN、套接字、线程和其他功能的支持,这些功能是强大的脚本语言所需的。

Perl has been around for a long time. It was originally designed as a reporting language used for parsing text files. With increased popularity, Perl has added a module support or CPAN, sockets, threading, and other features needed in a powerful scripting language.

Perl 相对于 PHP、Python 或 Ruby 的最大优势是:它能以最少的麻烦完成任务。这种 Perl 的理念并不总是意味着它能正确地完成任务。然而,对于 Linux 上的管理任务,Perl 被认为是作为脚本语言的最佳选择。

The biggest advantage of Perl over PHP, Python, or Ruby is: it gets things done with minimal fuss. This philosophy of Perl does not always mean it gets things done the right way. However, for administration tasks on Linux, Perl is considered as the go-to choice for a scripting language.

Perl 相对于 Python 或 Ruby 的部分优势有:

Some advantages of Perl over Python or Ruby are −

  1. Powerful text processing

  2. Perl makes writing scripts quick and dirty (usually a Perl script will be several dozen lines shorter than an equivalent in Python or Ruby)

  3. Perl can do anything (almost)

Perl 的部分缺点有:

Some drawbacks of Perl are −

  1. Syntax can be confusing

  2. Coding style in Perl can be unique and bog down collaboration

  3. Perl is not really Object Oriented

  4. Typically, there isn’t a lot of thought put into standardization and best-practice when Perl is used.

当决定使用 Perl、Python 或 PHP 时,应询问以下问题:

When deciding whether to use Perl, Python or PHP; the following questions should be asked −

  1. Will this application ever need versioning?

  2. Will other people ever need to modify the code?

  3. Will other people need to use this application?

  4. Will this application ever be used on another machine or CPU architecture?

如果对上述所有问题的答案均为“不”,那么 Perl 是一个不错的选择,并且可能会在最终结果方面加快速度。

If the answers to all the above are "no", Perl is a good choice and may speed things up in terms of end-results.

在提及这一点之后,让我们配置 CentOS 服务器以使用最新版本的 Perl。

With this mentioned, let’s configure our CentOS server to use the most recent version of Perl.

在安装 Perl 之前,我们需要了解对 Perl 的支持。Perl 仅在最后两个稳定版本中得到官方支持。因此,我们希望确保我们开发环境与 CentOS 版本隔离。

Before installing Perl, we need to understand the support for Perl. Officially, Perl is only supported far back as the last two stable versions. So, we want to be sure to keep our development environment isolated from the CentOS version.

隔离的原因是:如果有人向 CentOS 社区发布使用 Perl 的工具,很有可能它会被修改为在与 CentOS 一起发布的 Perl 上工作。但是,我们也希望为开发目的安装最新版本。与 Python 一样,CentOS 内置 Perl 专注于稳定性,而不是前沿技术。

The reason for isolation is: if someone releases a tool in Perl to the CentOS community, more than likely it will be modified to work on Perl as shipped with CentOS. However, we also want to have the latest version installed for development purposes. Like Python, CentOS ships Perl focused on the reliability and not cutting edge.

让我们检查一下 CentOS 7 中的当前 Perl 版本。

Let’s check our current version of Perl on CentOS 7.

[root@CentOS]# perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi

我们当前运行的是 Perl 5.16.3。截至撰写本文时,最新版本为:perl-5.24.0

We are currently running Perl 5.16.3. The most current version as of this writing is: perl-5.24.0

我们肯定想要升级我们的版本,能够在我们的代码中使用最新的 Perl 模块。幸运的是,有一个很好的工具可以维护 Perl 环境,同时还能将我们的 CentOS Perl 版本保持隔离。它叫做 perlbrew

We definitely want to upgrade our version, being able to use up-to-date Perl modules in our code. Fortunately, there is a great tool for maintaining Perl environments and keeping our CentOS version of Perl isolated. It is called perlbrew.

我们来安装 Perl Brew。

Let’s install Perl Brew.

[root@CentOS]# curl -L https://install.perlbrew.pl | bash
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
100   170  100   170    0     0    396      0 --:--:-- --:--:-- --:--:--   397
100  1247  100  1247    0     0   1929      0 --:--:-- --:--:-- --:--:--  1929

现在我们已经安装了 Perl Brew,让我们来为 Perl 最新版本创建一个环境。

Now that we have Perl Brew installed, let’s make an environment for the latest version of Perl.

首先,我们需要当前安装的 Perl 版本来引导 perlbrew 安装。因此,让我们从 CentOS 存储库中获取一些需要的 Perl 模块。

First, we will need the currently installed version of Perl to bootstrap the perlbrew install. Thus, let’s get some needed Perl modules from the CentOS repository.

Note - 在可用时,我们总希望在 CentOS Perl 安装中使用 CentOS Perl 模块,而不是 CPAN。

Note − When available we always want to use CentOS Perl modules versus CPAN with our CentOS Perl installation.

Step 1 - 安装 CentOS Perl Make::Maker 模块。

Step 1 − Install CentOS Perl Make::Maker module.

[root@CentOS]# yum -y install perl-ExtUtils-MakeMaker.noarch

Step 2 - 安装最新版本的 perl。

Step 2 − Install the latest version of perl.

[root@CentOS build]# source ~/perl5/perlbrew/etc/bashrc
[root@CentOS build]# perlbrew install -n -j4 --threads perl-5.24.1

我们为 Perl 安装选择的选项是 -

The options we chose for our Perl install are −

  1. n − No tests

  2. j4 − Execute 4 threads in parallel for the installation routines (we are using a quadcore CPU)

  3. threads − Enable threading support for Perl

在安装成功执行后,让我们切换到我们最新的 Perl 环境。

After our installation has been performed successfully, let’s switch to our newest Perl environment.

[root@CentOS]# ~/perl5/perlbrew/bin/perlbrew use perl-5.24.1

A sub-shell is launched with perl-5.24.1 as the activated perl. Run 'exit' to finish it.

[root@CentOS]# perl -v

This is perl 5, version 24, subversion 1 (v5.24.1) built for x86_64-linuxthread-multi

(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2017, 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.

[root@CentOS]#

简单的 Perl 脚本打印在 perlbrew 环境上下文中运行的 Perl 版本 -

Simple perl script printing perl version running within the context of our perlbrew environment −

[root@CentOS]# cat ./ver.pl
#!/usr/bin/perl
print $^V . "\n";
[root@CentOS]# perl ./ver.pl
v5.24.1
[root@CentOS]#

Perl 安装后,我们可以使用 perl brew 的 cpanm 加载 cpan 模块 -

Once perl is installed, we can load cpan modules with perl brew’s cpanm −

[root@CentOS]# perl-brew install-cpanm

现在,让我们使用 cpanm 安装程序在 perl brew 中用我们当前的 Perl 版本 5.24.1 来制作 LWP 模块。

Now let’s use the cpanm installer to make the LWP module with our current Perl version of 5.24.1 in perl brew.

Step 1 - 切换到我们当前 Perl 版本的上下文中。

Step 1 − Switch to the context of our current Perl version.

[root@CentOS ~]# ~/perl5/perlbrew/bin/perlbrew use perl-5.24.1

一个子 shell 以 perl-5.24.1 作为激活的 perl 启动。运行“exit”来结束它。

A sub-shell is launched with perl-5.24.1 as the activated perl. Run 'exit' to finish it.

[root@CentOS ~]#

Step 2 - 安装 LWP 用户代理 Perl 模块。

Step 2 − Install LWP User Agent Perl Module.

[root@CentOS ~]# ~/perl5/perlbrew/bin/cpanm -i LWP::UserAgent

Step 3 - 现在,让我们使用新的 CPAN 模块来测试我们的 Perl 环境。

Step 3 − Now let’s test our Perl environment with the new CPAN module.

[root@CentOS ~]# cat ./get_header.pl
#!/usr/bin/perl
use LWP;
my $browser = LWP::UserAgent->new();
my $response = $browser->get("http://www.slcc.edu/");
unless(!$response->is_success) {
   print $response->header("Server");
}

[root@CentOS ~]# perl ./get_header.pl
Microsoft-IIS/8.5 [root@CentOS ~]#

就是这样!Perl Brew 使 perl 环境隔离变得小事一桩,并且可以被认为是 Perl 中的最佳实践。

There you have it! Perl Brew makes isolating perl environments a snap and can be considered as a best practice as things get with Perl.