Linux 管理员 – 为 CentOS Linux 设置 Perl

Linux 管理员 – 为 CentOS Linux 设置 Perl


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

Perl 相对于 PHP、Python 或 Ruby 的最大优势是:它以最小的麻烦完成任务。Perl 的这种哲学并不总是意味着它以正确的方式完成工作。但是,对于 Linux 上的管理任务,Perl 被认为是脚本语言的首选。

Perl 相对于 Python 或 Ruby 的一些优势是 –

  • 强大的文本处理

  • Perl 使编写脚本变得又快又脏(通常 Perl 脚本比 Python 或 Ruby 中的等价脚本要短几十行)

  • Perl 可以做任何事情(几乎)

Perl 的一些缺点是 –

  • 语法可能令人困惑

  • Perl 中的编码风格可能是独一无二的,并且会使协作陷入困境

  • Perl 并不是真正的面向对象

  • 通常,在使用 Perl 时,没有太多考虑标准化和最佳实践。

在决定使用 Perl、Python 还是 PHP 时;应该问以下问题 –

  • 此应用程序是否需要版本控制?
  • 其他人是否需要修改代码?
  • 其他人是否需要使用此应用程序?
  • 此应用程序是否会在另一台机器或 CPU 架构上使用?

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

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

在安装 Perl 之前,我们需要了解对 Perl 的支持。正式地,Perl 仅作为最后两个稳定版本得到支持。所以,我们要确保我们的开发环境与 CentOS 版本隔离。

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

让我们在 CentOS 7 上检查我们当前的 Perl 版本。

[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

我们肯定希望升级我们的版本,以便能够在我们的代码中使用最新的 Perl 模块。幸运的是,有一个很好的工具可以维护 Perl 环境并保持我们的 CentOS 版本的 Perl 隔离。它被称为perlbrew

让我们安装 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 创建一个环境。

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

注意– 当可用时,我们总是希望在 CentOS Perl 安装中使用 CentOS Perl 模块而不是 CPAN。

步骤 1 – 安装 CentOS Perl Make::Maker 模块。

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

第 2 步– 安装最新版本的 perl。

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

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

  • n – 没有测试

  • j4 – 为安装例程并行执行 4 个线程(我们使用的是四核 CPU)

  • 线程– 启用对 Perl 的线程支持

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

[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]#

在我们的 perlbrew 环境的上下文中运行的简单 perl 脚本打印 perl 版本 –

[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 模块 –

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

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

步骤 1 – 切换到我们当前 Perl 版本的上下文。

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

使用 perl-5.24.1 作为激活的 perl 启动子 shell。运行“退出”以完成它。

[root@CentOS ~]#

步骤 2 – 安装 LWP 用户代理 Perl 模块。

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

第 3 步– 现在让我们使用新的 CPAN 模块测试我们的 Perl 环境。

[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 的发展,可以将其视为最佳实践。

觉得文章有用?

点个广告表达一下你的爱意吧 !😁