Path::Tiny
最近Perlを書くときにanatofuz.iconが使っているモジュール コアモジュールをつなぎ合わせることで便利なファイル操作が可能となっている
こんなときに便利
再帰的に走査して置換して回るとき
大量のファイルを操作するとき
だいたいSYNOPSISをみれば雰囲気がわかる
code:example.pl
use Path::Tiny;
# creating Path::Tiny objects
$dir = path("/tmp");
$foo = path("foo.txt");
$subdir = $dir->child("foo");
$bar = $subdir->child("bar.txt");
# stringifies as cleaned up path
$file = path("./foo.txt");
print $file; # "foo.txt"
# reading files
$guts = $file->slurp;
$guts = $file->slurp_utf8;
@lines = $file->lines;
@lines = $file->lines_utf8;
($head) = $file->lines( {count => 1} );
($tail) = $file->lines( {count => -1} );
# writing files
$bar->spew( @data );
$bar->spew_utf8( @data );
# reading directories
for ( $dir->children ) { ... }
$iter = $dir->iterator;
while ( my $next = $iter->() ) { ... }
ファイルを置換するときはeditとかedit_linesをつかう
utf8がついているのはPerlの内部文字列 -> utf8の変換時に利用される
インストール方法
cpanmを使う場合
$cpanm Path::Tiny
cpmを使う場合
$cpm install -g Path::Tiny