Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl - using backticks instead of system()

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?

like image 289
Tyzak Avatar asked Dec 06 '22 14:12

Tyzak


1 Answers

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.

like image 179
TLP Avatar answered Dec 31 '22 02:12

TLP