CPAN の 使 い 方 2009/5/7 斎 藤 輪 太 郎 CPAN(http://www.cpan.org/)は Comprehensive Perl Archive Network の 略 で Perlで 作 成 されたソフトウェアやその 関 連 文 書 の 巨 大 な 集 合 体 です 多 くの 開 発 者 たちが 開 発 し た 膨 大 な 量 のPerlプログラムがCPANに 登 録 されています 従 ってソフトウェア 開 発 に 先 立 ってまず 自 分 が 開 発 しようとしているソフトウェアと 同 じ 機 能 を 持 つものがCPANに 登 録 されていないかを 調 べ もし 登 録 されていればそれを 利 用 すべきでしょう Perlプログラマ は 積 極 的 にCPANを 利 用 することで 開 発 にかかる 時 間 を 大 幅 に 削 減 することができるので す 1. 初 期 設 定 root 権 限 を 持 たない 一 般 ユーザ 向 けの CPAN の 初 期 設 定 方 法 を 紹 介 します 1.1 モジュールダウンロードディレクトリの 作 成 自 分 のホームディレクトリ 下 に CPAN からモジュールをダウンロードするためのディレク トリを 作 成 します この 場 合 はディレクトリ 名 は 任 意 です mkdir p ~/Library/Perl5 1.2 CPAN 設 定 ファイルの 作 成 CPAN 用 の 設 定 ファイルを 以 下 のように 決 められたディレクトリに 作 成 します mkdir -p ~/.cpan/cpan echo '$CPAN::Config = {}' > ~/.cpan/cpan/myconfig.pm 1.3 CPAN 対 話 モードの 起 動 CPAN 対 話 モードに 入 ります cpan すると 様 々な 表 示 が 出 ます 基 本 的 にはリターンキーを 押 せば OK ですが 以 下 に 示 すい くつかの 箇 所 では 定 められた 入 力 を 行 います PREFIXの 設 定 では 1.1 モジュールダウンロードディレクトリの 作 成 で 作 成 したディレ クトリ 名 を 使 用 します
Every Makefile.PL is run by perl in a separate process. Likewise we run 'make' and 'make install' in processes. If you have any parameters (e.g. PREFIX, LIB, UNINST or the like) you want to pass to the calls, please specify them here. If you don't understand this question, just press ENTER. Parameters for the 'perl Makefile.PL' command? Typical frequently used settings: PREFIX=~/perl non-root users (please see manual for more hints) Your choice: [] PREFIX=~/Library/Perl5/ LIB=~/Library/Perl5/lib INSTALLMAN1DIR=~/Library/Perl5/man1 INSTALLMAN3DIR=~/Library/Perl5/man3 これを 入 力 改 行 はここだけ 地 域 の 問 い 合 わせには(2) Asia を 選 びます First, pick a nearby continent and country (you can pick several of each, separated by spaces, or none if you just want to keep your existing selections). Then, you will be presented with a list of URLs of CPAN mirrors in the countries you selected, along with previously selected URLs. Select some of those URLs, or just keep the old list. Finally, you will be prompted for any extra URLs -- file:, ftp:, or http: -- that host a CPAN mirror. (1) Africa (2) Asia (3) Central America (4) Europe (5) North America (6) Oceania (7) South America Select your continent (or several nearby continents) [] 2 入 力 国 は(5) Japan を 選 びます Sorry! since you don't have any existing picks, you must make a
geographic selection. (1) China (2) Hong Kong (3) Indonesia (4) Israel (5) Japan (6) Republic of Korea (7) Russia (8) Singapore (9) Taiwan (10) Thailand (11) Turkey (12) Viet Nam Select your country (or several nearby countries) [] 5 さらに ftp サイトをいくつか 選 びます Sorry! since you don't have any existing picks, you must make a geographic selection. (1) ftp://ftp.dti.ad.jp/pub/lang/cpan/ (2) ftp://ftp.jaist.ac.jp/pub/cpan/ (3) ftp://ftp.kddilabs.jp/cpan/ (4) ftp://ftp.nara.wide.ad.jp/pub/cpan/ (5) ftp://ftp.riken.jp/lang/cpan/ (6) ftp://ftp.ring.gr.jp/pub/lang/perl/cpan/ (7) ftp://ftp.u-aizu.ac.jp/pub/cpan (8) ftp://ftp.yz.yamagata-u.ac.jp/pub/lang/cpan/ Select as many URLs as you like (by number), put them on one line, separated by blanks, e.g. '1 4 5' [] 1 2 3 4 5 6 7 8 これを 入 力 以 下 のように CPAN のコマンドプロンプトに 戻 ったら cpan> o conf commit として 設 定 を 保 存 し 最 後 に q と 入 力 してで 抜 けます cpan> q
1.4 環 境 変 数 の 設 定 環 境 変 数 PERL5LIB を Perl モジュールが 作 成 されるディレクトリ 名 にセットします setenv PERL5LIB ~/Library/Perl5/lib/ これは1.1 モジュールダウンロードディレクトリの 作 成 で 作 成 したディレクトリの 下 にな りますが 具 体 的 なディレクトリ 名 はシステムによって 異 なるので 一 度 2.2 CPANモジュ ールのインストール でCPANのモジュールをダウンロードして それがどこにダウンロー ドされたか 確 かめるといいでしょう なお echo setenv PERL5LIB ~/Library/Perl5/lib/ >> ~/.cshrc とすると 毎 回 シェルの 利 用 を 開 始 する 度 に 環 境 変 数 が 自 動 的 に 設 定 されます 上 記 環 境 変 数 の 設 定 は C シェルの 場 合 を 例 にしていますが bash などのシェルの 場 合 は echo export PERL5LIB=~/Library/Perl5/lib/ >> ~/.bashrc とします
2. CPAN の 利 用 それでは 初 期 設 定 ができたら 次 に CPAN に 登 録 されているモジュールを 使 用 する 方 法 を 紹 介 します 2.1 モジュールの 探 索 まず CPAN の 中 から 自 分 の 目 的 に 合 ったモジュールを 探 索 するところから 始 めます ここ では 与 えられた 要 素 の 集 合 に 関 して 全 ての 順 列 を 求 めるモジュールを 探 してみましょう つまり 例 えば ( a, b, c )が 与 えられた 場 合 ( a, b, c ), ( a, c, b ), ( b, a, c ), ( b, c, a ), ( c, a, b ), ( c, b, a )を 自 動 的 に 出 力 するようなモジュールを 探 します まずは http://search.cpan.orgをブラウズして 下 さい 順 列 は 英 語 で permutation なので テキスト 入 力 位 置 に permutation と 入 力 し 検 索 を 開 始 します すると 以 下 のようにいくつかの 候 補 が 出 てきます
黄 色 い がユーザによる 評 価 を 表 しています ここでは 評 価 が 比 較 的 高 い Math::Combinatorics を 見 てみましょう Math::Combinatorics の 文 字 をクリックすると このモジュールに 関 する 詳 しい 説 明 を 見 ることができます Synopsis( 概 要 )のところを 見 る と use Math::Combinatorics; my @n = qw(a b c); print "combinations of 2 from: ".join(" ",@n)." n"; print "------------------------".("--" x scalar(@n))." n"; print join(" n", map { join " ", @$_ } combine(2,@n))," n"; print " n"; print "permutations of 3 from: ".join(" ",@n)." n"; print "------------------------".("--" x scalar(@n))." n"; print join(" n", map { join " ", @$_ } permute(@n))," n"; Output: combinations of 2 from: a b c ------------------------------ a b a c b c
permutations of 3 from: a b c ------------------------------ a b c a c b b a c b c a c a b c b a とあります どうやら Math::Combinatorics に 含 まれる permute という 関 数 を 使 えば 目 的 のものが 得 られそうです そこで Math::Combinatorics の 中 の permute を 使 う 方 法 を 解 説 していきます 2.2 CPAN モジュールのインストール CPAN のモジュールをインストールするにはまず cpan と 入 力 して cpan を 起 動 した 後 install Math::Combinatorics とします インストールが 無 事 終 了 したら q で 抜 けましょう 以 下 インストール 中 の 画 面 の 一 例 です cpan> install Math::Combinatorics 入 力 CPAN: Storable loaded ok LWP not available CPAN: Net::FTP loaded ok : : モジュールがインストールされた 場 所 Installing /home/rsaito/cnsimac/library/perl5/lib/math/combinatorics.pm Installing /home/rsaito/cnsimac/library/perl5/man/man3/math::combinatorics.3pm Writing /home/rsaito/cnsimac/library/perl5/lib/darwin-thread-multi-2level/a uto/math/combinatorics/.packlist Appending installation info to /home/rsaito/cnsimac/library/perl5/lib/darwin-thread-multi-2level/p erllocal.pod /usr/bin/make install -- OK
cpan> q 入 力 2.3 CPAN モジュールの 使 用 それではインストールしたモジュールの 中 の 関 数 を 実 際 に Perl のプログラムから 使 用 して みましょう 以 下 のように use Math::Combinatorics;でモジュールの 使 用 宣 言 を 行 ってから そこに 含 まれる 関 数 permute を 使 用 します #!/usr/bin/env perl use Math::Combinatorics; my @test_array = ( a, b, c ); my @test_result = permute(@test_array); foreach my $per (@test_result){ print join(, @$per), n ; } 実 行 結 果 : a b c a c b b a c b c a c a b c b a