开发者

Perl - Master script calling sub-scripts and return status

开发者 https://www.devze.com 2023-02-06 22:58 出处:网络
Here\'s the design I want to accomplish in Perl: A master script calls multiple sub-scripts. The master script controls the calling of each sub-script in a particular sequence and records output from

Here's the design I want to accomplish in Perl:

A master script calls multiple sub-scripts. The master script controls the calling of each sub-script in a particular sequence and records output from each sub-script in order to decide whether on not to call the next script.

Currently, I have a master script that calls the sub-script using a system() call, but I am having trouble having the sub-script communicate back status to开发者_JAVA百科 the master script.

Do not want to use sub functions, would really like to keep each of the sub-script code separate.

To shed more light on the problem: The sub script should decide what to report back to the master script. For eg: sub script sends code 1 when sub script finds a string value in the database, it sends a code 2 when the sub string doesn't find the file its looking for, and sends a code of 0 when everything goes fine.


Can't you just use exit codes for this?

my $code = system( 'perl', '-e', 'exit 2;' ) >> 8; # $code = 2
say "\$code=$code";

Exit codes can be 255 distinct values.


You can execute and capture output from system commands with backtick syntax.

# get result as scalar
$result = `ls -lA`;

# get the result as an array, each line of output is a separate array entry
@result = `ls -lA`;

Whenever you use the backtick syntax, the exit status of the command is also stored in the automatic variable $?

You can then have the master script decide if the output is good or not using whatever logic you need.


Looking at Axeman's answer you could use the IPC::System::Simple module:

#!/usr/bin/perl
use warnings;
use 5.012;
use IPC::System::Simple qw(system $EXITVAL EXIT_ANY);


system( [2], 'perl', '-e', 'exit 2' );
say "EXITVAL: $EXITVAL";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号