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

world's simplest program:

print "\n\$ARGV[0] = $ARGV[0]\n";

I invoke it with this command:

argstest.pl Hello

it says I'm trying to use uninitialized value $ARGV[0]. I've been writing Perl for years. I use command line args all the time. But this is a new build on a new computer. The old build on the old computer still works fine. This is driving me nuts!

New build is 64-bit Perl28 from ActiveState.

Replies are listed 'Best First'.
Re: command line args
by pryrt (Abbot) on Jul 24, 2019 at 19:11 UTC
    argstest.pl Hello ... New build is 64-bit Perl28 from ActiveState.

    Check your file association for perl*. My guess is it's just running c:\path\to\perl.exe "%1". If that's the case, then none of your other command-line arguments are being passed to argtest.pl, hence the ARGV is undefined. You want the association to be something like c:\path\to\perl.exe "%1" %*, where the %* passes the remaining command-line arguments to the script.

    FOOTNOTE *: sometimes assoc .pl then ftype perl_auto (replace perl_auto with the results of the assoc). If not there, run regedit, then search either HKEY_CLASSES_ROOT\.pl or HKEY_CURRENT_USER\Software\Classes\.pl, look in the "(Default)" item, which will tell the name of the class (might be perl_auto; on my system, since i manually associated, it's called PerlScript); then at the same level as .pl, search for a key (folder) with that name, then dig into the ...\shell\xxx\command\(DEFAULT), where xxx will be "run" or "open" or some such.

    update: haukex's suggestion of "Have you tried running perl argtest.pl Hello" would lend further strength: if that command works, but the auto-associated run does not, it's almost certainly the file association.

Re: command line args
by haukex (Archbishop) on Jul 24, 2019 at 18:55 UTC

    That code should work fine no matter the Perl version. Since you're getting a warning, I doubt that is your full test script - can you show an SSCCE? I suspect there is a difference between your two machines and/or in how Perl is set up. What's the Perl version on your old machine? What are the Windows versions of both machines, and are you using cmd.exe on both? Have you tried running perl argtest.pl Hello?

Re: command line args
by Anonymous Monk on Jul 28, 2019 at 20:23 UTC

    I must thank haukex and pryrt for excellent help. The line was missing %* at the end. I added it in with regedit. All works fine now. Thank you thank you.

Re: command line args
by Anonymous Monk on Jul 25, 2019 at 22:07 UTC

    Here's the entire argstest pgm, all 5 lines:,

    use 5.012; use strict; use warnings; #use diagnostics; print "\n\$ARGV[0] = $ARGV[0]\n";

    This works fine:

    perl argstest.pl Xxx

    assoc .pl says file extension not found. regedit says .pl default is pl_auto_file and then for that it says

    "C:\Users\Thomas\perl\Perl64\bin\perl.exe" "%1"

    I don't recall seeing

    %*

    at the end of that. Will check again.

    The old machine runs Perl5.12. The new one Perl28.

      help ftype tells you
      ASSOC .pl=PerlScript FTYPE PerlScript=perl.exe %1 %*