How do you use file permission handling (umask) with a short example?

File permission handling in Perl can be managed using umask, which sets the default file permissions for newly created files. The umask value specifies which permission bits will not be set on newly created files and directories. A common use of umask is to restrict access for security reasons.

Below is a short example of how to use umask in Perl:

#!/usr/bin/perl use strict; use warnings; # Set umask to 027 - Owner can read/write, Group can read, Others have no access umask(027); # Create a new file with default permissions open(my $fh, '>', 'example.txt') or die "Could not open file: $!"; print $fh "This file has restricted permissions based on umask.\n"; close($fh); print "File created with restricted permissions.\n";

umask Perl file permissions security programming