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

Dear Monks,

I am trying to get Win32::Job to execute a command line argument whereby the command line argument is in a variable

The output returns always following error:
'$exe' is not recognized as an internal or external command, operable program or batch file.

Any help would be appreciated

use Win32::Job; my $timeout = 30; # sec my $retry = 3; # times my $exe = "dir"; my $ok = 0; my $pid = undef; my $job = undef; # Try to execute command up to $retry times while ((! $ok) && ($retry > 0)) { # Create job $job = Win32::Job->new; # Create a new process and associate with the job $pid = $job->spawn("cmd", q{cmd /C $exe}, ); print "PID:$pid\n"; print "SYS:$^E\n"; # Run the job and kill it if more than $timeout sec $ok = $job->run($timeout); $retry = $retry - 1; }

Replies are listed 'Best First'.
Re: Use variable in Win32::Job to execute command line
by chrestomanci (Priest) on Dec 07, 2010 at 22:11 UTC

    You have used single quoting for your command, so $exe will not be interpolated.

    If you re-write line 16 as:

    pid = $job->spawn("cmd", qq{cmd /C $exe}, );

    Then it should work

Re: Use variable in Win32::Job to execute command line
by ikkeniet (Acolyte) on Dec 08, 2010 at 13:32 UTC
    Thanks for the help. Works like a charm now :-)