ronniec has asked for the wisdom of the Perl Monks concerning the following question:

Hello there monks I'm trying to convert a script that used to use vars/Getopts::Std (I still don't know why this works on 1 PC and not another, especially when I did exactly the same install of Perl 5.8.7 Build 813 on both PC's!!!) to use command line parameters. I don't seem, using my usual method, to be able to pick up any parameters. Is there a different method employed in Win32 Perl from that used on a Unix server? An example of the code follows :-
# # Process the parameters #------------------------# $P_count = scalar @ARGV ; if ($P_count == 1) { $file_in = $ARGV[0] ; } elsif ($P_count == 2) { ($IP_in, $MAC_in) = @ARGV ; } elsif ($P_count == 3) { ($IP_in, $MAC_in, $Port_in) = @ARGV ; } else { print "\n\tIncorrect number of parameters supplied - ($P_count)" ; print "\n\tThere MUST be between 1 & 3 parameters!\n" ; exit 123 ; }
On the command line I type the script name and the parameters and my counter always says that there are 0 supplied! eg script.pl(fred), script.pl fred & script "fred" all produce 0 parameters? Is there a difference or have I got a typo or just plain misunderstood something?

Replies are listed 'Best First'.
Re: Can't pass parameters in Win32 Perl Scripts
by Corion (Patriarch) on Oct 25, 2006 at 11:17 UTC

    Invocation through filename associations doesn't work in my experience. I always launch my scripts by using

    perl -w scriptname.pl

    or convert them into executable form by using pl2bat.cmd.

      Thanks that did the trick!! It may not be as neat as the Getopt:Std version but it does work!! Cheers!
      PS I loaded Perl onto yet another machine and yes the original script works there! (Only my office PC causes it to fall through!?)

        Assuming all other factors are equal, what is ".pl" associated with on you office PC? What is it associated with on the other PCs?

        BTW for extra fun try getting a shortcut to a perl program running with parameters specified in the shortcut... I've only ever got this to work via a batch file...

        Tom Melly, tom@tomandlu.co.uk
      Invocation through filename associations doesn't work in my experience.

      Years ago I had one NT4-Box at work with the same symptoms and have also seen the same problem reported a few times in Perl fora over the years.

      Apart from that, parameter passing to calls using extension association has never been a problem for me again (using it all the time.)

      If anyone knows how to *repair* such an incident, I would be interested to hear about it. If only to satisfy my curiosity.

        One idea I have is, that possibly the filetype isn't set up correctly by whatever installs the association:

        Q:\>ftype perl perl="C:\Programme\perl\bin\perl.exe" "%1" %* Q:\>type tmp.pl #!/usr/bin/perl -l print for @ARGV; Q:\>tmp.pl 1 2 3 4 5 1 2 3 4 5

        So on this machine, with this setup, it works. If the association is set up wrongly (which I can't check as I don't have access to a non-working machine at the moment), for example by double-clicking a .pl file and then choosing perl.exe, then likely the execution from the command line doesn't work either:

        Hypothetical:\>ftype perl perl="C:\Programme\perl\bin\perl.exe" "%1"