Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Making sense of File::Find

by zentara (Archbishop)
on Sep 26, 2014 at 18:23 UTC ( [id://1102168]=note: print w/replies, xml ) Need Help??


in reply to Making sense of File::Find

#!/usr/bin/perl # Recursively searchs down thru directories replacing patterns in file +s. # usage zsr 'search' 'replace' 'ext'(optional with no .) # use '' for null string in $replace # ex: zsr 'type1' 'series A' use warnings; use strict; use File::Find; my ($search,$replace,$ext) = @ARGV; if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'}; die "Usage : zsr 'search' 'replace' 'extension' (extension optional)\n +" if ($search eq ""); find (\&wanted, "."); sub wanted { my $open = $_; my $tempfile = 0; if (!($open =~ /$ext$/i) or (-d||-B||-l)) {return} print $open,"\n"; my $mode = (stat $open)[2]; #print $mode,"\n"; #printf "Permissions are %04o\n", $mode & 07777; open (TEMP,">> $tempfile"); open (FH, "< $open") or die "Can't open $open: $!\n"; while (<FH>) { $_ =~ s/$search/$replace/g; print TEMP $_; } close FH; close TEMP; rename ($tempfile, $open) or die "Can't rename $open: $!\n"; chmod ($mode,$open) or die "Can't restore permissions to $open, possib +ly wrong owner: $!\n"; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1102168]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found