[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
http://archive.develooper.com/beginners@perl.org/msg20814.html):

#!/usr/bin/perl
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:
http://archive.develooper.com/beginners@perl.org/msg20815.html


> 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:
http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Path.pm

> Regards,
>
> 	Shlomi Fish
>

Hope this helps :-)
Offer Kaye




More information about the Perl mailing list