Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Calling perl function from another perl script with different Active perl versions

by maria80e (Novice)
on Apr 06, 2020 at 07:32 UTC ( [id://11115119]=perlquestion: print w/replies, xml ) Need Help??

maria80e has asked for the wisdom of the Perl Monks concerning the following question:

We have two versions of Active perl 5.6 and 5.24. We have web services which has to be executed on Active perl '5.24' versions(to adopt TLS 1.2 version) and this needs to be invoked from Active perl '5.6' version. We are using windows operating system.

Steps followed : Caller code which is executed in 5.6 version invokes the 5.24 version using system /require command.

Problem: How to call the 5.24 perl function(example: webservicecall(arg1){return "xyz") from 5.6 perl script through system command, require or etc..? Also how to get the return value of perl function 5.24?

Note: Its a temporary work around to have two perl versions and the we have a plan to do upgrade it for higher version.

Here perl version 5.6 installed in "C:\Perl\bin\perl\" and perl version 5.24 installed in "D:\Perl\bin\perl\".

p5_6.pl print "Hello Perl5_6\n"; system('D:\Perl\bin\perl D:\sample_program\p5.24.pl'); print $OUTFILE; $retval = Mul(25, 10); print ("Return value is $retval\n" );
p5_24.pl print "Hello Perl5_24\n"; our $OUTFILE = "Hello test"; sub Mul($$) { my($a, $b ) = @_; my $c = $a * $b; return($c); }

I have written sample program for detail information to call perl 5.24 version from perl script 5.6 version. During execution I didn't get the expected output. How to get the "return $c" value & the "our $OUTFILE" value of p5_24.pl in p5_6.pl script?

Note: The above is the sample program based on this I will modify the actual program using serialized data.

Replies are listed 'Best First'.
Re: Calling perl function from another perl script with different Active perl versions
by choroba (Cardinal) on Apr 06, 2020 at 08:58 UTC
    You can't "call a function in a different Perl version". You need the different Perl version to run a full program with input arguments and capture its output to get the result.
    #!perl5.6 $result = `D:/Perl/bin/perl D:/sample_program/p5.24.pl 25 10`;

    #!perl5.24 my ($x, $y) = @ARGV; print $x * $y;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Calling perl function from another perl script with different Active perl versions
by bliako (Monsignor) on Apr 06, 2020 at 16:59 UTC
    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.

Re: Calling perl function from another perl script with different Active perl versions
by choroba (Cardinal) on Apr 07, 2020 at 17:32 UTC
    Probably crossposted to StackOverflow. Crossposting is OK, just please mention it, so people not attending both sites don't waste their efforts hacking a problem already solved at the other site.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Calling perl function from another perl script with different Active perl versions
by Anonymous Monk on Apr 06, 2020 at 08:51 UTC
    Capture::Tiny

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115119]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found