print command in qx// and execute it

Y

yong321

Is there a way to print the command in qx/command/ or `command` and
then execute it, short of saving the command as a string and printing
the string first?

Yong Huang
 
M

Mirco Wahab

Is there a way to print the command in qx/command/ or `command` and
then execute it, short of saving the command as a string and printing
the string first?

What exactly do you mean?

sth. like the following?

my $command = 'qx{dir /w}';
print $command, "\n";

print eval $command;


Regards

M.
 
Y

yong321

Mirco said:
What exactly do you mean?

sth. like the following?

my $command = 'qx{dir /w}';
print $command, "\n";

print eval $command;


Regards

M.

I mean if I do

$result=qx/complicatedexecutable/;

I won't see the command. So I have to do

$cmd="complicatedexecutable";
print $cmd;
$result=qx/$cmd/;

It would be nice to combine print and qx into one command without
saving complicatedexecutable into a string first. Not a big deal but
it's something nice to have.

Yong Huang
 
A

anno4000

I mean if I do

$result=qx/complicatedexecutable/;

I won't see the command. So I have to do

$cmd="complicatedexecutable";
print $cmd;
$result=qx/$cmd/;

It would be nice to combine print and qx into one command without
saving complicatedexecutable into a string first. Not a big deal but
it's something nice to have.

Write a sub to do that (as BugBear suggested):

sub print_and_run {
local $" = ' ';
print "@_\n";
return qx(@_);
}

That works for single strings and commands in list form.

Anno
 
U

Uri Guttman

a> Write a sub to do that (as BugBear suggested):

a> sub print_and_run {
a> local $" = ' ';
a> print "@_\n";
a> return qx(@_);
a> }

a> That works for single strings and commands in list form.

it still doesn't solve the OP's question which really can't be
solved. he wants his qx to print its command and there is no direct way
to do that. a sub is one way or he can just use the variable. when i
want to log/print a qx i always use a variable. big deal. i don't waste
time trying to optimized little things like that.

uri
 
A

anno4000

Uri Guttman said:
a> Write a sub to do that (as BugBear suggested):

a> sub print_and_run {
a> local $" = ' ';
a> print "@_\n";
a> return qx(@_);
a> }

a> That works for single strings and commands in list form.

it still doesn't solve the OP's question which really can't be
solved. he wants his qx to print its command and there is no direct way
to do that.

Sure. A source filter could do it. Most of the time.
a sub is one way or he can just use the variable. when i
want to log/print a qx i always use a variable. big deal. i don't waste
time trying to optimized little things like that.

Nor do I. But note that I wrapped a little lesson about treatment
of $" and friends in the bit of useless code.

Anno
 
U

Uri Guttman

a> Nor do I. But note that I wrapped a little lesson about treatment
a> of $" and friends in the bit of useless code.

i never use those implicit vars like $". i would always be explicit and
use join. it seems clearer and less magical and doesn't really clutter
things up much. and i don't like munging globals even if i localize
them. just too fugly. same as why i don't use $_ unless i have to or
there is a good reason. sometimes explicit stuff is better than too much
implicit stuff. but that is a judgment call i can make easily.

uri
 
B

Ben Morrow

Quoth Uri Guttman said:
a> Write a sub to do that (as BugBear suggested):

it still doesn't solve the OP's question which really can't be
solved. he wants his qx to print its command and there is no direct way
to do that.

use 5.9.5;

BEGIN {
*CORE::GLOBAL::readpipe = sub ($) {
my ($cmd) = @_;
warn $cmd;
readpipe $cmd;
};
}

my $foo = `echo foo`;

# :)

# Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top