Link to home
Start Free TrialLog in
Avatar of asenna425
asenna425

asked on

Getopt:STD in .bat file

I have a perl script that use getopt.  It works fine if it is save as *.pl, but it wouldn't work if I change the file type to *.bat.  How could I use Getopt function in a *.bat file?

code:
#!/usr/bin/perl
#perl script starts below here
use Getopt::Std;

getopt("s");

if ($opt_s ne ""){
   goto "STEP".$opt_s;
}
STEP1:
#print "";
print "Step1: Start\n";

STEP2:
print "Step2\n";

STEP3:
print "Step3\n";
Avatar of Bartender_1
Bartender_1
Flag of Canada image

This is because DOS commands don't easily accept user input. Are you sure you have to run this as a batch file? If so, see here about getting batch files to accept user input:
http://www.robvanderwoude.com/userinput.php

Hope this helps!

:o)

Bartender_1
Avatar of Bill Prew
Bill Prew

It looks like you were running a perl script with either a /s1 or mabe a /s 1 parm passed in to it, it that right?  And you want to do something similar with a BAT file?  

If so, you might want to consider moving to a positional parm, and using that parms value as the switch.  So you could use a command like like:

foo.bat 2

and then the BAT file could do:

@echo off
if not "%~1" == "" goto STEP%~1
exit /b
:STEP1
echo Step 1
exit /b
:STEP2
echo Step 2
exit /b
:STEP3
echo Step 3
exit /b

~bp
Avatar of asenna425

ASKER

Hi billprew,

thanks for the quick response.  I tried your suggestion but it failed to run if i implemented it with some header code...

@rem = 'Perl, ccperl will read this as an array of assignment & skip this block
@D:\Perl\bin\perl.exe -s "%~dp0%~nx0" %*
@SET resexe=%ERRORLEVEL%
@ECHO RESEXE=%resexe%
@GOTO:EOF
@rem ';

#!/usr/bin/perl
#perl script starts below here
use Win32::Process;
use Win32API::File 0.08 qw( :ALL );
use Fcntl ':flock';
use Errno qw (EWOULDBLOCK) ;

### your suggestion starts here ###
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is this what you're after?  It works fine for me.  Adjust the c:\strawberry\perl\bin\perl.exe to the path of your perl (D:\Perl\bin\perl.exe).
@rem = 'trick to get perl to ignore this junk
@echo off
c:\strawberry\perl\bin\perl.exe %0 %1 %2 %3 %4 %5
SET resexe=%ERRORLEVEL%
ECHO RESEXE=%resexe%
@rem';

use Getopt::Std;

getopt("s");

if ($opt_s ne ""){
   goto "STEP".$opt_s;
}
STEP1:
#print "";
print "Step1: Start\n";

STEP2:
print "Step2\n";

STEP3:
print "Step3\n";

Open in new window

When you rename it to a .bat, how were you starting it?  If you didn't have the header code (that you now have), unless you specifically told perl to run it, windows would not have known it was a perl file, and wouldn't have executed it correctly at all (windows doesn't use the #! line).  The installation of perl associates *.pl files with perl, so windows knows how to execute them.  As perl doens't care about the file name or extension, I think the error is related to how windows is handling the file.