“捕获”语法从批处理返回Perl脚本的值?

时间:2012-07-05 21:56:36

标签: perl batch-file return

我想在批处理中存储perl的返回值。我该如何做到这一点?

Batch Code:
FOR /F "delims=" %%I IN ('perl.exe c:\TestProp1noRefCases.pl 859') DO set lineCase=%%I 
echo !lineCase! ::Nothing is outputted

Perl Code:
use strict;
use warnings;
sub main1;

my $arg1 =shift;
main1($arg1);
exit;
sub main1
{
            #Returns 1,2,3 Depending on testNum passed
            my @gp1= (829,845,851,859,864,867);
            my @gp2= (861,863,865);
            # my @gp4= (826-828,830-839,843-844,847-850,852-854,860-862,883);
    # my @gp3= (877-882,884);
            my $val1=1;
            my $val2=2;
            my $val3=3;
            my $val4=4;

            if((grep /^$arg1$/,@gp1) || ($arg1 >=822 && $arg1<=824))
            {     
                            # print "$val1\n";
                            return $val1;

            } elsif ((grep /^$arg1$/,@gp2) || ($arg1>=855 && $arg1<=858))
            {
            #print "$val2\n";
                            return $val2;
            } elsif (($arg1==884) || ($arg1>=877 && $arg1<=882))
            {
            #print "$val3\n";
                            return $val3;
            } else
            {
                #print "$val4\n";
                            return $val4;

            }

}

2 个答案:

答案 0 :(得分:1)

您没有退出返回值。

而不是

main1($arg1);
exit;

尝试:

my $result = main1($arg1);
exit $result;

或:

exit main1($arg1);

然后检查批次中的%ERRORLEVEL%

perl.exe c:\TestProp1noRefCases.pl 859
set lineCase=%ERRORLEVEL%

编辑:注意,这仅适用于小的无符号整数,例如示例中的$val1..$val4

答案 1 :(得分:1)

您实际上从未打印过要捕获的值。变化

main1($arg1);

print main1($arg1);