kennethsmithesq has asked for the wisdom of the Perl Monks concerning the following question:

I know how to create new files through PERL (and know the security risks), but can't for the life of me work out how to set file permissions whilst I am doing so. I have racked my brains, other people's scripts and web site Q & As and am tearing my hair out. The code I am using to create a new file is... how do I incorporate the chmod.
open (INDEX,">../../directory/$filename.htm") || &error_html("Could no +t page: $!"); print INDEX qq~ ~; close (INDEX);

Added code tags - 2002-01-05 by dvergin

Replies are listed 'Best First'.
Re: Creating / Chmoding new files
by Vavoom (Scribe) on Jan 06, 2002 at 02:43 UTC
    The simplest approach I know would be to use the builting chmod command as added to your code below:
    open (INDEX,">../../directory/$filename.htm") || &error_html("Could not page: $!"); chmod 0400, "../../directory/$filename.htm"; print INDEX qq~ ~; close (INDEX);
    Hope that helps.

    Vavoom
Re: Creating / Chmoding new files
by particle (Vicar) on Jan 06, 2002 at 08:36 UTC
    umask can be used to restrict file permissions within a script. i often find umask useful when creating temp files inside a script running in setuid mode as root.

    it won't allow full control of file permissions, but i think it's a good solution to the situation i've mentioned above.

    ~Particle

Re: Creating / Chmoding new files
by grep (Monsignor) on Jan 06, 2002 at 04:39 UTC
    You can also set the permissions when you open the file for writing if you use sysopen.
    sysopen FILEHANDLE,FILENAME,MODE,PERMS

    You can look at the docs on perldoc.com (for some reason the sysopen() docs are not in the Monastary).

    grep
    grep> cd pub grep> more beer