perlbrewなperlで共通のパスを@INCに入れたい、もしくはperl実行時にごにょごにょしたい話
perlbrewでインストールしたperlたちの@INCは$PERLBREW_ROOT配下のディレクトリになります。おかげで互いに影響せずにモジュールを入れられるわけですが、システム全体で共通のモジュールも読ませたい局面もあります。
PERL5LIB(でパスを指定)やPERL5OPT(で-Mlib=/path/to)でもできますが、perl -MOrenoConf とした場合はまだ環境変数が評価される前なのでCan't locateと怒られてしまいます。
Ubuntuのsystem perlは /etc/perl が @INC に入っています。(確かGentooのもこんな感じだったと思います)
$ perl -V ... @INC: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .
今はちょっと違うかもしれませんが、以前確認したときは perl.c にパッチを当てて、
incpush ("/etc/perl", FALSE, FALSE, FALSE);
としていました。
これと同じ事をperlbrewでもやりたいと思ったのですが、パッチを当てるのはめんどいなぁ。。。と思いつつperlbrewのヘルプ(perlbrew help install)を眺めていたら
--sitecustomize $filename Specify a file to be installed as sitecustomize.pl
というのをみつけました。
長年Perlを触ってるのに初めて見たのですが、perldoc perlrunによれば、
Perl can be built so that it by default will try to execute
$Config{sitelib}/sitecustomize.pl at startup (in a BEGIN block).
This is a hook that allows the sysadmin to customize how perl
behaves. It can for instance be used to add entries to the @INC
array to make perl find modules in non-standard locations.
とのことで、$Config{sitelib}/sitecustomize.pl に置いたファイルがしょっぱなの方のBEGINブロックで評価されるようにできる、ただしperl本体のビルド時にその有効化の指定が必要、だそうです。
これが有効になっているかどうかは、perl -Vのconfig_argsを見れば確認できます。
$ perl -V | grep sitecustomize config_args='-de -Dprefix=... -Dusesitecustomize -Aeval:scriptdir=...
perlbrewでsitecustomizeを有効にしてビルドするには
perlbrew install --notest 5.16.1 --sitecustomize /usr/oreno/etc/perl/sitecustomize.pl
とします。
これで、/usr/oreno/etc/perl/sitecustomize.plが$Config{sitelib}/sitecustomize.plにデッドコピーされ、perl起動時に評価されるようになります。
$Config{sitelib}のパスは
$ perl -MConfig -e 'warn $Config{sitelib}'
で確認できます。
というわけで、$Config{sitelib}/sitecustomize.plに
unshift @INC, "/usr/oreno/etc/perl"; 1;
と書いておけば、/usr/oreno/etc/perl/OrenoConf.pm にあるモジュールも、perl -e 'use OrenoConf;' や perl -MOrenoConf で読めるようになります!
はっぴー!!