How to call a another program and die in Perl?

tim85a

New member
I've got a program that I'm wighting in Perl that I would like to call program2.pl and die if something happens. I've got the line of code:

die {exec "program2.pl"};

will that make my first program die? I know it calls program2.pl, but I don't want program1 to keep going. I read some where that the exec bit pauses the program and calls whatever program you want.
 
Get rid of the "die" part. exec all by itself will REPLACE the current program with the program2.pl. BUT you have to put 'perl' in front of it:

exec 'perl program2.pl';
print STDERR "This message will never get printed\n";
 
exec "perl program2.pl" or die $!

Quote from perlfunc manpage:
The exec function executes a system command and never returns-- use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell.
Since it's a common mistake to use exec instead of system, Perl warns you if there is a following statement which isn't die, warn, or exit (if -w is set - but you always do that).
 
Get rid of the "die" part. exec all by itself will REPLACE the current program with the program2.pl. BUT you have to put 'perl' in front of it:

exec 'perl program2.pl';
print STDERR "This message will never get printed\n";
 
exec "perl program2.pl" or die $!

Quote from perlfunc manpage:
The exec function executes a system command and never returns-- use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell.
Since it's a common mistake to use exec instead of system, Perl warns you if there is a following statement which isn't die, warn, or exit (if -w is set - but you always do that).
 
Back
Top