in reply to search and replace strings in different files in a directory
You've fallen for the basic readdir trap
Also, you're not using $^I correctly (you really don't want to use it anyway)
Make your life easier by using Path::Tiny, improve this program
#!/usr/bin/perl -- ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END if " -otr -opr - +ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; Main( @ARGV ); exit( 0 ); sub Main { my $date = POSIX::strftime( '%Y-%m-%d', localtime ); my $bak = "$date.bak"; my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Diddle( $file, "$file-$bak" ); } } ## end sub Main sub Diddle { my( $in, $bak ) = @_; path( $in )->move( $bak ); my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&}g; ## will match more than what you want fix it s{&amp;}{&}g; s{\s>\s}{>}g; s{\s<\s}{<}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Diddle
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search and replace strings in different files in a directory (Path::Tiny)
by PitifulProgrammer (Acolyte) on Aug 18, 2014 at 10:49 UTC | |
by Anonymous Monk on Aug 19, 2014 at 09:49 UTC | |
by PitifulProgrammer (Acolyte) on Aug 19, 2014 at 13:51 UTC | |
by Anonymous Monk on Aug 19, 2014 at 23:40 UTC | |
by PitifulProgrammer (Acolyte) on Aug 20, 2014 at 08:57 UTC | |
|