- @INCをスクリプトで調整し、任意のディレクトリの.pmファイルをuseできるようにしてみる。
- <= Perl 5.8
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
# adjustment for include path (terminate with path separator)
BEGIN {
my (undef, $__base_dir, undef) = fileparse($0); # File::Basename
push(@INC, $__base_dir);
}
use test::Test1;
print $test::Test1::msg, "\n";
package test::Test1;
use strict;
use warnings;
our $msg = "Hello, test::Test1!";
$ perl ./manip_inc01.pl
Hello, test::Test1!
- ポイント:File::Basename::fileparseで取得できる、スクリプトファイル自身($0)のディレクトリを、BEGINブロックで@INCにpushしている。
- PHPの擬似コードだと次の様になる。
<?php
$__basedir = realpath(dirname(__FILE__));
set_ini("include_path", get_ini("include_path").PATH_SEPARATOR.$__basedir);
...
- ちなみに、デフォルトでは"."が最初から@INCに入っている場合もあるので、適宜利用し、かつ混乱しないこと。
プレーンテキスト形式でダウンロード