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:
How can I use @ARGV

 



digioleg54
User

Aug 24, 2016, 10:21 AM

Post #1 of 10 (2774 views)
How can I use @ARGV Can't Post

I am trying to read arguments from command line, this way:

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


my $file = $ARGV[1];
print "$file\n";
open (FL,"<$file") or die "Could not open file $file $!\n";

while(<FL>){
print "$_";
}


My command line:

perl readfile.pl file.txt

it always show the errors:

Use of uninitialized value $file in concatenation (.) or string at readfile.pl line 7.
Use of uninitialized value $file in concatenation (.) or string at readfile.pl line 7.
Could not open file ,No such file or directory at readfile.pl line 7.


file.txt created on the same directory as my script.
Version:

This is perl 5, version 22, subversion 2 (v5.22.2) built for darwin-2level

Can somebody explain me , why?


FishMonger
Veteran / Moderator

Aug 24, 2016, 1:40 PM

Post #2 of 10 (2768 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post

Arrays are 0 indexed. The first element is at index 0 not 1.

Code
my $file = $ARGV[0];

or

Code
my $file = shift @ARGV;



(This post was edited by FishMonger on Aug 24, 2016, 1:41 PM)


digioleg54
User

Aug 24, 2016, 1:51 PM

Post #3 of 10 (2764 views)
Re: [FishMonger] How can I use @ARGV [In reply to] Can't Post

I know that, 0 element is the program name, the second is file name, that is why I made it ARGV[1]. I need the file name;


FishMonger
Veteran / Moderator

Aug 24, 2016, 1:56 PM

Post #4 of 10 (2761 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post


In Reply To
I know that, 0 element is the program name, the second is file name, that is why I made it ARGV[1]. I need the file name;


WRONG


Code
c:\test>type test.pl 
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

print Dumper \@ARGV;


Code
c:\test>perl test.pl file.txt 
$VAR1 = [
'file.txt'
];



(This post was edited by FishMonger on Aug 24, 2016, 1:56 PM)


digioleg54
User

Aug 24, 2016, 1:59 PM

Post #5 of 10 (2758 views)
Re: [FishMonger] How can I use @ARGV [In reply to] Can't Post

I know it, unfortunately DUMPER is not working, I don't know why. Should it be installed separate from Perl version?


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:00 PM

Post #6 of 10 (2757 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post

The program name is in $0 not @ARGV


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:01 PM

Post #7 of 10 (2756 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post

Data::Dumper is a core module that comes with perl. If you don't have it, then your perl installation is broken.


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:07 PM

Post #8 of 10 (2752 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post

Here's the example with using the Data::Dumper module.

Code
c:\test>type test.pl 
#!/usr/bin/perl

use strict;
use warnings;

print "Program name is: $0\n";
print "The first arg passed to the script is: $ARGV[0]\n";


Code
c:\test>perl test.pl file.txt 
Program name is: test.pl
The first arg passed to the script is: file.txt



digioleg54
User

Aug 24, 2016, 2:08 PM

Post #9 of 10 (2750 views)
Re: [FishMonger] How can I use @ARGV [In reply to] Can't Post

Strange, now everything is working, Dumper also.
OK,
Thanks


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:11 PM

Post #10 of 10 (2747 views)
Re: [digioleg54] How can I use @ARGV [In reply to] Can't Post

It doesn't appear that you understand the minimum basics of perl. You should get a beginner level book and do some reading.

The best one available is "Learning Perl, 6th Edition"
http://shop.oreilly.com/product/0636920018452.do

 
 


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

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