I would like to pass in some variables via the command line. For example,
I want to be able to run the program the following way.
perl program.pl -u my_username -p my_password
Where the -u switch signifies the following string is the user name and
the -p switch is for their password. Can you help me on how to do this?
it is simple. The command line parameters are in the array @_ thus
$_ is '-u'
$_ is 'my_username'
and so on
Thanks for the quick answer. But what about if I have a bunch of
variables to pass in and I don't want to let the user pass it in any order
on the command line.
perl program.pl -u username -p password -O org_id -G group_id
say I want the user to be able to type perl program.pl -u username -O
org_id -p password -G group_id
This does not make any difference. The array @_ will contain the command
line parameters in the order the user specifies the parameters.
All you need in a small loop that checks them. For example:
$i = 0;
$password = $_[$i+1] if $_[$i] eq '-p';
$username = $_[$i+1] if $_[$i] eq '-u';
... and so on ...
$i += 2;
[ back to toc ]