Require exit status !=0 when script inside system command doesn't go well

272 views Asked by At

Code of inter.pl is:

use strict;
use warnings;

my $var1=`cat /gra/def/ment/ckfile.txt`;  #ckfile.txt doesn't exist
print "Hello World";
exit 0;

Code of ext.pl

my $rc = system ("perl inter.pl");
print "$rc is rc\n";

Here, when I run "perl ext.pl", $rc is coming as 0.

Although file inside inter.pl (/gra/def/ment/ckfile.txt) doesn’t exist, I am getting $rc as 0.

I would want $rc to be != 0 (as in one way, it should be an error as file ckfile.txt doesn't exist) in this same scenario.

Note: I can't do any modification in inter.pl

How can it be implemented?

Thanks In Advance.

1

There are 1 answers

2
ikegami On

If you want the program to have an non-zero exit status, you'll need to replace the (useless) exit 0;.

my $var1=`cat /gra/def/ment/ckfile.txt`;
exit 1 if $?;

or

my $var1=`cat /gra/def/ment/ckfile.txt`;
die("Can't spawn child: $!\n") if $? == -1;
die("Child killed by signal ".( $? & 0x7F )."\n") if $? & 0x7F;
die("Child exited with error ".( $? >> 8 )."\n") if $? >> 8;