1

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... 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.)

Community
  • 1
  • 1
Alex
  • 909
  • 2
  • 11
  • 25
  • Works fine on ActivePerl 5.10 as well – DVK Oct 31 '10 at 21:15
  • @DVK, the program seems so basic for it to be an incompatibility between 5.12 and 5.10 or 5.8....but that's my only guess as well....though I'd still not understand the problem... – Alex Oct 31 '10 at 21:26
  • Would it be out of the question to try installing the latest ActiveState Perl (which IIRC is 5.10) and test? – DVK Oct 31 '10 at 21:41
  • hmmm...i still get the same thing....stumped – Alex Oct 31 '10 at 22:04
  • Are you running activestate or strawberry. Strawberry doesn't setup .pl associations as far as I can tell, and rely on .bat files to make it work, perhaps there is a reason? – Kent Fredric Nov 01 '10 at 01:12
  • @Kent, I was originally running strawberry on this system but as part of trouble shooting installed ActiveState. The problem was happening with strawberry and continued after the ActiveState install. See the edit with the fix. When going through the registry (with activestate installed) looking for perl.exe i ran across the ftype(?) which was wrong and also pointing to strawberry perl. So...i still don't know what caused it but the strawberry perl uninstall doesn't clean up after itself and the activestate install doesn't overwrite all the correct items. – Alex Nov 01 '10 at 08:15

6 Answers6

4

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
  • 116,958
  • 15
  • 196
  • 339
2

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
  • 53,118
  • 13
  • 86
  • 159
  • 1
    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 Oct 31 '10 at 21:19
  • Not completely irrelevant. Perl still parses the #! line for options, no matter the platform or invocation mechanism. – tchrist Oct 31 '10 at 21:24
  • 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 Oct 31 '10 at 21:24
  • @tchrist - sorry, meant irrelevant to deciding whether to interpret the script via Perl or some other program. your caveat is correct – DVK Oct 31 '10 at 21:26
  • @DVK: oops yes, I missed the OS clarification :) – Ether Oct 31 '10 at 21:29
  • 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 Oct 31 '10 at 22:27
0

If you are using the key word perl fileName.pl -f checkfile (for ex) then you are explicitly invoking the interpreter.

Now, if you want to execute the same command but without the perl key word, then you will need to add the shebang to your code. Check your Windows perl installation. it should be something like this: #!/perl/bin/perl.

What is going to happen now, with this new line, the command line will read the 1st 2 bytes(#!) of the script and figure out that it is a perl script and will know where to find the interpreter and will invoke it(since you provided the path to it). Hence your script will be executed as a perl script.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
0

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
  • 126,886
  • 32
  • 213
  • 327
  • it just says perl.exe. (and there's only one perl installed on the system...) – Alex Oct 31 '10 at 21:28
  • Try replacing the association with actual Perl path – DVK Oct 31 '10 at 21:41
  • 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 Oct 31 '10 at 22:10
  • 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 Oct 31 '10 at 22:12
  • @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 Oct 31 '10 at 22:34
  • @tchrist,@Alex - momentary glitch in the matrix, started thinking in shell, sorry :) Alex - [\@ARGV] – DVK Oct 31 '10 at 23:50
0

fixed and this question was a repeat... 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!

Community
  • 1
  • 1
Alex
  • 909
  • 2
  • 11
  • 25
0

You don't have to add "perl" prefix as..

In order to do so you need to change the PATH environment variable, below is the command to change PATH environment variable

PATH=$PATH:$(pwd)

This will append the current working directory to the list of directories in the PATH environment variable. Once we do this we can now run without "perl" prefix

$ hello.pl

Hello world
Jan_V
  • 4,244
  • 1
  • 40
  • 64