从批处理调用时,Perl脚本不返回任何值?

时间:2012-07-05 19:03:36

标签: batch-file for-loop

我从批处理中调用perl脚本并给出一个合适的参数,它假设返回1,2,3或4.但它不是。我从命令行检查了Perl的输出,他们正在输出正确的结果,我认为它在批处理语法中

批次:

@echo off 
setlocal enabledelayedexpansion
FOR /F "delims=*" %%I IN ('Perl C:/ExecutionSDKTest_10.2.2/TestProp1noRefCheck.pl 844') DO     set lineCase=%%I  
echo line case is !lineCase!

的Perl:

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;

            }

}

1 个答案:

答案 0 :(得分:0)

我这样做test.cmd

@echo off 
setlocal enabledelayedexpansion
for %%I in (%*) do (
   call perlfake %%I
   set lineCase=!ERRORLEVEL!
   echo line case [for %%I] is !lineCase!
)

然后在同一目录中创建perlfake.cmd

@echo off 
set /a returnvalue="%1 %% 4 + 1"
REM echo (%1 mod 4) + 1 = %returnvalue%
exit /b %returnvalue%

然后拨打test 1 2 3 4 844 35

结果:

line case [for 1] is 2
line case [for 2] is 3
line case [for 3] is 4
line case [for 4] is 1
line case [for 844] is 1
line case [for 35] is 4

确定实际返回值的逻辑与perl代码不同,但正如您所看到的,这只是假代码。

因此,请将此调用更改为:

,以对照您的perl代码
REM call perlfake %%I
call Perl C:/ExecutionSDKTest_10.2.2/TestProp1noRefCheck.pl %%I

关于你的Perl脚本,我可以看到你在执行此操作时丢弃了main1的返回值:

main1($arg1);
exit;

你想要这样的东西:

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

像这样从Perl退出将设置errorlevel。

编辑:

我现在已经下载了ActiveState的Perl 5.14.2,并更改了我的批处理以调用你的perl脚本,我的更改返回$ result。这一切现在都有效,例如: test.cmd 844 861

返回:

line case [for 844] is 4
line case [for 861] is 2