Home: Perl Programming Help: Intermediate:
running a prog inside of a prog


Dec 17, 2000, 4:27 PM

Views: 317
running a prog inside of a prog

How do you run an external program from within another?

Enthusiast / Moderator

Dec 17, 2000, 4:33 PM

Views: 315
Re: running a prog inside of a prog

Did you try include "script.pl"; ?



Dec 17, 2000, 5:32 PM

Views: 314
Re: running a prog inside of a prog

There are basically three ways:

# exec() replaces the current process with a new one 
exec $prog, @args;
# program only gets here if exec() failed
die "couldn't exec '$prog': $!";

# system() returns the exit status, NOT the output
system($prog, @args) and die "$prog failed: $?";

# backticks or qx!! return output
@output = `$prog @args` or die "$prog failed: $!";
$output = qx($prog @args) or die "$prog failed: $!";

You can use the open() function, but these will probably suffice. Please read perldoc -f exec and perldoc -f system for the documentation on those functions, and perldoc perlop for documentation on qx().

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author