On Thu, Feb 28, 2002 at 01:21:23AM -0500, Chris Nandor wrote:
> I normally just roll my own.  It's quite simple code, once you know what
> you're doing, and really only a handful of lines.  Something like:
> 
> #!perl -w
> use strict;
> use File::Copy;
> use File::Find;
> use File::Path;
> 
> my $origpath = 'Bourque:Prog:MacPerl �:lib:auto:';
> my $destpath = 'Bourque:Desktop Folder:auto:';
> 
> find(\&mycopy, $origpath);
> 
> sub mycopy {
>       my($file) = $File::Find::name;
>       (my $dest = $file) =~ s/^\Q$origpath/$destpath/;
>       if (-d $file) {
>               mkpath $dest;
>       } elsif (-f $file) {
>               copy $file, $dest;
>       }
> }
> 
> __END__

If you wrote this as a reuseable subroutine, $origpath and $destpath would
vary between invocations.  To avoid the performance hit of recompiling the
regex each time, you could use eval like this:

my $orig2dest = eval {
        sub {
                my ($file) = @_;
                (my $dest = $file) =~ s/^\Q$origpath/$destpath/;
                return $dest;
        }
};

Then, replace the substitution line in the original code with:

my $dest = $orig2dest->($file);

Optimizing my code for brevity is left as an exercise.

Josh

Reply via email to