バージョン
- Path::Class 0.33
- Path::Tiny 0.056
オブジェクト生成
Path::Classで生成したオブジェクトは、ファイルはPath::Class::File、ディレクトリはPath::Class::Dirとなる。Path::TinyはどちらもPath::Tinyとなる。
# Path::Class $file = file($path); $dir = dir($path); # Path::Tiny $file = path($path); $dir = path($path);
ディレクトリ配下のファイルオブジェクト生成
# Path::Class $file = $dir->file($name); # Path::Tiny $file = $dir->child($name);
ディレクトリ配下のディレクトリオブジェクト生成
# Path::Class $dir = $dir->subdir($name); # Path::Tiny $dir = $dir->child($name);
ディレクトリ削除(配下ファイルごと)
# Path::Class $dir->rmtree; # Path::Tiny $dir->remove_tree;
ファイルオープン
# Path::Class $read = $file->openr; $read = $file->open('<:encoding(cp932)') or die "Can't read $file: $!"; $read = $file->open('<:utf8') or die "Can't read $file: $!"; # Path::Tiny $read = $file->openr; $read = $file->openr(':encoding(cp932)'); $read = $file->openr_utf8;
ファイルの内容を配列に読み込み
# Path::Class @lines = $file->slurp( chomp => 1 ); # Path::Tiny @lines = $file->lines( { chomp => 1 } );
ファイルにまとめて書き込み
# Path::Class $file->spew( iomode => ':utf8', $data ); # Path::Tiny $file->spew( { binmode => ':utf8' }, $data );
親ディレクトリ名でファイルを作成
# Path::Class file($file->parent)->touch; # Path::Tiny $file->parent->touch;
ファイルをコピー
# Path::Class $file->copy_to($path); # Path::Tiny $file->copy($path);
ファイルをリネーム
# Path::Class $file->move_to($file_path); # Path::Tiny $file->move($file_path);
ファイルを指定ディレクトリに移動
Path::Tinyのmoveはrenameと同等の機能なので注意が必要。
# Path::Class $file->move_to($dir_path); # Path::Tiny use File::Copy 'move'; move $file, $dir_path;
ディレクトリ配下のファイルを処理
# Path::Class $dir->recurse( callback => sub { my $file = shift; return unless -f $file; print $file, "\n"; }); # Path::Tiny my $iterator = $dir->iterator( { recurse => 1 } ); while( my $file = $iterator->() ) { next unless $file->is_file; print $file, "\n"; }