ローカル環境でperlモジュールを使う(Inline::Cを使うまで)

Inline::C というのはperlスクリプト上でC言語のコードを埋め込むことが
できるようになるモジュール。
C言語で書いた部分は初回時にコンパイルされ、二度目以降はコンパイルされたバイナリで
動作するため高速に実行することが可能になる。
速度を一部だけ上げたい場合には有効な手段だったりする。
(全部Cで書けよというのは言わない約束で)
あとC言語忘れかけたときなどに遊ぶのが主な使い方?(笑
 
The CPAN Search Site - search.cpan.org
http://search.cpan.org/
まずはここでInlineを検索。(Inline::CはInlineに入っています)
 
Inline - search.cpan.org
http://search.cpan.org/~sisyphus/Inline-0.45/Inline.pod
右側のアーカイブファイルをダウンロード
http://search.cpan.org/CPAN/authors/id/S/SI/SISYPHUS/Inline-0.45.tar.gz
 
ここからLinux側のコマンド

$ mkdir tmp
$ cd tmp
$ wget http://search.cpan.org/CPAN/authors/id/S/SI/SISYPHUS/Inline-0.45.tar.gz
--2009-10-17 21:55:07--  http://search.cpan.org/CPAN/authors/id/S/SI/SISYPHUS/Inline-0.45.tar.gz
search.cpan.org をDNSに問いあわせています... 64.235.248.44, 207.115.101.144
search.cpan.org|64.235.248.44|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://ftp.riken.jp/lang/CPAN/authors/id/S/SI/SISYPHUS/Inline-0.45.tar.gz [続く]
--2009-10-17 21:55:07--  http://ftp.riken.jp/lang/CPAN/authors/id/S/SI/SISYPHUS/Inline-0.45.tar.gz
ftp.riken.jp をDNSに問いあわせています... 134.160.38.1
ftp.riken.jp|134.160.38.1|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 93844 (92K) [application/x-gzip]
`Inline-0.45.tar.gz.1' に保存中

100%[================================================>] 93,844      --.-K/s   時間 0.09s

2009-10-17 21:55:08 (1.02 MB/s) - `Inline-0.45.tar.gz.1' へ保存完了 [93844/93844]
$ mkdir ~/perl
$ ls
Inline-0.45.tar.gz
$ tar xzf Inline-0.45.tar.gz
$ ls
Inline-0.45  Inline-0.45.tar.gz
$ cd Inline-0.45
$ ls
C               Inline-FAQ.pod      Inline.pod   README  modules
Changes         Inline-Support.pod  MANIFEST     ToDo    symbols.perl
Inline-API.pod  Inline.pm           Makefile.PL  lib     t
$ perl Makefile.PL PREFIX=~/perl
Checking if your kit is complete...
Looks good
Warning: prerequisite Parse::RecDescent 1.8 not found.

Inline::C is packaged with Inline.pm because it is the most commonly used
Inline Language Support Module (ILSM).

See also: Inline::ASM, ::Awk, ::BC, ::Basic, ::Befunge, ::CPP (C++), ::CPR,
          ::Foo, ::Guile, ::Java, ::Octave, ::PERL, ::Python, ::Ruby, ::TT,
          ::Tcl and ::WebChat.

Config.pm indicates that your version of Perl was built with this C compiler:

    cc

I have located this compiler on your system:

Do you want to install Inline::C? [y]y
Writing Makefile for Inline::C
Writing Makefile for Inline
$ make
cp Inline.pod blib/lib/Inline.pod
cp lib/Inline/MakeMaker/Changes blib/lib/Inline/MakeMaker/Changes
cp lib/Inline/denter.pm blib/lib/Inline/denter.pm
AutoSplitting blib/lib/Inline/denter.pm (blib/lib/auto/Inline/denter)
cp Inline.pm blib/lib/Inline.pm
AutoSplitting blib/lib/Inline.pm (blib/lib/auto/Inline)
cp lib/Inline/Foo.pm blib/lib/Inline/Foo.pm
cp Inline-API.pod blib/lib/Inline-API.pod
cp lib/Inline/MakeMaker.pm blib/lib/Inline/MakeMaker.pm
cp Inline-FAQ.pod blib/lib/Inline-FAQ.pod
cp Inline-Support.pod blib/lib/Inline-Support.pod
make[1]: ディレクトリ `/tmp/hato/Inline-0.45/C' に入ります
cp lib/Inline/C/ParseRegExp.pm ../blib/lib/Inline/C/ParseRegExp.pm
cp C-Cookbook.pod ../blib/lib/Inline/C-Cookbook.pod
cp C.pm ../blib/lib/Inline/C.pm
cp C.pod ../blib/lib/Inline/C.pod
cp lib/Inline/C/ParseRecDescent.pm ../blib/lib/Inline/C/ParseRecDescent.pm
Manifying ../blib/man3/Inline::C::ParseRegExp.3pm
Manifying ../blib/man3/Inline::C-Cookbook.3pm
Manifying ../blib/man3/Inline::C::ParseRecDescent.3pm
Manifying ../blib/man3/Inline::C.3pm
make[1]: ディレクトリ `/tmp/hato/Inline-0.45/C' から出ます
Manifying blib/man3/Inline.3pm
Manifying blib/man3/Inline-API.3pm
Manifying blib/man3/Inline-FAQ.3pm
Manifying blib/man3/Inline-Support.3pm
$ make test
 ------
        This innvocation of Inline requires the Parse::RecDescent module.
Can't locate Parse/RecDescent.pm in @INC (@INC contains: /tmp/hato/Inline-0.45/C/_Inline_test/lib ../blib/lib blib/lib /tmp/hato/Inline-0.45/C/../blib/lib /tmp/hato/Inline-0.45/C/../blib/arch /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ../blib/lib/Inline/C/ParseRecDescent.pm line 14.
 --- 略 ---
Result: FAIL
Failed 4/8 test programs. 0/8 subtests failed.
make[1]: *** [test_dynamic] エラー 255
make[1]: ディレクトリ `/tmp/hato/Inline-0.45/C' から出ます
make: *** [subdirs-test] エラー 2
 --- Perse::RecDescent というモジュールに依存しているらしいのでこれもDLして入れよう
$ pwd
/home/hato/tmp/Inline-0.45
$ cd ..
$ pwd
/home/hato/tmp
 --- さっきと同じようにPerse::RecDescentを検索
$ wget http://search.cpan.org/CPAN/authors/id/D/DC/DCONWAY/Parse-RecDescent-1.962.2.tar.gz
--2009-10-17 22:05:39--  http://search.cpan.org/CPAN/authors/id/D/DC/DCONWAY/Parse-RecDescent-1.962.2.tar.gz
search.cpan.org をDNSに問いあわせています... 64.235.248.44, 207.115.101.144
search.cpan.org|64.235.248.44|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://ftp.jaist.ac.jp/pub/CPAN/authors/id/D/DC/DCONWAY/Parse-RecDescent-1.962.2.tar.gz [続く]
--2009-10-17 22:05:40--  http://ftp.jaist.ac.jp/pub/CPAN/authors/id/D/DC/DCONWAY/Parse-RecDescent-1.962.2.tar.gz
ftp.jaist.ac.jp をDNSに問いあわせています... 150.65.7.130
ftp.jaist.ac.jp|150.65.7.130|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 153799 (150K) [application/x-tar]
`Parse-RecDescent-1.962.2.tar.gz' に保存中

100%[================================================>] 153,799     --.-K/s   時間 0.1s

2009-10-17 22:05:40 (1.06 MB/s) - `Parse-RecDescent-1.962.2.tar.gz' へ保存完了 [153799/153799]
$ ls
Inline-0.45  Inline-0.45.tar.gz  Parse-RecDescent-1.962.2.tar.gz
$ tar xzf Parse-RecDescent-1.962.2.tar.gz
$ cd Parse-RecDescent-1.962.2/
$ perl Makefile.PL PREFIX=~/perl
Checking if your kit is complete...
Looks good
Writing Makefile for Parse::RecDescent
$ make
cp lib/Parse/RecDescent.pm blib/lib/Parse/RecDescent.pm
Manifying blib/man3/Parse::RecDescent.3pm
$ make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/00.load.t ............... 1/1 # Testing Parse::RecDescent 1.962.2
t/00.load.t ............... ok
t/basics.t ................ ok
t/pod.t ................... skipped: Test::Pod 1.14 required for testing POD
t/re_capture_return.t ..... ok
t/separated_repetition.t .. ok
All tests successful.
Files=5, Tests=24,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.30 cusr  0.03 csys =  0.35 CPU)
Result: PASS
$ make install
Installing /home/hato/perl/share/perl/5.10.0/Parse/RecDescent.pm
Installing /home/hato/perl/man/man3/Parse::RecDescent.3pm
Appending installation info to /home/hato/perl/lib/perl/5.10.0/perllocal.pod
$ pwd
/home/hato/tmp/Parse-RecDescent-1.962.2
$ cd ../Inline-0.45/
$ make test
 --- 略 ---
        This innvocation of Inline requires the Parse::RecDescent module.
Can't locate Parse/RecDescent.pm in @INC (@INC contains: /tmp/hato/Inline-0.45/C/_Inline_test/lib ../blib/lib blib/lib /tmp/hato/Inline-0.45/C/../blib/lib /tmp/hato/Inline-0.45/C/../blib/arch /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ../blib/lib/Inline/C/ParseRecDescent.pm line 14.
 --- ~/perl にperl的にパスが通っていないからなのでパスを通す ---
$ find ~/perl/
/home/hato/perl/
/home/hato/perl/share
/home/hato/perl/share/perl
/home/hato/perl/share/perl/5.10.0
/home/hato/perl/share/perl/5.10.0/Parse
/home/hato/perl/share/perl/5.10.0/Parse/RecDescent.pm
/home/hato/perl/lib
/home/hato/perl/lib/perl
/home/hato/perl/lib/perl/5.10.0
/home/hato/perl/lib/perl/5.10.0/perllocal.pod
/home/hato/perl/lib/perl/5.10.0/auto
/home/hato/perl/lib/perl/5.10.0/auto/Parse
/home/hato/perl/lib/perl/5.10.0/auto/Parse/RecDescent
/home/hato/perl/lib/perl/5.10.0/auto/Parse/RecDescent/.packlist
/home/hato/perl/man
/home/hato/perl/man/man3
/home/hato/perl/man/man3/Parse::RecDescent.3pm
$ echo "export PERL5LIB=/home/hato/perl/share/perl/5.10.0" >> ~/.bashrc
$ source ~/.bashrc
$ make test
 --- 略 ---
Result: PASS
make[1]: ディレクトリ `/tmp/hato/Inline-0.45/C' から出ます
$ perldoc Inline::C
 --- ↑が見れれば成功 ---
$ perldoc Inline::C-Cookbook
$ cd ..
$ 
$ cat > a.pl
use Inline C => <<'END_C';

void greet() {
   printf("Hello, world\n");
}
END_C

greet;
 --- ここで Ctrl+D
$ perl a.pl
Hello, world

 
キモは2点
環境変数PERL5LIBを適切に設定すること。
perl Makefile.PL に PREFIXをつけてインストール場所を指定すること。
以上!

※ubuntu9.04の当方の環境ではParse::RecDescentだけだったが
 centosのほかの環境ならばversion.pm も入れないとダメだった。