Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Perl scripts recognize parameters in the Win32 cmd console?

When I invoke my Perl scripts in the Windows environment without invoking perl first, the parameters are not passed to my script.

For example,

C:\> C:\my-perl-scripts\foo.pl bar

invokes foo.pl but doesn't recognize bar as a parameter (@ARGV is empty). However,

C:\> perl C:\my-perl-scripts\foo.pl bar

works as expected.

Is this a configuration issue?

Ideally, I'd like to be able to distribute some perl scripts, have the user add C:\my-perl-scripts\ to the path and then just be able to invoke foo.pl from anywhere while running cmd.

If they have to specify perl first, then they'll always have to give a complete path.

Any ideas or recommendations?

Edit: To show that the assoc and ftype are correct on my system, I executed the following commands.

C:\>assoc .pl
.pl=Perl

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

C:\>more t.pl
print "'$_'\n" for @ARGV;

C:\>t a b

C:\>perl t.pl a b
'a'
'b'

C:\>t.pl a b

C:\>

I included both the output of t and t.pl to show it's not a %PATHEXT% problem. Both outputted nothing as originally described whereas invoking perl first gave the expected response.

I'm not sure where to look next, but thanks for the suggestions so far. They have been very helpful.

Edit 2: The problem appears to be on my vista business box. On my xp pro box, it works as expected. Both have ActivePerl 5.8.9. I have another vista home box that I have not yet tried yet. I'll post back if I discover anything.

Edit 3: I found the answer (posted below). I found it by running a registry cleaner, removing perl, running the registry cleaner again. On 2nd cleaning, only one invalid entry remained - the one that was causing the problem (probably left over from a previous installation).

like image 708
Keith Bentrup Avatar asked Nov 08 '09 02:11

Keith Bentrup


People also ask

How do you pass an argument in Perl?

You can pass various arguments to a Perl subroutine like you do in any other programming language and they can be accessed inside the function using the special array @_. Thus the first argument to the function is in [0],thesecondisin_[1], and so on.

Which variable contains the command line arguments passed to a Perl script?

Perl command line arguments stored in the special array called @ARGV . The array @ARGV contains the command-line arguments intended for the script.

How do I start an interpreter in Perl?

On Win32 systems, if you double-click on the icon for the Perl executable, you'll find yourself in a command-prompt window, with a blinking cursor. You can enter your Perl commands, indicating the end of your input with CTRL-Z, and Perl will compile and execute your script.


1 Answers

I found out what the problem was. Although the ftype and the assoc values were set as suggested, the actual behavior on my system seems to be determined by the registry key

HKEY_CLASSES_ROOT\Applications\perl.exe\shell\open\command

It should have a (Default) string value of "C:\Perl\bin\perl.exe" "%1" %*

When I found this entry, it was set to "C:\Perl\bin\perl.exe" "%1". Changing it immediately fixed the problem.

Why it was set that way in the first place? I don't know. Maybe from a previous installation?

Anyway, thanks for the suggestions, and I hope this answer helps someone else, too.

like image 169
Keith Bentrup Avatar answered Oct 21 '22 05:10

Keith Bentrup