【Perl】Path::File 目录的创建和删除

 

1 Path::File中两个函数的基本使用

 

make_path( $dir1, $dir2, .... )
make_path( $dir1, $dir2, ...., \%opts ) #\%opts表示哈希引用

mkpath( $dir )
mkpath( $dir, $verbose, $mode )
mkpath( [$dir1, $dir2,...], $verbose, $mode )
mkpath( $dir1, $dir2,..., \%opt )

mode => $num 每个创建目录的权限位,默认为0777,可以通过umask来修改。Linux下
verbose => $bool 若为1则打印所有创建成功的目录。默认情况下为0
error => \$err 一个list引用,存储发生的任何错误信息。
owner => $onwer
user => $user
uid => $owner 用户id
group => $group


remove_tree( $dir1, $dir2, .... )
remove_tree( $dir1, $dir2, ...., \%opts )

rmtree( $dir )
rmtree( $dir, $verbose, $safe )
rmtree( [$dir1, $dir2,...], $verbose, $safe )
rmtree( $dir1, $dir2,..., \%opt )

verbose => $bool 是否打印成功删除的目录
safe => $bool 设为1时则删除时会跳过不能删除的目录

2 代码举例:

 

use File::Path;
rmtree('shan');

rmtree( 'foo/bar', 'bar/rat', {error => \my $err} );
if (@$err)
{
    for my $diag (@$err)
    {
        my ($file, $message) = %$diag;
        if ($file eq '')
        {
            print "general error: $message\n";
        } else
        {
            print "problem unlinking $file: $message\n";
        }
    }
} else
{
    print "No error encountered\n";
}

3 more help

perldoc Path::File

Author: visaya fan <visayafan[AT]gmail.com or visayafan[AT]163.com>

Date: 2011-10-29 15:52:00

HTML generated by org-mode 6.33x in emacs 23

posted @ 2011-10-29 15:53  visayafan  阅读(4353)  评论(0编辑  收藏  举报