Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

system & shell metacharacters

by Anonymous Monk
on Jul 22, 2003 at 23:22 UTC ( #276954=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


I have a script that reads a tiny XML file into a variable. then calls another Perl program via system and attempts to give the XML to with a command line switch -d:

#!/usr/bin/perl -w open (XML, $somefile) || die "Could not open $somefile - $!"; my $xmldata; while (<XML>) { $xmldata .= $_; } close (XML); system(" -p 1234 -d $xmldata"); just has the following:
#!/usr/bin/perl -w use Getopt::Std; getopts('d:p:'); our $opt_d; our $opt_p; print $opt_p, "\n"; print $opt_d, "\n";
Unfortunately, the XML file (that reads) contains all kinds of yucky shell metacharacters such as quotes, newlines etc:
<?xml version="1.0"?> <data config="dev"> <SomeTag>data</SomeTag> <AnotherTag>data</AnotherTag> <SQL>select foo from bar where baz = 'something'</SQL> </data>
Only a portion of the xml data is actually making it into $opt_d in Do I have to escape every metacharacter in $xmldata prior to calling

Replies are listed 'Best First'.
Re: system & shell metacharacters
by Thelonius (Priest) on Jul 23, 2003 at 00:24 UTC
    Are you sure you want to pass XML data on the command line? Why not have read the XML data from stdin? Or, for that matter, why not just give the name of the file and have it read the XML file itself?
      I'm with you. I'd use something like
      open BAR, '|' || die qq(Useful msg: $!); print BAR $xmldata;
      and let read from STDIN. No shell at all. OP might want a glance at perlipc.
        open BAR, '|' || die qq(Useful msg: $!);

        ... is wrong, because it is interpreted as:

        open BAR, ('|' || die qq(Useful msg: $!));

        ... so remember to use "or die" rather than "|| die".

      Do not pass large amounts of data on the command line. The system only provides a certain amount of memory for that data and you can not exceed that amount.
Re: system & shell metacharacters
by bobn (Chaplain) on Jul 22, 2003 at 23:32 UTC

    try system(qw[ -p 1234 -d], $xmldata). Calling system() with a list takes the shell out of play.

    Not tested, but should work.

    --Bob Niederman,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://276954]
Approved by vek
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2016-10-24 01:52 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.