Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

system() and qx comparison

by didess (Sexton)
on Sep 27, 2011 at 21:55 UTC ( #928192=perlquestion: print w/ replies, xml ) Need Help??
didess has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks! I know there are two forms for submitting a command to system :

One through the shell with the command and its parameters in one single scalar system('echo $LANG'); for example.

One without the shell, with the command and its parameters in a list. like system('echo','$LANG');

With this second form, the command echo will print '$LANG' without any resolution of the variable LANG.

Can I get these two different ways , and how if yes, with the qx syntax : qx {echo $LANG} ?

Thanks to you!

Comment on system() and qx comparison
Select or Download Code
Re: system() and qx comparison
by Kc12349 (Monk) on Sep 27, 2011 at 22:00 UTC

    Remove the single quotes from around $LANG in your second form or replace them with double quotes.

    qx is another form for backticks, which in perl will capture the return from the system call.

    my $return = qx{echo $LANG}; say $return;
      thanks for your answer;

      but, if replace single quotes by doubles quotes with qx, $LANG will be resolved as a Perl variable, not as a shell environment variable

      This little code will show it clearly:
      cat essaiQX.pl #!/usr/bin/perl my $LANG = "script internal"; system('echo $LANG'); system('echo','$LANG'); my @out = qx "echo $LANG"; chomp(@out); print "out=@out\n"; my @out = qx' echo $LANG '; chomp(@out); print "out=@out\n";

      it's output is :

      ./essaiQX.pl fr_FR.UTF-8 $LANG out=script internal out=fr_FR.UTF-8

      In fact i'd like to pass parameters to the command (the "echo" program in this little example), with possibly $, *, and such characters which the shell interpretes. while avoiding a large collection of antislashes. May be, it's not possible with qx and I must use system(). That's what I'd like to know</p

        If you want the environments LANG variable, use $ENV{LANG}.
Re: system() and qx comparison
by ikegami (Pope) on Sep 27, 2011 at 23:25 UTC

    IPC::System::Simple provides capture and capturex which are single- and multi-arg versions of qw respectively.

    use IPC::System::Simple qw( capture capturex ); my $foo = capture("shell command"); my $foo = capturex("program"); my $foo = capturex("program", @args);

    Conveniently, they even do the error checking for you.

    By the way, IPC::System::Simple can also add error checking to system.

    use IPC::System::Simple qw( system systemx ); system("shell command"); systemx("program"); systemx("program", @args);
      Thank you a lot. It really seems to be what I was looking for !!!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2014-07-29 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (225 votes), past polls