ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

【perl】cpan コマンドで Perl モジュールをインストール

Perl では外部モジュールを利用するのに CPAN を利用します。

CPAN とは

CPAN(シーパン、Comprehensive Perl Archive Network)とは、Perl のライブラリ・モジュールやその他の Perl で書かれたソフトウェアを集めた巨大なアーカイブです。
ja.wikipedia.org

セットアップ

初期設定

まっさらな環境で CPAN コマンドを利用しようとすると、初期設定が行われます。
cpan コマンドを実行すると以下のような流れで初期設定が行われます。
デフォルトyesなのでそのままエンターを押せばすべて自動的によしなに設定がされます。

$ cpan
Loading internal logger. Log::Log4perl recommended for better logging

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] 

 <install_help>

Warning: You do not have write permission for Perl library directories.

To install modules, you need to configure a local Perl library directory or
escalate your privileges.  CPAN can help you by bootstrapping the local::lib
module or by configuring itself to use 'sudo' (if available).  You may also
resolve this problem manually if you need to customize your setup.

What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
 [local::lib] 

Autoconfiguration complete.

Attempting to bootstrap local::lib...

Writing /home/hoge/.cpan/CPAN/MyConfig.pm for bootstrap...
commit: wrote '/home/hoge/.cpan/CPAN/MyConfig.pm'
Fetching with HTTP::Tiny:
http://www.cpan.org/authors/01mailrc.txt.gz
Reading '/home/hoge/.cpan/sources/authors/01mailrc.txt.gz'
............................................................................DONE
Fetching with HTTP::Tiny:
http://www.cpan.org/modules/02packages.details.txt.gz
Reading '/home/hoge/.cpan/sources/modules/02packages.details.txt.gz'
  Database was generated on Fri, 25 Nov 2022 07:29:01 GMT
  HTTP::Date not available
..............
  New CPAN.pm version (v2.34) available.
  [Currently running version is v2.28]
  You might want to try
    install CPAN
    reload cpan
  to both upgrade CPAN.pm and run the new version without leaving
  the current session.


..............................................................DONE
Fetching with HTTP::Tiny:
http://www.cpan.org/modules/03modlist.data.gz
Reading '/home/hoge/.cpan/sources/modules/03modlist.data.gz'
DONE
Writing /home/hoge/.cpan/Metadata
Fetching with HTTP::Tiny:
http://www.cpan.org/authors/id/H/HA/HAARG/local-lib-2.000029.tar.gz
Fetching with HTTP::Tiny:
http://www.cpan.org/authors/id/H/HA/HAARG/CHECKSUMS
Checksum for /home/hoge/.cpan/sources/authors/id/H/HA/HAARG/local-lib-2.000029.tar.gz ok
'YAML' not installed, will not store persistent state
Configuring H/HA/HAARG/local-lib-2.000029.tar.gz with Makefile.PL
Attempting to create directory /home/hoge/perl5
Checking if your kit is complete...
Looks good
Generating a Unix-style Makefile
Writing Makefile for local::lib
Writing MYMETA.yml and MYMETA.json
  HAARG/local-lib-2.000029.tar.gz
  /usr/bin/perl Makefile.PL --bootstrap -- OK
Running make for H/HA/HAARG/local-lib-2.000029.tar.gz
cp lib/POD2/DE/local/lib.pod blib/lib/POD2/DE/local/lib.pod
cp lib/local/lib.pm blib/lib/local/lib.pm
cp lib/POD2/PT_BR/local/lib.pod blib/lib/POD2/PT_BR/local/lib.pod
cp lib/lib/core/only.pm blib/lib/lib/core/only.pm
Manifying 4 pod documents
  HAARG/local-lib-2.000029.tar.gz
  /usr/bin/make -- OK
The current configuration of allow_installing_outdated_dists is 'ask/no', but for this option we would need 'CPAN::DistnameInfo' installed. Please install 'CPAN::DistnameInfo' as soon as possible. As long as we are not equipped with 'CPAN::DistnameInfo' this option does not take effect
Running make test for HAARG/local-lib-2.000029.tar.gz
PERL_DL_NONLAZY=1 "/usr/bin/perl" "-I/home/hoge/perl5/lib/perl5" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/bad_variables.t ...... ok   
t/carp-mismatch.t ...... ok   
t/classmethod.t ........ ok   
t/de-dup.t ............. ok   
t/lib-core-only.t ...... ok   
t/pipeline.t ........... ok   
t/shell.t .............. ok       
t/stackable.t .......... ok     
t/subroutine-in-inc.t .. ok   
t/taint-mode.t ......... ok   
All tests successful.
Files=10, Tests=173,  2 wallclock secs ( 0.05 usr  0.01 sys +  1.35 cusr  0.34 csys =  1.75 CPU)
Result: PASS
  HAARG/local-lib-2.000029.tar.gz
  /usr/bin/make test -- OK
Running make install for HAARG/local-lib-2.000029.tar.gz
Manifying 4 pod documents
Installing /home/hoge/perl5/lib/perl5/local/lib.pm
Installing /home/hoge/perl5/lib/perl5/POD2/PT_BR/local/lib.pod
Installing /home/hoge/perl5/lib/perl5/POD2/DE/local/lib.pod
Installing /home/hoge/perl5/lib/perl5/lib/core/only.pm
Installing /home/hoge/perl5/man/man3/POD2::DE::local::lib.3pm
Installing /home/hoge/perl5/man/man3/POD2::PT_BR::local::lib.3pm
Installing /home/hoge/perl5/man/man3/lib::core::only.3pm
Installing /home/hoge/perl5/man/man3/local::lib.3pm
Appending installation info to /home/hoge/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/perllocal.pod
  HAARG/local-lib-2.000029.tar.gz
  /usr/bin/make install  -- OK

local::lib is installed. You must now add the following environment variables
to your shell configuration files (or registry, if you are on Windows) and
then restart your command line shell and CPAN before installing modules:

PATH="/home/hoge/perl5/bin${PATH:+:${PATH}}"; export PATH;
PERL5LIB="/home/hoge/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/hoge/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/hoge/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/hoge/perl5"; export PERL_MM_OPT;

Would you like me to append that to /home/hoge/.zshrc now? [yes] 


commit: wrote '/home/hoge/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

To fix that, maybe try>  install Term::ReadLine::Perl


cpan shell -- CPAN exploration and modules installation (v2.28)
Enter 'h' for help.

cpan[1]> 

設定内容確認

CPAN シェルで、o conf というコマンドを発行すると、設定状況を一覧することができます。

cpan[1]> o conf
$CPAN::Config options from /home/hoge/.cpan/CPAN/MyConfig.pm:
    commit             [Commit changes to disk]
    defaults           [Reload defaults from disk]
    help               [Short help about 'o conf' usage]
    init               [Interactive setting of all options]

    allow_installing_module_downgrades [ask/no]
    allow_installing_outdated_dists [ask/no]
    applypatch         []
    auto_commit        [0]
    build_cache        [100]
    build_dir          [/home/hoge/.cpan/build]
    build_dir_reuse    [0]
    build_requires_install_policy [yes]
    bzip2              [/usr/bin/bzip2]
    cache_metadata     [1]
    check_sigs         [0]
    cleanup_after_install [0]
    colorize_debug     undef
    colorize_output    [0]
    colorize_print     undef
    colorize_warn      undef
    commandnumber_in_prompt [1]
    commands_quote     undef
    connect_to_internet_ok [1]
    cpan_home          [/home/hoge/.cpan]
    curl               undef
    dontload_hash      undef
    dontload_list      undef
    ftp                undef
    ftp_passive        [1]
    ftp_proxy          []
    ftpstats_period    undef
    ftpstats_size      undef
    getcwd             [cwd]
    gpg                [/usr/bin/gpg]
    gzip               [/usr/bin/gzip]
    halt_on_failure    [0]
    histfile           [/home/hoge/.cpan/histfile]
    histsize           [100]
    http_proxy         []
    inactivity_timeout [0]
    index_expire       [1]
    inhibit_startup_message [0]
    keep_source_where  [/home/hoge/.cpan/sources]
    load_module_verbosity [none]
    lynx               undef
    make               [/usr/bin/make]
    make_arg           []
    make_install_arg   []
    make_install_make_command [/usr/bin/make]
    makepl_arg         [INSTALLDIRS=site]
    mbuild_arg         []
    mbuild_install_arg []
    mbuild_install_build_command [./Build]
    mbuildpl_arg       [--installdirs site]
    ncftp              undef
    ncftpget           undef
    no_proxy           []
    pager              [/usr/bin/less]
    password           undef
    patch              [/usr/bin/patch]
    patches_dir        undef
    perl5lib_verbosity [none]
    plugin_list       
    prefer_external_tar [1]
    prefer_installer   [MB]
    prefs_dir          [/home/hoge/.cpan/prefs]
    prerequisites_policy [follow]
    proxy_pass         undef
    proxy_user         undef
    randomize_urllist  undef
    recommends_policy  [1]
    scan_cache         [atstart]
    shell              [/bin/zsh]
    show_unparsable_versions [0]
    show_upload_date   [0]
    show_zero_versions [0]
    suggests_policy    [0]
    tar                [/usr/bin/tar]
    tar_verbosity      [none]
    term_is_latin      [1]
    term_ornaments     [1]
    test_report        [0]
    trust_test_report_history [0]
    unzip              [/usr/bin/unzip]
    urllist           
	0 [http://www.cpan.org/]
    urllist_ping_external undef
    urllist_ping_verbose undef
    use_prompt_default [0]
    use_sqlite         [0]
    username           undef
    version_timeout    [15]
    wait_list          undef
    wget               [/usr/bin/wget]
    yaml_load_code     [0]
    yaml_module        [YAML]


cpan[2]> 

モジュールインストール

以下のようなコマンドで、DBI と DBD::MySQL がインストールできます。

cpan> install DBI
cpan> install DBD::MySQL