in reply to using qx{}

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.

Replies are listed 'Best First'.
Re: Re: using qx{}
by CheeseLord (Deacon) on Sep 04, 2001 at 05:07 UTC

    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)];