LINUX CLASSES - PERL PROGRAMMING
I'd Like to Have an Argument... in Perl!
Arguments and Other Special Variables
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
[ RETURN TO INDEX ]
Comments - most recent first (Please feel free to answer questions posted by others!)
sun ( 09 Feb 2012, 15:10)
$service = SOAP::Lite->proxy(
sun ( 09 Feb 2012, 15:09)
use SOAP::Lite;
sun ( 09 Feb 2012, 15:07)
Dear Dr. Bob I have to call the needel web service and I wrote like this
can you tell me where I got wrong?
Sun ( 02 Feb 2012, 14:27)
Hello Dr. Bob, my friend Alex directed me to your site. I am working on a
bioinformatic program in Perl. I need to call a web service: needle and put
two inputs on my command line. I have tried in many ways, but I cannot get
it to work. Could you please, please, PLEASE help me.
Bob Rankin ( 31 Jan 2012, 11:11)
@Mavi - If you're trying to match the first letter in your example, move
the caret (^) outside the brackets like this:
if ( $protein =~ /^[BJOUXZ]/ )
mavi ( 31 Jan 2012, 10:38)
HI BOB! I am trying to do a pattern match. My goal is to give an error
message when the input sequence has characters different from the 20 common
amino acids. I tried at first to put all the 20 amino acids in, but it
wouldn't work! So I tried this way, but it still doesn't. What did I do
wrong?!?!
if ( $protein =~ /[^BJOUXZ]/ ) {
print "error\n";
} else {
print "good\n";
}
alex ( 30 Jan 2012, 15:48)
Thanks a lot for your help, I had forgotten about that! I'll try it out
tomorrow. I started learning linux yesterday.
Bob Rankin ( 30 Jan 2012, 12:13)
@alex - Did you include
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;
alex ( 30 Jan 2012, 11:46)
and when i type:
$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.
Alex ( 30 Jan 2012, 11:43)
i got how to check the number of arguments, but how do i make it print an
error message if the number of arguments is not 1? and i don't understand
how to assign the first argument to one variable?
Alex ( 30 Jan 2012, 11:34)
Hi! how do i check the number of arguments received on the command line and
if
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.??
Bob Rankin ( 20 Jan 2012, 10:06)
@water - Thanks, fixed now!
water ( 20 Jan 2012, 09:43)
./testvars dogs can whistle
should be ./testvars.pl
TOMSON K V,IIFL ( 21 Nov 2011, 23:52)
Can I combine linux command and perl command in one script,also how it
run,?
Tomson ( 21 Nov 2011, 11:44)
It is very useful for me really,thanks
Muralikrishnan ( 08 Nov 2011, 04:18)
could you please teach setp by step screpting
Reji ( 02 Nov 2011, 03:28)
Great Explanation.,Thanks alot :)
nitha ( 11 Oct 2011, 13:30)
i am new to Perl. i have to do Perl program in Linux(ubuntu), first i need
to know all the basic command like opening file, closing, saving program
and running the program. Plz help me learn Perl program
karthik ( 22 Sep 2011, 06:40)
its really useful for me.. thanks lot
Kulbhushan Mayer ( 01 May 2011, 08:08)
Hi,
Just to inform that $#ARGV is giving one number bcoz it is not treating pl
script as argument.
BR,
KM
Stefan ( 13 Jan 2011, 10:52)
Big help, thx
barehide ( 05 Dec 2010, 16:42)
Hey Dr. Bob-
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.
Mousmi ( 03 Nov 2010, 02:03)
Hello Sir,
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
at ( 01 Oct 2010, 04:19)
can i combine linux command and perl commands in one script? can i run that
script with command sh filename?
at ( 01 Oct 2010, 04:16)
i am receiving an error while runnig perl script.
unexpected error near token ...
why?
Fernando ( 04 Mar 2010, 06:32)
To specify the script name you can also use __FILE__
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 Bob Rankin
- Privacy Policy
All rights reserved - Redistribution is allowed only with permission.