Hi Monks, I am writing a script that does the following:

1. traverses directory(ies), looking for a file called 'metadata.xml'

2. Looks for a tag called <merchant>

3. Replaces underscores with spaces in value assigned to <merchant> tag.

My script seems to work, but I could use help/tips on the following:

1. creating a backup file of any file that I alter.

2. the directories I look at are originally in zip format. Right now I manually unzip before updating, and then rezip. I'd like to implement a process that unzips the original zip, traverses the directories and modifies the metadata.xml files, rezips the files, and removes any clutter.

3. Any tips on making the code more efficient, elegant, and less redundant..

Thanks!

#!/usr/bin/perl use strict; use warnings; use File::Find; @ARGV = ('.') unless @ARGV; my $dir = shift @ARGV; find(\&edits, $dir); sub edits() { my $seen = 0; my $file = $_; if ($file eq 'metadata.xml') { open (my $file_fh, $file) || die "Can't open $file!\n $!"; my @lines = <$file_fh>; close $file_fh; open $file_fh, ">$file"; foreach my $line ( @lines ) { if ($line =~/merchant/) { $line =~s/_/ /g; } print $file_fh $line; $seen++; } close $file_fh; } print "Updated $File::Find::name\n" if $seen > 0; }

In reply to Recursive editing of a single xml tag.. by bryank

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.