"The perl script does not need to know what env variables are being set, only that the shell script ran."
If you need to know that it ran then don't go for backticks... but reasonablekeith has correctly offered here the answer to the bit you said you needed - the exit code of the script - which you can control with the exit command.
#!/bin/sh
#
# shell_script.sh
#
MY_VAR="something"
./simulated_binary_executable.pl
echo "Returned $?"
echo "Exporting MY_VAR"
export MY_VAR
./simulated_binary_executable.pl
echo "Returned $?"
exit 0
and (to simulate the binary you are calling from the shell script) try.pl like this
#!/usr/bin/perl
#
# simulated_binary_executable.pl
#
use strict;
use warnings;
if ( $ENV{MY_VAR} ) {
print "MY_VAR is '$ENV{MY_VAR}'\n";
exit 0;
}
else {
print "MY_VAR is not set\n";
exit 1;
}
Wrapping this in a dummy of your outer perl script:
#!/usr/bin/perl
#
# Dummy of outer perl script that is calling shell script
#
use strict;
use warnings;
# You could use $? later instead of defining $return
my $return = system("./shell_script.sh");
if ($return == -1) {
print "failed to execute: $!\n";
}
elsif ($return & 127) {
printf "child died with signal %d, %s coredump\n",
($return & 127), ($return & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $return >> 8;
}
Or rather doing away with the intermediate shell script and setting the variables it would have set from within perl:
#!/usr/bin/perl
#
# Dummy of outer perl script
# This time calling the binary without shell wrapper script
#
use strict;
use warnings;
#
# Export the environment variable for the child binary
# directly from Perl
#
$ENV{MY_VAR} = "something else";
my $binary = "./simulated_binary_executable.pl";
system($binary); # Run the external program
if ($? == -1) {
print "Child '$binary' failed to execute: $!\n";
}
elsif ($? & 127) {
printf "Child '%s' died with signal %d, %s coredump\n",
$binary, ($? & 127), ($? & 128) ? 'with' : 'without';
}
elsif ($? == 0) {
print "Child '%s' exited successfully.\n";
}
else {
printf "Child '%s' exited with value %d\n", $binary, $? >> 8;
}
|