#!/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 #### #!/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; } #### #!/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; } #### #!/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; }