Perl chmod函数


学习Perl语言编程中……,打算边学边把Perl函数整理出来。

介绍:

chmod函数改变一列文件的权限。列表的第一个元素必须是数字模式。chmod函数返回成功改变了的文件的数目。如:

$cnt = chmod 0755, 'file1', 'file2';

会把$cnt设置为0,1,或2,具体多少取决于改变的文件的数目。

用法:

chmod LIST

例子:

 

   $cnt = chmod 0755, 'foo', 'bar';
    chmod 0755, @executables;

    $mode = '0644'; chmod $mode, 'foo';      
    # !!! sets mode to --w----r-T
    # 数字模式不能用引号!看起来是八进制数字的原因。

    $mode = '0644'; chmod oct($mode), 'foo'; # 用oct函数把八进制转换成一个数字
    $mode = 0644;   chmod $mode, 'foo';      # 不用引号就最好了!

如果返回错误,意味着你没有足够的权限修改文件模式。你可能既不是文件的所有者,也不是超级用户。用$!看看失败的实际原因是什么。

下面是典型的用法:

chmod(0755, @executables) == @executables
     or die "couldn't chmod some of @executables:$!";
如果你想知道是哪个文件不允许这样修改,使用如下所示的代码:

@cannot = grep {not chmod 0755, $_} 'file1', 'file2', 'file3';
die "$0:could not chmod @cannot\n" if @cannot;

此处使用grep函数选择列表里那些chmod函数对之操作失败的元素。


一条回应:“Perl chmod函数”

  1. After exploring a few of the blog articles on
    your site, I really like your technique of blogging.
    I saved as a favorite it to my bookmark site list and will be checking back soon.
    Take a look at my web site as well and let me know how you feel.