[Perl] Two Filesystem Questions

Offer Kaye oferk at oren.co.il
Thu Jan 16 00:38:52 PST 2003

> 1. How do I copy a file while preserving all of its permissions, portably?
> (i.e: it will work on VMS and NT as well).
> File::Copy does not seem to do that with syscopy() or copy();

This might work (taken from

use warnings;
use File::Copy;
use strict;

my $mode = (stat $ARGV[0])[2]; #decimal unmasked
my $mode = sprintf("%04o", $mode & 07777) ; #octal masked off
print $mode,"\n";
copy( "$ARGV[0]", "$ARGV[0]\.bak" );
chmod (oct($mode), "$ARGV[0]\.bak");
print "Backup completed.\n";
exit 0;

The conversion to octal above is not strictly necessary- see the comments by
Randal L. Schwartz:

> 2. How do I delete an entire directory tree portably? (without starting to
> hack away on one of those File::Finders.)

Use the "rmdir" function from the File::Path module:

> Regards,
> 	Shlomi Fish

Hope this helps :-)
Offer Kaye

More information about the Perl mailing list