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

dear monks...
I have a problem with backticks.
I have a program (lets call it a.pl) that uses them in a basic fashion:
$ret = `$exc`;
where $exc is the command (another script + its argument).
My problem is that I'd like to let a.pl execute in background, as in:
a.pl &
Yet, when arriving at the backtick call, I see its status (jobs) changing from Running to Suspended.
Calling it to foreground (fg) it resumes without problem.
Why? Nowhere in the call $exc needs user input - which is the only reason I can guess for this behaviour - and the output surely must go to $ret.

I hope you cna help me here...
thanks
Alessandro

Replies are listed 'Best First'.
Re: backticks & job control
by mickeyn (Priest) on Mar 01, 2006 at 13:13 UTC
    check IPC::Run

    Mickey

Re: backticks & job control
by wulvrine (Friar) on Mar 01, 2006 at 14:25 UTC
    Have you tried forking this off and having the child run the exec? This way the child keeps running as well as the parent (your main program). The parent would also have access to the child incase it needed to kill the process (a.pl).
      you mean with fork() ? mmmm... I've always been much afraid of forks/threads, don't even know where to begin... alessandro
        I was in the similar situation some weeks ago, there is a snippet:
        my $pid = fork(); if ($pid) { #parent process continues here, $pid contains children pid } else { #child process continues here exec('some_command'); }
Re: backticks & job control
by graff (Chancellor) on Mar 02, 2006 at 06:02 UTC
    I'm a little confused... Your original "a.pl" program captures the output from your other script, and assigns it to $ret. If the other script were running in the background (so that "a.pl" continues to do other things at the same time), do you still need the output of that other script to be assigned to some variable in "a.pl"?

    Maybe you've already figured that part out, but it wasn't clear from your post or other replies in the thread, and it would make a difference in terms of what a correct solution would look like.

    Obviously, if you want output from the other script to be assigned to a variable in "a.pl", and you want the other script to run in the background, you have to save it's output to a file, have "a.pl" doing other stuff (possibly including "wait" or "waitpid") until the background job is done, then read the output file that it created. Or maybe you can find another technique in perlipc that would make more sense for you.