in reply to Can anyone help me to improve this code, please?
The wanted() sub gets called every time a file/dir is found. Thus the map gets run every time. This is not efficient.
The typical perl idiom for doing this sort of thing involves making an RE that uses alternation operatior | as in m/this|that|other|thing/. We can automatically generate the RE from your array and also use qr// to compile it.
++ for using strict, warnings and modules BTW.
#!perl use strict; use warnings; use File::Find; my @ext = qw(.tmp .bak .$$$ .chk .old .gid .fts .ftg .log .dmp .--- .prv .temp); # build and compile an RE that uses alternation operator | my $re = join '|', map{ quotemeta } @ext; # compile RE. ?: avoids capture into $1 from ( ) # and the $ locks the matched ext to end of filename $re = qr/(?:$re)$/; sub wanted { return unless -f $_; return unless m/$re/; warn "Unlinking $File::Find::name\n"; unlink $_ or warn "\tUnlink $_ failed $!\n"; } find(\&wanted, '/');
Typically you want to do a dummy run without unlinking. Trust me on this ;-)
Update Fixed typo, thanks hv
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can anyone help me to improve this code, please?
by ihb (Deacon) on Jun 23, 2004 at 01:55 UTC |