CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Use of uninitialized value $ARGV[0]

 



pechkin03
Novice

Mar 23, 2009, 1:12 PM

Post #1 of 19 (3299 views)
Use of uninitialized value $ARGV[0] Can't Post

I have a problem trying to use @ARGV. Can someone help please.

My code is below:

C:\how_to>type ex7.plx
#!/usr/bin/perl
use warnings;
print $ARGV[0];


C:\how_to>ex7.plx
Use of uninitialized value $ARGV[0] in print at C:\how_to\ex7.plx line 3.

C:\how_to>


ItecKid
Novice

Mar 23, 2009, 1:20 PM

Post #2 of 19 (3297 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

@ARGV is the array that holds command line arguments. However, unlike other languages such as C, the program name is not the first index of the argv array. In Perl, this value is stored in the $0 variable.

Thus, @ARGV is uninitialised in your program because you don't run it with command-line arguments. If you run it like this...


Code
./file.pl file1.txt file2.txt


Then $0 is file.pl, $ARGV[0] is file1.txt, and $ARGV[1] is file2.txt.


KevinR
Veteran


Mar 23, 2009, 1:29 PM

Post #3 of 19 (3293 views)
Re: [ItecKid] Use of uninitialized value $ARGV[0] [In reply to] Can't Post


In Reply To
@ARGV is the array that holds command line arguments. However, unlike other languages such as C, the program name is not the first index of the argv array. In Perl, this value is stored in the $0 variable.

Thus, @ARGV is uninitialised in your program because you don't run it with command-line arguments. If you run it like this...


Code
 
./file.pl file1.txt file2.txt


Then $0 is file.pl, $ARGV[0] is file1.txt, and $ARGV[1] is file2.txt.


Exactly Smile
-------------------------------------------------


(This post was edited by KevinR on Mar 23, 2009, 1:30 PM)


pechkin03
Novice

Mar 23, 2009, 1:54 PM

Post #4 of 19 (3278 views)
Re: [KevinR] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Thank you guys!

Now I have name of the perl script.

But I still can not get arguments....

C:\how_to>type ex7.plx
#!/usr/bin/perl
use warnings;
print '$0='."$0\n";
print $ARGV[0];
print $ARGV[1];
print $ARGV[2];

C:\how_to>ex7.plx one two three
$0=C:\how_to\ex7.plx
Use of uninitialized value $ARGV[0] in print at C:\how_to\ex7.plx line 4.
Use of uninitialized value $ARGV[1] in print at C:\how_to\ex7.plx line 5.
Use of uninitialized value $ARGV[2] in print at C:\how_to\ex7.plx line 6.

C:\how_to>


ItecKid
Novice

Mar 23, 2009, 2:02 PM

Post #5 of 19 (3275 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Hmm...

Try this:


Code
#! /usr/bin/perl 
use warnings;
use strict;

my @files = @ARGV;
print $files[0];


And see what the output is.


pechkin03
Novice

Mar 23, 2009, 2:07 PM

Post #6 of 19 (3270 views)
Re: [ItecKid] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Still no luck...

C:\how_to>type ex8.plx
#! /usr/bin/perl
use warnings;
use strict;
my @files = @ARGV;
print $files[0];

C:\how_to>ex8.plx one two three
Use of uninitialized value $files[0] in print at C:\how_to\ex8.plx line 5.

C:\how_to>


KevinR
Veteran


Mar 23, 2009, 2:12 PM

Post #7 of 19 (3265 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Are you trying to get input into a CGI script?
-------------------------------------------------


pechkin03
Novice

Mar 23, 2009, 2:15 PM

Post #8 of 19 (3262 views)
Re: [KevinR] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

No. I need to call Oracle sqlload based on parametes being passed to perl program.


KevinR
Veteran


Mar 23, 2009, 2:19 PM

Post #9 of 19 (3258 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Your example code should work.
You have .plx extension associated with your perl executable, is there anything in the file association that is blocking input from the command line?
-------------------------------------------------


ItecKid
Novice

Mar 23, 2009, 2:22 PM

Post #10 of 19 (3256 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Ok!

After running several tests, I now know what your problem is.

Your shebang line is wrong. Whenever you try to use Perl's built in variables, you need to tell the interpreter where to find the Perl files. Try changing your shebang to either of these:


Code
#! /perl/bin/perl


or


Code
#! /usr/bin/env perl



KevinR
Veteran


Mar 23, 2009, 2:28 PM

Post #11 of 19 (3254 views)
Re: [ItecKid] Use of uninitialized value $ARGV[0] [In reply to] Can't Post


In Reply To
Ok!

After running several tests, I now know what your problem is.

Your shebang line is wrong. Whenever you try to use Perl's built in variables, you need to tell the interpreter where to find the Perl files. Try changing your shebang to either of these:


Code
#! /perl/bin/perl


or


Code
#! /usr/bin/env perl



Windows ignores the shebang line as fas as finding perl, otherwise his script would not even be running unless he has that directory structure setup on his PC. An HTTP server might need it but than if the shebang line were wrong it would error out right away and return nothing. It could be wrong somehow in the file association though.
-------------------------------------------------


pechkin03
Novice

Mar 23, 2009, 2:37 PM

Post #12 of 19 (3249 views)
Re: [KevinR] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

I just noticed that I can not asociate ".pl" extention on my machine with perl.

All others like ".plx" or ".pl_test" work fine.

Is this where the problem starts ?


KevinR
Veteran


Mar 23, 2009, 2:41 PM

Post #13 of 19 (3246 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

It might be the source of the problem but I have little experience with the file association side of things. Can you look at the file association and see what it looks like?
-------------------------------------------------


pechkin03
Novice

Mar 23, 2009, 2:56 PM

Post #14 of 19 (3242 views)
Re: [KevinR] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

When I save a file , right-mouse click --> Open with window --> Choose Program

I select Perl Command Line Interpreter. File Icon is being changed to the same icon as Perl Command Line Interpreter from "Open with window".

This happens for all the files but not for ".pl". Those ones are being associated with Notepad.


KevinR
Veteran


Mar 23, 2009, 4:13 PM

Post #15 of 19 (3238 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

You may need to look at the actual file association using the File Manager / Windows Explorer or whatever it might be called for your version of Windows. Like I said, I have little experience with this part of the problem. You might try running a perl program striaght from the perl executable and see if that works:

c:\perl name.pl one two three

where perl is the path to perl and name.pl is the name of a perl program, supply full paths as needed
-------------------------------------------------


FishMonger
Veteran / Moderator

Mar 23, 2009, 9:16 PM

Post #16 of 19 (3229 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

When you fix the file type association it show look like this:

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

You may need to alter the path according to your installation.


pechkin03
Novice

Mar 24, 2009, 7:19 AM

Post #17 of 19 (3221 views)
Re: [FishMonger] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Guys,

Thank you so much for your help!

It does not work this way: "program_name" "arg1" "arg2" "arg3"

but works if I call it "perl" "program_name" "arg1" "arg2" "arg3"

C:\how_to>type ex7.plx
#!/usr/bin/perl
use warnings;
print '$0='."$0\n";

print $ARGV[0];
print $ARGV[1];
print $ARGV[2];

C:\how_to>perl ex7.plx one two three
$0=ex7.plx
onetwothree
C:\how_to>



What is C:\Perl\bin\perl.exe "%1" %* is it need to be added to windows Path variable or to registries ?


FishMonger
Veteran / Moderator

Mar 24, 2009, 7:43 AM

Post #18 of 19 (3218 views)
Re: [pechkin03] Use of uninitialized value $ARGV[0] [In reply to] Can't Post


Quote
What is C:\Perl\bin\perl.exe "%1" %*

That's what is put in the file type association for the .pl extension.

Open Windows Explorer
Click on Tools -> Folder Options -> File Types
Scroll down and select the PL extension
Click on Advanced
Select the Open action then click on Edit
Copy "C:\Perl\bin\perl.exe" "%1" %* into the dialog box for "Application used to preform this action".
Click on OK in each of the dialog boxes.


pechkin03
Novice

Mar 24, 2009, 8:09 PM

Post #19 of 19 (3200 views)
Re: [FishMonger] Use of uninitialized value $ARGV[0] [In reply to] Can't Post

Thank you!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives