Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Handling Parameters

by jethro (Monsignor)
on Jan 07, 2009 at 17:07 UTC ( [id://734675]=note: print w/replies, xml ) Need Help??


in reply to Handling Parameters

The right answer is naturally to use one of the Getopt modules as the previous posters said. If you want to know a better way in detail, your algorithm is a good starting point, just change it slightly:

loop through @ARGV.
Check if the value starts with a "-"
if it does then figure out which flag it is and assign the correct variable with the next value in @ARGV. Step over both values
if the value doesn't start with a "-" and the previous value doesn't either, then take that as normal input and proceed with the script.
if the value doesn't start with a "-" but the previous value does, then treat it as the value for that flag

Instead of looping over the values you can shift them off the array, which makes the variable stepping much easier

Replies are listed 'Best First'.
Re^2: Handling Parameters
by wol (Hermit) on Jan 07, 2009 at 17:41 UTC
    Given the effort that's embodied in the Getopt modules, I'd really recommend using them unless you have some obscure requirement that they just can't meet. Re-inventing this wheel (a "better way"?) is well worth avoiding.

    Still interesting from an academic perspective. Would anyone care to comment on how the Getopt modules are implemented?

    --
    use JAPH;
    print JAPH::asString();

      "better way" than xorl's algorithm not "better way" than Getopt.

      Getopt::Std uses the algorithm I described, with shift.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-23 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found