Linux – Perl & Modules

Loading

安裝 Perl – Practical Extraction and Report Language , 測試環境為 CentOS 7 x64 (虛擬機)

前置作業 – 所需套件

[root@localhost ~]# yum groupinstall development
[root@localhost ~]# yum -y install perl*

Perl

[root@localhost ~]# perl -v

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 33 registered patches, see perl -V for more detail)

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.

在 CentOS 7 使用版本為 perl 5, version 16, subversion 3 (v5.16.3) ,需要更新可以透過手動方式.

[root@localhost ~]# curl -L http://xrl.us/installperlnix | bash
...
## Installing patchperl

## Done.
Checking/updating /root/.bashrc to source perlbrew bashrc
Updating your current environment
Installing Perl 5.20.1 through perlbrew
Fetching perl 5.20.1 as /root/perl5/perlbrew/dists/perl-5.20.1.tar.bz2
Download http://www.cpan.org/authors/id/S/SH/SHAY/perl-5.20.1.tar.bz2 to /root/perl5/perlbrew/dists/perl-5.20.1.tar.bz2
Installing /root/perl5/perlbrew/build/perl-5.20.1/perl-5.20.1 into ~/perl5/perlbrew/perls/perl-5.20.1

This could take a while. You can run the following command on another shell to track the status:

  tail -f ~/perl5/perlbrew/build.perl-5.20.1.log

perl-5.20.1 is successfully installed.
Setting Perl 5.20.1 to default
Installing cpanm,

cpanm is installed to

    /root/perl5/perlbrew/bin/cpanm

------------------
Install complete - close this terminal window and open a new one,
then to confirm Perl 5.20.1 is installed type: perl -v

安裝後需要自行設定 softlink 或是 alias (需寫到使用者的 .bashrc).

[root@localhost ~]# ln -s /root/perl5/perlbrew/perls/perl-5.20.1/bin/perl /usr/bin/perl5
[root@localhost ~]# perl5 -v

This is perl 5, version 20, subversion 1 (v5.20.1) built for x86_64-linux
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2014, 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@localhost ~]# alias perl520=/root/perl5/perlbrew/perls/perl-5.20.1/bin/perl

試一下 perl 是否正常運作.

[root@localhost ~]# perl5 -e 'print "hello world\n";'
hello world
[root@localhost ~]# perl520 -e 'print "hello world\n";'
hello world

Perl 指令參數:
[-e|-E] ’command’ , -e 後面就是接著使用 perl 語法.

自行下載 perl source code 後編輯也可以.

[root@localhost ~]# cd /tmp/
[root@localhost ~]# wget http://www.cpan.org/src/5.0/perl-5.20.1.tar.gz
[root@localhost ~]# tar -zxvf perl-5.20.1.tar.gz
[root@localhost ~]# cd perl-5.20.1.tar.gz/
[root@localhost ~]# ./Configure 
[root@localhost ~]# make 
[root@localhost ~]# make test 
[root@localhost ~]# make install

Perl Modules 模組安裝,更新可以透過下面方式.

  1. Source Code

    從 http://search.cpan.org/ 下載模組的原始碼後執行以下的指令來編譯.

    [root@localhost ~]# perl Makefile.PL 
    [root@localhost ~]# make 
    [root@localhost ~]# make test 
    [root@localhost ~]# make install
    
  2. CPAN

    cpan 應該是最早更新模組的指令 (1997 v5.004),後面還有許多類似的指令.
    CPAN – Comprehensive Perl Archive Network 是管理 Perl 模組的程式,下面範例透過 cpan 來安裝 cpanminus (無使用 shell ,直接下命令安裝模組指令).

    [root@localhost ~]# perl -MCPAN -e shell
    Terminal does not support AddHistory.
    
    cpan shell -- CPAN exploration and modules installation (v1.9800)
    Enter 'h' for help.
    
    cpan[1]> install App::cpanminus
    Reading '/root/.local/share/.cpan/Metadata'
      Database was generated on Wed, 30 May 2018 22:17:02 GMT
    Running install for module 'App::cpanminus'
    Running make for M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz
    Fetching with LWP:
    http://cpan.stu.edu.tw/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz
    Fetching with LWP:
    http://cpan.stu.edu.tw/authors/id/M/MI/MIYAGAWA/CHECKSUMS
    Checksum for /root/.local/share/.cpan/sources/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz ok
    Scanning cache /root/.local/share/.cpan/build for sizes
    ............................................................................DONE
    
      CPAN.pm: Building M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz
    
    Checking if your kit is complete...
    Looks good
    Writing Makefile for App::cpanminus
    Writing MYMETA.yml and MYMETA.json
    cp lib/App/cpanminus/fatscript.pm blib/lib/App/cpanminus/fatscript.pm
    cp lib/App/cpanminus.pm blib/lib/App/cpanminus.pm
    cp bin/cpanm blib/script/cpanm
    /usr/bin/perl -MExtUtils::MY -e 'MY->fixin(shift)' -- blib/script/cpanm
    Manifying blib/man1/cpanm.1
    Manifying blib/man3/App::cpanminus::fatscript.3pm
    Manifying blib/man3/App::cpanminus.3pm
      MIYAGAWA/App-cpanminus-1.7044.tar.gz
      /usr/bin/make -- OK
    Running make test
    PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
    t/happy_cpantesters.t .. 1/1 # App::cpanminus/1.7044
    t/happy_cpantesters.t .. ok   
    All tests successful.
    Files=1, Tests=1,  0 wallclock secs ( 0.03 usr  0.00 sys +  0.02 cusr  0.01 csys =  0.06 CPU)
    Result: PASS
      MIYAGAWA/App-cpanminus-1.7044.tar.gz
      /usr/bin/make test -- OK
    Running make install
    Installing /usr/local/share/perl5/App/cpanminus.pm
    Installing /usr/local/share/perl5/App/cpanminus/fatscript.pm
    Installing /usr/local/share/man/man1/cpanm.1
    Installing /usr/local/share/man/man3/App::cpanminus::fatscript.3pm
    Installing /usr/local/share/man/man3/App::cpanminus.3pm
    Installing /usr/local/bin/cpanm
    Appending installation info to /usr/lib64/perl5/perllocal.pod
      MIYAGAWA/App-cpanminus-1.7044.tar.gz
      /usr/bin/make install  -- OK
    
    cpan[2]> Terminal does not support GetHistory.
    Lockfile removed.
    

    按 ctrl + D 可以跳出.
    除了剛剛方式也可以直接用 #cpan 指令.

    [root@localhost ~]# cpan
    Terminal does not support AddHistory.
    
    cpan shell -- CPAN exploration and modules installation (v1.9800)
    Enter 'h' for help.
    
    cpan[1]> install App::cpanminus
    Reading '/root/.local/share/.cpan/Metadata'
      Database was generated on Wed, 30 May 2018 22:17:02 GMT
    App::cpanminus is up to date (1.7044).
    
    cpan[2]> Terminal does not support GetHistory.
    Lockfile removed.
    
    [root@localhost ~]# cpan App::cpanminus
    
  3. CPANM

    cpanm from App::cpanminus is a script to get, unpack, build and install modules from CPAN. It’s dependency free (can bootstrap itself) and requires zero configuration. I
    cpanm – cpanminus , 無使用 shell ,直接下命令安裝模組.

    [root@localhost ~]# cpanm
    Usage: cpanm [options] Module [...]
    
    Try `cpanm --help` or `man cpanm` for more options.
    

    當 cpan 找不到時可以試試看 cpanm .

    [root@localhost ~]# cpan LWP::Protocol::https10
    Going to read '/root/.cpan/Metadata'
      Database was generated on Mon, 04 Jun 2018 15:41:02 GMT
    Warning: Cannot install LWP::Protocol::https10, don't know what it is.
    Try the command
    
        i /LWP::Protocol::https10/
    
    to find objects with matching identifiers.
    
    [root@localhost ~]# cpanm LWP::Protocol::https10
    ! Finding LWP::Protocol::https10 on cpanmetadb failed.
    --> Working on LWP::Protocol::https10
    Fetching http://cpan.metacpan.org/authors/id/G/GA/GAAS/libwww-perl-5.837.tar.gz ... OK
    Configuring libwww-perl-5.837 ... OK
    
  4. CPANP

    與 cpanm 或 cpan 相比,他提供更多的選項.

    [root@localhost ~]# cpanp
    CPANPLUS::Shell::Default -- CPAN exploration and module installation (v0.9138)
    *** Please report bugs to <bug-cpanplus@rt.cpan.org>.
    *** Using CPANPLUS::Backend v0.9138.  
    *** ReadLine support available (try 'i Term::ReadLine::Perl').
    
    *** Type 'p' now to show start up log
    
    Did you know...
        You can install modules by URL using 'i URL'
    CPAN Terminal> 
    Exiting CPANPLUS shell
    

那要如何確認 Perl 模組是否安裝成功,可以透過 perl 指令本身.

[root@localhost ~]# perl -MApp::cpanminus -e "print \"Module installed.\\n\";"
Module installed.

找不到相對應的模組時

[root@localhost ~]# perl -MApp::cpanminuds -e "print \"Module installed.\\n\";"
Can't locate App/cpanminuds.pm in @INC (@INC contains: /usr/local/lib/perl5/site_perl/5.14.0/ /usr/local/lib/perl5/5.14.0/ /usr/local/lib/perl5/site_perl/5.14.0/x86_64-linux /usr/local/lib/perl5/site_perl/5.14.0 /usr/local/lib/perl5/5.14.0/x86_64-linux /usr/local/lib/perl5/5.14.0 .).
BEGIN failed--compilation aborted.

也可以利用指令 instmodsh 來執行查詢(所有或是個別的模組).

[root@localhost ~]# instmodsh
Available commands are:
   l            - List all installed modules
   m <module>   - Select a module
   q            - Quit the program
cmd? l
Installed modules are:
   Algorithm::C3
   Algorithm::Diff
   Alien::Build
...
cmd? q

指令的方式也可以.

[root@localhost ~]# perl -e 'while (<@INC>) { while (<$_/*.pm>) { print "$_\n"; } }'
[root@localhost ~]# perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'

可以讀取 PERLLIB 環境變數,來查看模組儲存位置.

[root@localhost ~]# echo $PERLLIB
/usr/local/lib/perl5/site_perl/5.14.0/:/usr/local/lib/perl5/5.14.0/
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料