Home: Perl Programming Help: Beginner:
How can I use @ARGV



digioleg54
User

Aug 24, 2016, 10:21 AM


Views: 4485
How can I use @ARGV

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


Views: 4479
Re: [digioleg54] How can I use @ARGV

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


Views: 4475
Re: [FishMonger] How can I use @ARGV

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


Views: 4472
Re: [digioleg54] How can I use @ARGV


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


Views: 4469
Re: [FishMonger] How can I use @ARGV

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


Views: 4468
Re: [digioleg54] How can I use @ARGV

The program name is in $0 not @ARGV


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:01 PM


Views: 4467
Re: [digioleg54] How can I use @ARGV

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


Views: 4463
Re: [digioleg54] How can I use @ARGV

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


Views: 4461
Re: [FishMonger] How can I use @ARGV

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


FishMonger
Veteran / Moderator

Aug 24, 2016, 2:11 PM


Views: 4458
Re: [digioleg54] How can I use @ARGV

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