Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: GetOpt::Long usage style

by mattriff (Chaplain)
on Mar 08, 2003 at 18:41 UTC ( [id://241417]=note: print w/replies, xml ) Need Help??


in reply to GetOpt::Long usage style

In the spirit of TIMTOWTDI...

I tend to just use a hash and be done with it:

my %opt; GetOptions(\%opt,'verbose','all');

Sure, $verbose is a few less characters than $opt{verbose}, but for me it's a nice way to separate the data that came from outside my script.

- Matt Riffle

Replies are listed 'Best First'.
Re: Re: GetOpt::Long usage style
by demerphq (Chancellor) on Mar 08, 2003 at 19:58 UTC

    Yeah I do use this style as well. However it still presents duplication when you want to provide defaults or multiple names or types for options (which rules out a quick key %opts), in fact its worse because you dont have compile time var name checking of the var names if they get out of synch. In other words the connection between destination and source becomes too loose and it becomes really easy to make a subtle error

    my %opt={verbose=>0,all=>0); GetOptions(\%opt,'talkative|verbose','everything|all');

    So what happens if we werent careful and do it like

    GetOptions(\%opt,'talkative|verbose','all|everything');

    Oops, now the --all options goes to the wrong place.

    Anyway, I still would use the %hash style as well for some situations, and as the interface to Getopt::Long is so flexible you can mix the two:

    GetOptions(\%opts,'Debug=i'=>\(my $Debug=0),'verbose','all');

    And then theres also using subs for the options... Mmm fun!


    ---
    demerphq


      Personally, I tend to keep thingsl ike synonyms in a data structure of their own. So, I might do something like:
      my %options = ( verbose => 0, everything => 1, ); my %synonyms = ( verbose => [ qw( talkative )], everything => [ qw( all )], ); GetOptions( \%options, ( map { join\('|', $_, @{ $synonyms{$_} || []}) } keys %options ), );
      This way you can, at a glance, see exactly what your synonyms are, laid out nice as can be. A similar thing can be done with the "=i", "=s", etc.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

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

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

    No recent polls found