in reply to Re^9: search and replace strings in different files in a directory (Path::Tiny)
in thread search and replace strings in different files in a directory
I'd like to thank Athanasius for picking up the slack :)
Why was the subroutine called with 6 passed as an argument. Was this just for demonstrating that not only strings but also numbers can be passed to a sub?.
:) To demonstrates that arguments can be passed and they end up in @_ ... so you can lookup @_ in perlvar and read about it :)
Its also so you can resolve the issue you had in Re^2: search and replace strings in different files in a directory (Path::Tiny), something like
#!/usr/bin/perl -- use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; Main( @ARGV ); exit( 0 ); sub Main { #~ my( $infile_paths ) = @_; my( $infile_paths ) = 'C:\temp\test_folder\paths.txt';; my @paths = GetPaths( $infile_paths ); for my $path ( @paths ){ FormerlyMainNowReplaceXmlKids( $path ); } } ## end sub Main sub GetPaths { my @paths = path( shift )->lines_utf8; s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub FormerlyMainNowReplaceXmlKids { #~ my( $directory ) = @_; ## same as shift @_ ## same as shift my $directory = shift; 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 ) { Replace( $file, "$file-$bak" ); } } ## end sub Main sub Replace { 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 Replace
Or (less complete but the sub names is the idea of this idea )
#!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; Main( @ARGV ); exit( 0 ); sub Main { my @dirs = DirsForDiddling( '.../paths.txt' ); DiddleXmlChildren( $_ ) for @dirs; } ## end sub Main sub DirsForDiddling { my( $dirfile ) = @_; my @dirs = path( $dirfile )->lines_utf8; s/\s+$// for @dirs; ## no trailing whitespace return @dirs; } ## end sub DirsForDiddling sub DiddleXmlChildren { my( $directory , $date, $bak ) = @_; $date ||= POSIX::strftime( '%Y-%m-%d', localtime ); $bak ||= "$date.bak"; my @xml_files = XmlChildren( $directory ); for my $file ( @xml_files ) { Diddle( $file, "$file-$bak" ); } } ## end sub DiddleXmlChildren sub XmlChildren { return path( shift )->children( qr/\.xml$/ ); } ## end sub XmlChildren
or more fun with names
sub Main { my @dirs = DirsForDonuts( '.../paths.txt' ); for my $dir( @dirs ){ DonutXmlChildren( $dir ); } } ## end sub Main sub DirsForDonuts { my( $dirfile ) = @_; my @dirs = path( $dirfile )->lines_utf8; s/\s+$// for @dirs; ## no trailing whitespace return @dirs; } ## end sub DirsForDonuts sub DonutXmlChildren { my( $directory , $date, $bak ) = @_; $date ||= POSIX::strftime( '%Y-%m-%d', localtime ); $bak ||= "$date.bak"; my @xml_files = XmlChildren( $directory ); for my $file ( @xml_files ) { Donut( $file, "$file-$bak" ); } } ## end sub DonutXmlChildren sub XmlChildren { return path( shift )->children( qr/\.xml$/ ); } ## end sub XmlChildren
The idea is to have many many many subroutines, and give them names that are memorable... I chose Diddle because RenameMe didn't fit for this problem, and I'm kinda tired of NotDemoMeaningfulName and NotMainMeaningfulNameYouPick is tad long :)
Athanasius is doing pretty well :)(thanks) and I'd like to add a little note on why Main/exit, its for ease of writing/debugging/maintaining/documenting the program. The program begins with Main( @ARGV ); and ends after Main completes .
In other languages like C main() is mandatory, but its not mandatory in perl, so perl will let you write interwoven stuff like
Sky( 6 ); my $confusion = 'uhoh'; sub Sky { ... } sub Fall { ... } Fall( 6666666666666 ); ... if( ... ){ ... } ...
and this is very hard to read and maintain, but folks still do it and often struggle debugging this stuff; Main/exit combination is much easier on the brain :)
You can read more about this Main/exit style convention /program template at (tye)Re: Stupid question (and see one discussion of that template at Re^2: RFC: Creating unicursal stars
If you've got more questions, I'll be back :)
|
|---|