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

Editing multiple xml files in perl with help of Getopt::Long and Globbing

by chidori (Novice)
on Nov 30, 2013 at 07:50 UTC ( #1065038=perlquestion: print w/replies, xml ) Need Help??
chidori has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using a perl script edit certain xml tags. Previously i was picking up the xml files one by one and running the code. But I thought it would be easy if i can edit all xml files in a single go.

Below is the part of my code
#!/usr/bin/perl use Getopt::Long; use XML::Twig; foreach my $xml(@file) { $twig->parsefile_inplace($xml); } sub getoption{ GetOptions ("file=s" => \$data,) or die("Error in command line argumen +ts\n"); @file=glob($data); #globbing to pick '*.xml' }
The code seems to work. But i would like to know if there is a better way to do and also if there is any pitfall the way i do it now.

Script calls

./script -f config.xml ./script -f '*.xml'

Replies are listed 'Best First'.
Re: Editing multiple xml files in perl with help of Getopt::Long and Globbing
by hippo (Abbot) on Nov 30, 2013 at 11:08 UTC

    Unless you are doing something else with other options to the script which you aren't showing us, I don't see any reason for you to do the globbing in the script. Why not just use the shell's globbing instead? If you invoke the script with:

    ./script *.xml

    and handle this in your code with:

    while (my $file = shift @ARGV) { next unless -r $file; # Process the file here }

    would that not achieve the end result?

      Shell globbing isn't universal or portable. So in many cases, it's appropriate to expand globs within the Perl script using File::Glob or other mechanism. For example, in the Microsoft Windows shell (Command Prompt), wildcards are not as powerful or as useful as BSD-style globs; in particular, they can't be used to expand directories (folder), which makes them generally useless for my purposes. I invariably need my Perl scripts to permit their users to glob multiple folder names.

      Sorry , didn't mention in my intial post. Yes, i have other options that will get the date and another option to print the xml content. Thats why i didn't do shell globbing.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1065038]
Approved by boftx
help
Chatterbox?
[ww]: er -- senior cognitive deficiency: ((22+(22*2))
[ww]: (but pls don't s/senior/senile/ ... not yet
[karlgoethebier]: A bit more audience now: The Impact of Information Sources on Code Security Very interesting IMHO...
[karlgoethebier]: ..it's about copy & paste ;-)
[ww]: Wonder what IEEE would have thought w/survey based on PM/perl
[ww]: s/thought/found/
[erix]: that title is a broken* sentence, no? But maybe that's deliberate
[karlgoethebier]: ww:My first thought, to be honest...
[erix]: and those authors' names don't look like native english speakers. Then again, neither am I :)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-09-25 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (280 votes). Check out past polls.

    Notices?