Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

using qx{}

by muzakfetch (Scribe)
on Sep 04, 2001 at 03:23 UTC ( [id://109962]=perlquestion: print w/replies, xml ) Need Help??

muzakfetch has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone! I am trying to run an external script that provides data from a script. Using the following:

my @radiusdata = qx{ radiusreport -t -l $username[0] -f /var/log/radac +ct/65.166.140.210/detail } ;

What happens, is the $username gets translated literally. Anyone shed a little light on this?

Thanks, Muzakfetch

Replies are listed 'Best First'.
Re: using qx{}
by monkfish (Pilgrim) on Sep 04, 2001 at 04:26 UTC
    I am not sure what you mean when you say translated literally, but let me try and help...

    $username[0] = "bob"; @foo = qx { echo $username[0] };

    qx interpolates the string so @foo[0] will get set to 'bob'.

    To avoid this you could escape the string:

    $username[0] = "bob"; @foo = qx { echo \$username[0] };

    This would cause the $username not to be interpolated and it would let the shell decipher a variable called $username when it called the echo.

    Hope that helps.

    -monkfish (The Fishy Monk)

    9/14/01 Edited to clarify and remove incorrect portion of post.

      While you are correct in your statements about interpolation, your second system call will not store anything in $foo[0] besides 0 (or, possibly, 256). The difference between system and qx is quite important - the former returns the return value of the code's execution, and the latter will return the output from the code's execution.

      Should you desire to keep the string passed to a qx from interpolating, use single quotes as the delimiter, like so:

      @foo = qx'echo $HOME';

      Which should store "/home/cheesy\n" or something like that in $foo[0]. I hope that helps...

      Update: Changed emphasis in a few places.

      Update #2: The node I replied to has had its content altered in a way that makes this node make no sense... the original second system call was as follows:

      @foo = system('echo $username[0]');

      I hope that clears up any confusion.

      His Royal Cheeziness

        You are correct in your statements about system, qx, and the backtick (`) operator. However, there is little syntactical difference between your example with qx'', and qx{}. Both are correct and will return the result of execution. The previous poster, didn't really mention system at all. (Just observing)

        #!/usr/bin/perl -w @username = qw(nobody anybody); @foo = qx{ echo $username[0] }; chomp($foo[0]); print "'$foo[0]'\n"; @foo = qx{ echo \$username[0] }; chomp($foo[0]); print "'$foo[0]'\n"; @foo = qx' echo $username[0] '; chomp($foo[0]); print "'$foo[0]'\n"; # prints out #'nobody' #'[0]' #'[0]'


        my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found