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

In short this is what I am trying to do. I have tried exec, system, and `` to call prog B. The problem I have is prog A waits for a return from prog B. Prog B is meant to loop as a batch job and not returna value. Any ideas.

Prog A
Create or delete a touch file
Call prog B (return right away)
End

Prog B
Loop while (-e touch file)
{Do Stuff}
end

Replies are listed 'Best First'.
Re: Calling a Program from a Program
by broquaint (Abbot) on Jun 17, 2003 at 17:01 UTC
    Just use a simple fork and exec e.g
    ## do stuff fork() and exec "your_program";
    This will fork off a child then executes a system command which then assumes the child process (as it were).
    HTH

    _________
    broquaint

Re: Calling a Program from a Program
by Zaxo (Archbishop) on Jun 17, 2003 at 17:05 UTC

    I don't see the need for two programs from what you show, but assuming your design is correct fork will let you exec B

    defined( my $foo = fork) or die $!; $foo or exec '/path/to/B', @parms;
    That is enough if you are certain that B will run longer than the remainder of A. Otherwise you must prevent B from going zombie.

    After Compline,
    Zaxo

      Just for my clarification. $foo will capture a fork error if it occurs?
      I am not educated enough in perl to understand the logical or in
      $foo or exec ....

        The return value from fork has three states,

        • undef - meaning fork failed
        • 0 - meaning you're in the child process
        • true - meaning you're in the parent, and the numeric value is the pid of the child

        That's all documented in the perldoc linked by fork.

        After Compline,
        Zaxo

Re: Calling a Program from a Program
by Itatsumaki (Friar) on Jun 17, 2003 at 17:05 UTC

    Maybe this link from the Perl FAQ will help?

    -Tats
Re: Calling a Program from a Program
by Coplan (Pilgrim) on Jun 17, 2003 at 17:01 UTC
    Since Prog B loops...would it be possible to have it update a data file every time it loops? At that point, you could simply pull the data created by prog B into Prog A. Of course, you might want to set Prog A to wait until the data created by Prog B has been updated AFTER the init time of Prog A.

    In other words:
    Set prog A to check the time when it starts. Then set Prog A to check the file date of the data file put out by Prog B. I would do that in a loop until the date/time of the data file is later than the start time of Prog A. Then we know the data is current. Then Prob A can continue on with its data. Meanwhile, Prog B continues to loop as its supposed to.

    But I must state the obvious right now. You said that you want Prog A to wait for a return from Prog B. Yet you also say that Prog B is not meant to return a value. Obviously...this can't work. I assume you mean otherwise?

    --Coplan