in reply to Calling perl function from another perl script with different Active perl versions
use strict; use warnings; use Capture::Tiny ':all'; # anonymonk tip #use File::Temp; # for complex drivers write it to a temp file sub bad { my ($perl_executable, $perl_module_to_require, $perl_code_to_drive_it ) = @_; # capture from external command my $prog = <<EOQ; require "$perl_module_to_require" or die "failed to load module $perl_ +module_to_require"; $perl_code_to_drive_it EOQ # my $tmpfile = File::Temp::tempfile(); print "executing: $perl_executable -I. -e '".$prog."'\n"; my ($stdout, $stderr, $exit) = capture { system( $perl_executable, '-I', '.', '-e', $prog ); }; die "exit=$exit\n $stderr\n" if $exit; print "stdout=$stdout\n"; print "stderr=$stderr\n"; } # and here is the example use bad( '/usr/bin/perl', # perl exec to run 'p5_24.pl', # the file which contains Mul() 'my $res = Mul(6,7); print "$res\n"' # the harness );
there is a reason I named it bad
bw, bliako
Edit: I have now seen the crosspost at https://stackoverflow.com/questions/61085771/invokes-perl-modules-from-perl-script-in-different-perl-versions and I somehow feel I want to say I definetely was not aware about any of the answers there when I answered. One answer, older than mine here, is outlining the above technique.
|
|---|