There are several options here. Pretty much any way you go about it, you'll need to get the existing file's permissions from stat, mentioned above. Setting your umask to those permissions is one way to go about it, but a umask is only a mask applied to newly created files, not an absolute setting of permissions. To set permissions, you could create the file first, then use chmod. Or you could use the built-in POSIX module and its creat() and open() modules, in which case you'd be working with file descriptors instead of file handles. The best way I can see, though, is through something like:
use POSIX; my $perms = (stat "file")[2]; sysopen(OUT, "newfile", O_CREAT | O_WRONLY, $perms) or die "$!\n"; print OUT "whatever\n"; close(OUT);
This lets you create a file with given permissions, and work with filehandles. The O_CREAT is needed to create the file if it doesn't already exist, and the O_WRONLY is needed to write to the file in case it doesn't have write perms (don't worry, if the file doesn't have write perms, O_WRONLY won't affect that. It'll write to the file and leave the perms untouched).

In reply to Re: Mode of created files by plaid
in thread Mode of created files by setantae

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.