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)

  • threads - 為 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]#

簡單的 perl 指令碼列印在 perlbrew 環境上下文中執行的 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 中當前的 5.24.1 Perl 版本建立 LWP 模組。

步驟 1 - 切換到我們當前 Perl 版本的上下文。

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

將啟動一個子 shell,並將 perl-5.24.1 作為啟用的 perl。執行“exit”以結束它。

[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 的發展情況就是這樣。

廣告

© . All rights reserved.