2014年2月4日火曜日

FindBinモジュールの使い方

FindBinモジュールはスクリプトの実行ファイルやディレクトリを変数に格納するモジュール。
以下のコマンドを実行してみるとわかりやすいかと。

vim /hoge/test.pl
で以下のファイルを作成。
use FindBin;
print "Bin: $FindBin::Bin\n";
print "Script: $FindBin::Script\n";

コマンドを実行。
perl /hoge/test.pl

実行結果:
Bin: /hoge
Script: test.pl

cpanサイトはこれ。
http://search.cpan.org/~rjbs/perl-5.18.2/lib/FindBin.pm

Path::Classモジュールの使い方

Path::Classモジュールの使い方がよくわからなかったので調べていたら、CPANのSYNOPSISを見るのが一番わかりやすいという結論になった。
このモジュールはWindowsでもLinuxでもディレクトリやファイルのパス名をよしなに生成してくれるモジュールのようだ。
以下のワンライナーを実行してみればわかりやすいかと。

perl -MPath::Class -e '
my $dir  = dir('foo', 'bar');
my $file = file('bob', 'file.txt');
print "dir: $dir\n";
print "file: $file\n";
'
出力結果:
dir: foo/bar
file: bob/txt

これがwindows環境で実行すれば
dir: foo\bar
file: bob\txt
となるというのがこのモジュールを使うメリット。

cpanサイトはこれ。
http://search.cpan.org/~kwilliams/Path-Class-0.33/README.pod