2012-07-05 132 views
1

我想批量存儲返回的perl值。我該如何做到這一點?「捕獲」從批處理返回的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; 

      } 

} 

回答

1

您從未實際打印要捕獲的值。更改

main1($arg1); 

print main1($arg1); 
+0

我不熟悉'回聲!lineCase!'。如果這不起作用,你可以使用'echo%lineCase%'。 – ikegami 2012-07-06 00:28:18

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