Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to script, getting: "Use of uninitialized value $ARGV[0]"

Tags:

perl

argv

Problem: Windows XP is not passing command line arguments to perl scripts.

Symptom: a simple command like:

say "Argument 1 (\$ARGV[0]) is: $ARGV[0], argument 2 (\$ARGV[1]) is: $ARGV[1].";

Resulted in:

Use of uninitialized value $ARGV[0] in concatenation (.) or string at... 

Solution:

The root problem is in Windows XP. The default method for starting perl passes only the first variable, which is the script name. Result is that $ARGV[0] is uninitialized.

The fix is to edit the Windows registry at:

\HKEY_CLASSES_ROOT\Perl\shell\Open\command

And make the entry:

"C:\Perl\bin\perl.exe"  %*

Result is:

C:\whatever>perl argtest.pl 1 2
Argument 1 ($ARGV[0]) is: 1, argument 2 ($ARGV[1]) is: 2.

Thanks especially to David W who pointed me in the right direction.

like image 810
user2246544 Avatar asked Oct 04 '22 12:10

user2246544


1 Answers

Note that @ARGV in Perl is not quite like argv in C.

                              C          Perl

Name of the program        argv[0]        $0
1st argument               argv[1]      $ARGV[0]
2nd argument               argv[2]      $ARGV[1]
n-th argument              argv[n]      $ARGV[n-1]

So if you provide one command line arguments to a Perl script, it will be found in $ARGV[0]. $ARGV[1] will be uninitialized.

like image 83
mob Avatar answered Oct 10 '22 01:10

mob