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

Hi, I have a script which writes text files like this.

open (FILEHANDLE, ">>$filepath")||&ErrorMessage;
print FILEHANDLE "$pageoutput";
close FILEHANDLE;

This prints a string that I've stuffed into $pageoutput.

What I need to know is how can I set the permissions I need at the same time.

Can anyone help?

Best wishes

Sid
  • Comment on Setting permissions as text file is created

Replies are listed 'Best First'.
Re: Setting permissions as text file is created
by Tomte (Priest) on Mar 17, 2004 at 10:43 UTC

    If you mean "at file creation-time" with "at the same time", set the umask of the executing user to your liking.

    otherwise, if modifying the permissions after file-creation is sufficient, have a look at chmod

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: Setting permissions as text file is created
by bart (Canon) on Mar 17, 2004 at 10:44 UTC
      Hi, This place is great. Thanks for the prompt replies.

      So my code becomes:

      open (FILEHANDLE, ">>$filepath")||&ErrorMessage;
      print FILEHANDLE "$pageoutput";
      close FILEHANDLE;
      chmod 0666, $filepath;

      Intuitively I think that is what I would have guessed but I was hoping someone would suggest a way of ammending my code in a way that included permissions in the open filehandle line.

      thanks again.

      Best wishes

      Sid
        Intuitively I think that is what I would have guessed but I was hoping someone would suggest a way of ammending my code in a way that included permissions in the open filehandle line.

        Then, like the other poster suggested, play with umask. On my system, its default value is 2, creating plain files with permissions 0664. If you were to (temporarily) set it to 0, the file created should have permissions set to 0666. It does for me.

        my $umask = umask 0; open (FILEHANDLE, ">>$filepath")||&ErrorMessage; umask $umask; print FILEHANDLE "$pageoutput"; close FILEHANDLE;
Re: Setting permissions as text file is created
by maa (Pilgrim) on Mar 17, 2004 at 10:48 UTC
    Hi,

    if you're on a *nix platform, check out umask.

    You can also use umask from Perl. perldoc -f umask

      if you're on a *nix
      Correction, if you're using perl, given you consult perlport and perl$youros
Re: Setting permissions as text file is created
by Fletch (Bishop) on Mar 17, 2004 at 12:58 UTC

    See also perldoc -f sysopen, or perldoc POSIX then search for open.

      Hi,

      So is this the correct syntax for sysopen to do the following. If filepath does not exist create a file with the name contained in the filepath with permissions 666, print some stuff to the file and then close it?

      sysopen (FILEHANDLE, ">>$filepath",O_CREAT, 0666)||&ErrorMessage;
      print FILEHANDLE "$pageoutput";
      close FILEHANDLE;

      Thanks

      Sid
        A couple of points:
        1) chmod and the sysopen* call above set the file mode not the permission
        2) A file mode of 0666 is generally bad as it is generally allows more access to the file than is needed (do you really want everyone to be able to read/change/delete your file?)
        3) If you need to modify owner or the group membership, use chown

        CC

        Updated: Ok, you can't actually delete the file unless you have write access to the directory whihch holds the file. You can, however, blank out the file. For example:

        > my.file
        will reduce the file to zero bytes without actually having write access to the directory. The file isn't deleted but it certainly isn't very useful now.

        * if your umask is sane (ie 022) then the sysopen (FILEHANDLE, ">>$filepath",O_CREAT, 0666) will not be inherently insecure.

        What happens when you try it?

        (And you probably should also read perldoc perlopentut which gives examples of correct use of sysopen().)