Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reading command line flags into variables...?

by Anonymous Monk
on Feb 01, 2001 at 06:34 UTC ( [id://55629]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to input some varibles and i was asked to do it by command line, so in other words it has to look like:

./new_user.pl -n Bob -l Smith

Where the value you type in after -n is stored into the varible $Name and the value after -l is Stored in $lastname

I have been reading many a perl book and man page but nothing seems to answer this clearly. All I know is that @ARGV reads things off the command line and i should be using a foreach loop of some sort, but i am really just confused and want a simple (although in-depth) answer.

Any help would be greatly appreciated =)

Replies are listed 'Best First'.
(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

Re: Reading command line flags into variables...?
by duct_tape (Hermit) on Feb 01, 2001 at 06:47 UTC
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 '-'.

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.
      In the example above, $firstName == '-n' and $lastName == 'Bob'. The command line flags are elements in @ARGV. Getopt::Std takes care of this for you. Also, if you are parsing the command line options by hand, you have to be aware of the order which the arguments were passed to the script.

      Update: I didn't read Adam's post closely enough to notice that he was not using command line flags. My apologies.

      ----
      Coyote

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,
Re: Reading command line flags into variables...?
by rlk (Pilgrim) on Feb 01, 2001 at 11:31 UTC
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
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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://55629]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found