chmod LIST

變更檔案清單的權限。清單的第一個元素必須是數字模式,它應該是八進位數字,而且絕對不能是八進位數字字串:0644 可以,但 "0644" 不行。傳回成功變更的檔案數量。如果你只有字串,請參閱 oct

my $cnt = chmod 0755, "foo", "bar";
chmod 0755, @executables;
my $mode = "0644"; chmod $mode, "foo";      # !!! sets mode to
                                            # --w----r-T
my $mode = "0644"; chmod oct($mode), "foo"; # this is better
my $mode = 0644;   chmod $mode, "foo";      # this is best

在支援 fchmod(2) 的系統上,你可以在檔案中傳遞檔案代號。在不支援 fchmod(2) 的系統上,傳遞檔案代號會引發例外。檔案代號必須傳遞為 glob 或 glob 參照才能被辨識;裸字詞會被視為檔案名稱。

open(my $fh, "<", "foo");
my $perm = (stat $fh)[2] & 07777;
chmod($perm | 0600, $fh);

你也可以從 Fcntl 模組匯入符號 S_I* 常數

use Fcntl qw( :mode );
chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
# Identical to the chmod 0755 of the example above.

移植性問題:"chmod" in perlport