(Coyote) Re: Reading command line flags into variables...?
by Coyote (Deacon) on Feb 01, 2001 at 06:56 UTC
|
I would recommend using the Getopt::Std module (which I believe comes with the standard distribution) to parse command line options. Here's some sample code:
use strict;
use warnings;
use Getopt::Std;
use vars qw($opt_n $opt_l);
getopts("n:l:");
print "Option -n: $opt_n \n";
print "Option -l: $opt_l \n";
__END__
./new_user.pl -n Bob -l Smith
Option -n: Bob
Option -l: Smith
----
Coyote | [reply] [Watch: Dir/Any] [d/l] |
Re: Reading command line flags into variables...?
by duct_tape (Hermit) on Feb 01, 2001 at 06:47 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Reading command line flags into variables...?
by runrig (Abbot) on Feb 01, 2001 at 08:31 UTC
|
Besides not caring about the order of arguments while using Getopt::Std
or Getopt::Long, those modules also convieniently shift the command line switches out
of @ARGV for you, in case you also want to provide
filenames for input to '<>', or in case you have other non-switch arguments on the command line.
And they automatically consider '--' an 'end of switches' marker (Just
like many unix commands),
in case one of your non-switch arguments starts with a '-'. | [reply] [Watch: Dir/Any] |
Re: Reading command line flags into variables...?
by Adam (Vicar) on Feb 01, 2001 at 07:28 UTC
|
While those modules do what you want, there is a learning curve involved, and they can some times be over kill. If all you need is to say, get the name a user then:
die "usage: script.pl firstName lastName\n" unless 2 == @ARGV;
my ( $firstName, $lastName ) = @ARGV;
Of course, if you want a fancier level of flag passing, such as optional parameters, then I recommend using one of the previously mentioned modules. I have rolled my own command line parser, and while it was fun, it was also a serious chunk of code. Don't go down that route. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: Reading command line flags into variables...?
by geektron (Curate) on Feb 01, 2001 at 07:08 UTC
|
for the example you give, GetOpt::Std works just fine.
if you want to expand the args to create, say:
new-user.pl --name=Bob --last=Smith
you could use GetOpt::Long. works about the same as GetOpt::Std, | [reply] [Watch: Dir/Any] [d/l] |
Re: Reading command line flags into variables...?
by rlk (Pilgrim) on Feb 01, 2001 at 11:31 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Reading command line flags into variables...?
by t0mas (Priest) on Feb 01, 2001 at 11:05 UTC
|
One option is to use the built in -s flag when you run perl. From perldoc perlrun
-s enables rudimentary switch parsing for switches on the comman
+d line
after the program name but before any filename arguments (or
+before
a --). Any switch found there is removed from @ARGV and sets
+the
corresponding variable in the Perl program. The following pro
+gram
prints "1" if the program is invoked with a -xyz switch, and
+"abc"
if it is invoked with -xyz=abc.
#!/usr/bin/perl -s
if ($xyz) { print "$xyz\n" }
/brother t0mas
| [reply] [Watch: Dir/Any] [d/l] |
Re: Reading command line flags into variables...?
by BoredByPolitics (Scribe) on Feb 02, 2001 at 02:04 UTC
|
Another solution, to complement all the suggestions you've had so far,
is to use AppConfig. It offers you the functionality of
both Getopt::Std and Getopt::Long, as well as supporting
the transparent use of configuration files.
Useful to consider, especially if there's any danger that the number
of commandline options your program needs will increase in the future.
HTH
Pete | [reply] [Watch: Dir/Any] |
Re: Reading command line flags into variables...?
by a (Friar) on Feb 01, 2001 at 12:43 UTC
|
Though Getopt::<etc> is a better idea, the old: use strict;
my ($name, $last, $arg);
while ($_ = shift ) {
last unless /^-\w/;
print "arg $_\n";
if ( /-n/ ) {
$name = shift;
}
if ( /-l/ ) {
$last = shift;
}
next if /-n|-l/;
die "bad arg: $_";
}
print "name $name, last $last\n";
er, or something like that. Its in the camel book
though.
a | [reply] [Watch: Dir/Any] [d/l] |