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

Perl (v5.16.3) works fine on my older Win7 Gateway. But on a new Win8 Gateway (same Perl release), it seems that @ARGV does not work.

use strict; my $numargs = @ARGV; print "numargs = $numargs\n"; if ( $numargs > 0 ) { for my $a ( @ARGV ) { print " $a\n"; } }

Running this with the command line

testarg a b c

produces the result

numargs = 0

I added tests to print @INC, %ENV, and a list of my own and everything else is fine. It's just @ARGV that does not work. Any ideas? I understand that this could well be a Windows issue, rather than Perl. But other command-line programs work fine.

Replies are listed 'Best First'.
Re: Why doesn't @ARGV work?
by NetWallah (Canon) on Jul 17, 2014 at 04:23 UTC
    Check your associations - when you run a perl script from Windows, it is usually associated with pl2bat (I have not used Windows in a while - the bat file name may have changed) - which is supposed to pass in arguments thus:
    perl -w scriptname.pl "%1" "%2" "%3" "%4" "%5" "%6" "%7" "%8" "%9"
    It is likely that in your windows 8 case, the arguments %* are missing in the bat file that invokes the script.

    To avoid this, Try running your script like this:

    perl testarg a b c
    and post your results.

            Profanity is the one language all programmers know best.

Re: Why doesn't @ARGV work?
by Your Mother (Archbishop) on Jul 17, 2014 at 04:11 UTC

    This is what I get with your code on OS X–

    moo@cow[387]~/>perl pm-1093976 3 2 1 numargs = 3 3 2 1

    You sure you’re calling the right script? I don’t think Win differs that much here…

Re: Why doesn't @ARGV work?
by Anonymous Monk on Jul 17, 2014 at 07:48 UTC
Re: Why doesn't @ARGV work?
by gurpreetsingh13 (Scribe) on Jul 17, 2014 at 10:04 UTC
    Same output for me too.
    numargs = 3 a b c
    Checked on windows xp command line, checked on cygwin bash shell, checked on unix box.
    I believe you need to check exactly which perl installation it is picking up from windows path or using perl -v (version checker) or better go to the exact installation directory on cmd and run the script from that perl.exe to see the output
Re: Why doesn't @ARGV work?
by bulrush (Scribe) on Jul 17, 2014 at 11:53 UTC
    This is how I would do it:
    use strict;
    # @ARGV is an array, so number of args in an array is $#ARGV+1
    # The last element position is $#ARGV.
    # Also I like to line up brackets and braces. 
    my $numargs = $#ARGV+1;
    print "numargs = $numargs\n";
    if ( $numargs > 0 )
    { for my $a ( @ARGV ) # $a gets assigned to each item in @ARGV.
       { print "  $a\n"; } 
    }
    
    Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)

      Thanks for all the inputs. The first thing I did was to download a fresh copy of the ActiveState .msi. I tried "repair" and when that didn't help, I tried removing the old install and reinstall (all using the ActiveState installer).

      The association seems to be OK (because it does run the correct script). I tried adding some other things to the script, defining my own @mylist = (1,2,3);, printing @INC and %ENV. All of that worked fine. There were a couple of other suggestions relating to assoc. I will try those, but that does not seem to be the issue.

        Now, I'm really confused !!!

        I just set up another new Gateway, identical to the Win8 system I was using yesterday and installed Perl as before. I get the same result as yesterday:

        numargs = 0

        Obviously, the correct little Perl script is running. At the moment, I just shut that machine off, and I will try the suggestion of using $#ARGV-1 instead of @ARGV. But I would not expect that to make any difference.

        This result seems to rule out any issues with the op sys on the other machine. I opened a standard CMD window, typed the program using Notepad, and ran it. So there was nothing else installed, otherwise a virgin machine.

        Has anybody else has any issues with Win8?