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).