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{&}{&amp;}g; ## will match more than what you want fix it s{&amp;amp;}{&amp;}g; s{\s>\s}{&gt;}g; s{\s<\s}{&lt;}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 :)


In reply to Re^10: search and replace strings in different files in a directory by Anonymous Monk
in thread search and replace strings in different files in a directory by PitifulProgrammer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.