How to change file permissions like chmod in Perl?
Question
How to change file permissions like chmod in Perl?
Changing file permissions in Perl is straightforward using the built-in chmod function, which works similarly to the Unix chmod command. The chmod function lets you modify the permission bits of files or directories directly from your Perl script.
Understanding chmod in Perl
The chmod function in Perl takes two arguments:
- A numeric mode (in octal) that specifies the permission bits you want to set
- A file (or list of files) whose permissions you want to change
It returns the number of files successfully modified.
For example, to set a file's permissions to rwxr-xr-- (octal 0754), you'd call:
chmod 0754, $filename;
Note the leading zero to indicate octal mode. This is important because file modes are specified in base 8, consistent with Unix conventions.
Perl-Specific Notes
- Sigils: The file name is usually a scalar, like
$filename. You can also pass a list of files. - TMTOWTDI: Perl’s philosophy ("There's More Than One Way To Do It") means you can combine
chmodwith other functions likestatto dynamically change bits. - Context: In scalar context,
chmodreturns the number of files modified. In list context, it returns the list of that value (rarely used). - Version compatibility:
chmodis a core function available in all Perl 5 versions.
Common pitfalls
- Always use an octal number for the mode, e.g.,
0755, not decimal755. - Windows filesystems do not always support Unix-style permissions;
chmodmay have limited effect there. - If your Perl script runs as a user without permission to change the file mode,
chmodwill fail silently unless checked.
Example: Changing file permissions in Perl
The following script creates a sample file, changes its permissions, and confirms the change by printing the new octal permissions.
use strict;
use warnings;
use Fcntl ':mode'; # For file mode constants
my $filename = "example.txt";
# Create the file (or truncate if exists)
open my $fh, '>', $filename or die "Cannot create $filename: $!";
print $fh "Hello, Perl chmod example\n";
close $fh;
print "Created $filename\n";
# Set permissions to rwxr-xr-- (0754)
my $mode = 0754;
my $result = chmod $mode, $filename;
if ($result == 1) {
print "Permissions changed successfully.\n";
} else {
warn "Failed to change permissions on $filename.\n";
}
# Verify and print the new permissions
my @stats = stat($filename);
if (@stats) {
my $perm_oct = sprintf "%04o", S_IMODE($stats[2]);
print "New permissions for $filename: $perm_oct\n";
} else {
warn "Could not stat $filename.\n";
}
How it works:
- Creates or overwrites
example.txtwith some content. - Calls
chmodwith octal0754, setting the file’s permissions accordingly. - Uses
statandS_IMODE(fromFcntl) to extract the permission bits. - Prints the permissions in four-digit octal form, showing the result.
Summary
Use the built-in chmod function with an octal mode and the file name(s) to change permissions in Perl. Remember the octal prefix, verify results with stat if needed, and be aware of OS limitations. This simple approach works well for Unix-style permission management in Perl scripts.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 7ms
(empty)(empty)