1

I'd like to store a returned value of perl in batch. How do I accomplish this?

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 Answers 2

1

You never actually print a value to be captured. Change

main1($arg1);

to

print main1($arg1);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not familiar with echo !lineCase!. If that doesn't work, you can use echo %lineCase%.
1

You're not exiting with a return value.

Instead of

main1($arg1);
exit;

Try:

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

or:

exit main1($arg1);

and then examine %ERRORLEVEL% in your batch.

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

Edit: Note, this only works for small unsigned integers, like the $val1..$val4 in your example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.