Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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:

All,

I have a script foo.pl that reads a tiny XML file into a variable. foo.pl then calls another Perl program bar.pl via system and attempts to give the XML to bar.pl 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("bar.pl -p 1234 -d $xmldata");
bar.pl 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 foo.pl 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 bar.pl. Do I have to escape every metacharacter in $xmldata prior to calling bar.pl?

Comment on system & shell metacharacters
Select or Download Code
Re: system & shell metacharacters
by bobn (Chaplain) on Jul 22, 2003 at 23:32 UTC

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

    Not tested, but should work.

    --Bob Niederman, http://bob-n.com
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 bar.pl read the XML data from stdin? Or, for that matter, why not just give bar.pl the name of the file and have it read the XML file itself?
      I'm with you. I'd use something like
      open BAR, '|bar.pl' || die qq(Useful msg: $!); print BAR $xmldata;
      and let bar.pl read from STDIN. No shell at all. OP might want a glance at perlipc.
        open BAR, '|bar.pl' || die qq(Useful msg: $!);

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

        open BAR, ('|bar.pl' || 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-11-27 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (188 votes), past polls