APM: Running a perl prog from a perl prog?

tmcd at panix.com tmcd at panix.com
Tue Oct 17 21:47:32 PDT 2006


On Tue, 17 Oct 2006, michalk <michalk at awpi.com> wrote:
> exec

Destroys the current process, so you can't use it alone to have one
Perl program call another.  If you call fork() first, then one of the
processes, usually the child, can call exec() to do the second
process.  fork-and-exec is the way to get the most sophisticated
effects and maximum control.

> system

Runs an arbitrary program (so whether it's Perl or not is
irrelevant).  The standard output and standard input remain the
caller's, so the caller doesn't get to look at the command's output.
Of course, that's sometimes exactly what you want.

> $variable = `otherprogram.pl`;
> also does the trick.  Note the backticks.
> This will kill peformance though.  Perl has a lot of initializing to
> do each time you call it.

If you're not doing many calls, the slowness isn't a problem, but the
problem as stated by Captain Nemo does seem to indicate that he
expects a lot of calls.

Using backticks means that the caller will wait until the child is
done and then it will get all its output at once.  Of course, that's
sometimes exactly what you want.

There's also
    open(FH, "command line here |")
to run the command and route its standard output to FH for the caller
to read (assuming the operating system allows it, as it certainly does
on Linux).  Similarly
    open(FH, "| command line here")
when the caller wants to feed input to the command.  And similar

constructs with various I/O modules.  Then the input/output can be
record by record; depending on the appication, it can be more
efficient (as opposed to one massive buffer in `...`).  Of course,
that's sometimes exactly what you want.  This also allows feeding
input into the command, which `...` doesn't allow (except by
    $x = `echo hello world | cat`;
).

-- 
Tim McDaniel; Reply-To: tmcd at panix.com


More information about the Austin mailing list