perl - using backticks instead of system()

backticks, perl, return-value, system

Solution

To go through the shell in order to move data from one perl script to another is not the best solution. You should know that backticks or `qx()` captures whatever is printed to STDOUT. Using `exit ($var)` from the other script is unlikely to be captured properly. You would need `print $var`. But don't do that.

Instead, make the other script into a module and use the subroutine directly. This is a simplistic example:

In bar.pm:

use strict;
use warnings;

package bar;  # declare our package

sub fooz {             # <--- Our target subroutine
    my $in = shift;    # passing the input
    return $in * 5;    # return value
}
1; # return value must be true

In main.pl:

use strict;
use warnings;
use bar;   # bar.pm must be in one path in @INC

my $foo = bar::fooz(12);  # calling fooz from our other perl script
print "Foo is $foo\n";

There is a lot more to learn, and I suggest you read up on the documentation.

Problem

I have a perl script that calls an other perl script by using system() it's like: ``` my $returnval= system("perl", $path,$val1, $val2,@myarray); ``` Because system() returns only the exit status but I want the script's output I want to use backticks. I tried something like that: ``` my $returnval = `$path`; ``` how can I add the parameters the script should receive? how should the other perl script's return code looks like? At the moment it's like ``` exit ($myreturnedvalue); ``` (how) Is it possible to return multiple values?

Original source