I'd Like to Have an Argument... in Perl!
Arguments are the values you pass to a Perl script. Each value on the command line after the name of the script will be assigned to the special variables $ARGV[0], $ARGV[1],$ARGV[2], and so on. The number of arguments passed to the script is stored in the $#ARGV variable, and the full argument string is in the variable @ARGV. The name of the currently running program is stored in the $0 variable. Let's try some examples working with arguments and other special variables. Create an executable script called testvars.pl containing these lines:
#!/usr/bin/perl
print "My name is $0 \n";
print "First arg is: $ARGV[0] \n";
print "Second arg is: $ARGV[1]
\n";
print "Third arg is: $ARGV[2] \n";
$num = $#ARGV + 1; print "How many args? $num \n";
print "The full argument string was: @ARGV \n";
Now if you run this script, here's what you'll see:
$ ./testvars.pl dogs can whistle
My name is testvars.pl
First arg is:
dogs
Second arg is: can
Third arg is: whistle
How many args? 3
The full argument string was: dogs can whistle
Just a few notes about that example. I did say that the $#ARGV variable contained the number of arguments, but I lied--sort of. Since the arguments are numbered starting at zero, you have to add one to the value of $#ARGV to get the actual number of arguments. It's a bit weird, but if you're a fan of the C language, it'll all seem quite normal.
Also note that the @ARGV variable doesn't start with a dollar sign. That's because it's an array variable, as opposed to the regular scalar variables we've worked with so far. An array can be thought of as a list of values, where each value is addressed by a scalar (dollar sign) variable and an index number in square brackets, as in $ARGV[0],$ARGV[1], and so on. Don't worry too much about arrays for now--that's a topic for more study on your own.
Previous Lesson: Perl Variables
Next Lesson: Perl Logic
Comments - most recent first
(Please feel free to answer questions posted by others!)
if ( $protein =~ /^[BJOUXZ]/ )
if ( $protein =~ /[^BJOUXZ]/ ) {
print "error\n";
} else {
print "good\n";
}
use strict;
in your program? If so, that's the reason for that error (undeclared variables). You can declare the variable like this:
my $num = $#ARGV + 1;
Or turn off strict as follows:
no strict;
$num = $#ARGV + 1;
print "How many args? $num \n";
it tells me: Global symbol "$num" requires explicit package name at ./oneargument line 12.
and i have no idea what that means.
the number is not 1 it prints an error message and exits ?? and on top of that it assigns the first argument to one variable.??
should be ./testvars.pl
Just to inform that $#ARGV is giving one number bcoz it is not treating pl script as argument.
BR,
KM
just wanted to say thanks for this site. it's simple & understandable yet also challenging. perfect for a n00b like me! keep up the good work.
I have few programs of perl, & m unable to run in perl with linux operating sys.
1) Actually, I have makefile.pl,ngrams.pl & ngrams.pm.which is the executable file n how it will give output.
2) Another thing should we save it in desktop N mention the path while execution????
Waiting for ur reply..
THANKS IN ADVANCE
unexpected error near token ...
why?
BR,
FR
I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But
don't ask me to send what you already have. :-) NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

