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

I can seem to be able to pass arguments to my perl script on a windows 7 machine. Microsoft is not help. Has anyone succeeded in being able to pass arguments?
Example: myperl.pl -p
My myperl.pl script does not receive any argument. I am sorry not to have provided more detail. I inserted the following at the very beginning of my code which showed me that I am not getting any arguments: my $cnt = @ARGV; print "Count=$cnt\n"; print "@ARGV\n"; exit; and I got the following when I run it: H:\>History-Reader.pl arg1 arg2 arg3 Count=0 I also get the following when issuing suggested commands: H:\>assoc .pl .pl=Perl H:\>ftype Perl Perl="C:\Perl\bin\perl.exe" "%1" %* I TOTALLY DO NOT LIKE WINDOWS, but I have no choice for this program. Does anyone have any other suggestions?

Replies are listed 'Best First'.
Re: Passing arguments
by ikegami (Patriarch) on Nov 04, 2010 at 19:04 UTC
    Sounds like your .pl association doesn't pass arguments. From a console, run
    assoc .pl ftype perlfile Replace "perlfile" with whatever .pl returned

    The ftype should look like

    "D:\Perl\bin\perl.exe" "%1" %*

    If not, fix it.

    ftype perlfile="D:\Perl\bin\perl.exe" "%1" %*

      Which is why real programmers don’t use Microsoft Windows.   ;-) ;-)

      “I never realized it could be so complicated to design a system that doesn’t work ...”   ;-) ;-)

      (All given merely in the gentle spirit of a little late-night fun...)   cat flames >/dev/null

        Unix GUIs do file associations too. This is actually a good feature. Whatever created the association is at fault here.
Re: Passing arguments
by kcott (Archbishop) on Nov 05, 2010 at 06:58 UTC

    If you run some very simple tests and then show the code and the output, you're likely to get a far more informative response. When you don't provide such information, we can only guess. Here's an example:

    C:\_\tmp>perl -e "print qq{@ARGV\n};" arg1 arg2 arg3 arg1 arg2 arg3 C:\_\tmp>perl test_args.pl arg1 arg2 arg3 arg1 arg2 arg3 C:\_\tmp>test_args.pl arg1 arg2 arg3 arg1 arg2 arg3 C:\_\tmp>type test_args.pl print qq{@ARGV\n}; C:\_\tmp>

    The above was run on Windows XP so it doesn't help troubleshoot a Windows 7 problem.

    -- Ken

Re: Passing arguments
by JavaFan (Canon) on Nov 04, 2010 at 18:53 UTC
    How did you deduce your program doesn't receive any arguments? Are you sure -p is actually passed to your Perl script, and not parsed by perl due to the way you start your script?
Re: Passing arguments
by cdarke (Prior) on Nov 05, 2010 at 13:49 UTC
    The file association is a common reason, but there is an issue with them on Windows 7. "Out of the box", the ASSOC command gives "Access is denied", even though the user is an Administrator.

    When this happened to me, my first thought was to turn off the dreaded User Account Control settings, but that changed nothing. The eventual solution was to go into regedit and grant my user full control on registry key HKEY_CLASSES_ROOT, which is where the file associations are held.
      I am sorry not to have provided more detail. I inserted the following at the very beginning of my code which showed me that I am not getting any arguments:

      my $cnt = @ARGV; print "Count=$cnt\n"; print "@ARGV\n"; exit;

      and I got the following when I run it:

      H:\>History-Reader.pl arg1 arg2 arg3
      Count=0

      I also get the following when issuing suggested commands:

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

      I TOTALLY DO NOT LIKE WINDOWS, but I have no choice for this program.

      Does anyone have any other suggestions?
        Interesting - If I run the script with "Perl" in front of the filename it works, but not if I run it via its association:
        H:\>Perl History-Reader.pl arg1 arg2 arg3
        Count=3
        arg1 arg2 arg3

        H:\>History-Reader.pl arg1 arg2 arg3
        Count=0

        How can I get it to work without having to prefix "Perl"?