The permissions on a file or directory define who (in broad categories) can do what (more or less) to that file or directory. Under UNIX, the typical way to change permissions on a file is with the chmod (1) command. (See its manpage if you are unfamiliar with its operation.) Similarly, Perl changes permissions with the chmod
function. This operator takes an octal numeric mode and a list of filenames, and attempts to alter the permissions of all the filenames to the indicated mode. To make the files fred and barney both read/write for everyone, for example, do something like this:
chmod(0666,"fred","barney");
Here, the value of 0666
happens to be read/write for user, group, and other, giving us the desired permission.
The return value of chmod
is the number of files successfully adjusted (even if the adjustment does nothing); so it works like unlink
, and you should treat it as such with regard to error checking. Here's how to change the permissions of fred and barney while checking the errors for each:
foreach $file ("fred","barney") { unless chmod (0666,$file) { warn "hmm... couldn't chmod $file: $!"; } }