views:

99

answers:

4

When i run my script like so:

C:\>perl script.pl -f file

It works fine. But, if I just do:

C:\>script.pl -f file

then I don't get any errors but getopts doesn't assign anything to $opt_f

This works just fine on perl 5.8 Windows XP, but it doesn't work on perl 5.12 on Windows 7. There aren't any other versions of perl installed (its a new OS build).

Code:

use Getopt::Std;
our ($opt_f);
getopts('f:');
print "input file is: $opt_f \n";
print "$0\n

Run with:

C:\> perl get.pl -f sadf
input file is: sadf
get.pl

Run without:

C:\>get.pl -f sadf
input file is:

Nothing!

EDIT: fixed and this question was a repeat... http://stackoverflow.com/questions/1695188/how-do-i-make-perl-scripts-recognize-parameters-in-the-win32-cmd-console

The OP of that post figured it out.
I had to do the same but also recreate the assoc in the gui (in addition to in the reg and on the command line with ftype.)

+2  A: 

When you invoke your code as perl script.pl -f file, you are explicitly running the perl executable and passing it a filename and options to parse. But when you invoke it as script.pl -f file, you are asking your login shell to run the file, which it will parse as a shell script in the absence of any other information -- this is not what you want, as your file is not a bash script, but a perl script!

Normally such information (what program to use to parse the script) is given in what is called a shebang line. If you add this to the top of your script, it should run properly:

#!/usr/bin/perl

(or perhaps #!/bin/env perl, if you want the env program to find perl in your $PATH for you).

Ether
While your answer seems spot on for a Unix box; the OP indicated it's on Windows7 system unless I misread something... I would have expected Perl scripts to have been associated (via .pl extension) with Perl in any case so the shebang line (which is not used on Windows) is irrelevant
DVK
Not completely irrelevant. Perl still parses the #! line for options, no matter the platform or invocation mechanism.
tchrist
er..., it's definitely running, note the "inputfile is:" that is printed. in windows, in Control Panel\All Control Panel Items\Default Programs\Set Associations, you set the associations for what program you want to run based on the extension of a file. So, the perl setup sets .pl to be associated with perl.exe . (and so you don't needed the #! in the script). yeah?
Alex
@tchrist - sorry, meant irrelevant to deciding whether to interpret the script via Perl or some other program. your caveat is correct
DVK
@DVK: oops yes, I missed the OS clarification :)
Ether
Is that '`#!/bin/env perl`' or '`#!/usr/bin/env perl`'? On one Linux box, it is found in both locations; on MacOS X, it is `/usr/bin/env` and not `/bin/env`; on Solaris 10, it is `/usr/bin/env`; on AIX (5.3), it is `/usr/bin/env`; on HP-UX 11i, it is `/usr/bin/env`. So, in general, for maximal portability, use `usr/bin/env` rather than `/bin/env`.
Jonathan Leffler
A: 

What are Perl scripts associated with (.pl extension to be precise) on the Windows7 system where the "script.pl" call doesn't work?

Also, could you please try running (with and without "perl " prefix):

use Data::Dumper; print Data::Dumper->Dump([$*, $0, $1])

DVK
it just says perl.exe. (and there's only one perl installed on the system...)
Alex
Try replacing the association with actual Perl path
DVK
Do we actually trust Windows associations to properly pass command-line arguments into the program? I remember having to use some special parsing routine in a C function to emulate crt0 back under CPM.
 



 I'm also confused about your array argument to `->Dump`, because it sure looks like the shell's idea of variables, not perl's. Is that intentional?
tchrist
c:\temp>d.pl $* is no longer supported at C:\temp\d.pl line 1. $VAR1 = undef; $VAR2 = 'C:\\temp\\d.pl'; $VAR3 = undef; c:\temp>perl d.pl $* is no longer supported at d.pl line 1. $VAR1 = undef; $VAR2 = 'd.pl'; $VAR3 = undef;
Alex
@Alex, those are the wrong variables; not sure what DVK had in mind. It's just `$0` and `@ARGV` that you need to print out, and you need to do so before calling `getopts()`. 'Fraid I'm not likely to be much more help than that, though, as my last Windows experience was CPM. ☺
tchrist
@tchrist,@Alex - momentary glitch in the matrix, started thinking in shell, sorry :) Alex - [\@ARGV]
DVK
+4  A: 

First, look at:

C:\> assoc .pl
.pl=Perl

Take the string on the RHS, and invoke:

C:\> ftype Perl
Perl="C:\opt\Perl\bin\perl.exe" "%1" %*

Make sure %* is there.

If not, run a cmd.exe shell as administrator, and invoke

C:\> ftype Perl=perl.exe %1 %*

See also ftype /?.

Sinan Ünür
yup...it;s exactly like that: >ftype PerlPerl="C:\Perl\bin\perl.exe" "%1" %*
Alex
A: 

fixed and this question was a repeat... http://stackoverflow.com/questions/1695188/how-do-i-make-perl-scripts-recognize-parameters-in-the-win32-cmd-console

The OP of that post figured it out. I had to do the same but also recreate the assoc in the gui (in addition to in the reg and on the command line with ftype.)

Thanks everyone for your help!

Alex