Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set file permissions from Perl?

Tags:

perl

I'm writing a Perl script that generates a Bash script. I'm using open() with a mode of > to output everything to a new file. Standard stuff:

open (FILEOUT, ">", "rename.sh") or die "Can't create rename.sh";

The resultant .sh file is read only, with an octal value of 444. In perldoc it says I can add a + to the > (open (FILEOUT, "+>", "rename.sh")) to make the newly created file readable and writable, or 666.

Is there a way to make the new file executable (755 or anything else) using open()? If not, what's the best way to set file permissions for the new file?

like image 625
Andrew Avatar asked Feb 22 '10 12:02

Andrew


1 Answers

You will want to chmod the file like this.

chmod 0755, $filename;
#or
chmod 0755, $fh;

Alternatively, if you use sysopen and set the umask appropriately, you can do without chmod.

like image 166
Leon Timmermans Avatar answered Oct 04 '22 22:10

Leon Timmermans