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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Getopt::Long is a basic part of the Perl toolset. However one minor nit that Ive had up to now is that specifying the arguments and their destinations, along with defaults seemed a bit clumsy under strict. Take an example from the documentation

use GetOpt::Long; my $verbose = ''; # option variable with default value (false) my $all = ''; # option variable with default value (false) GetOptions ('verbose' => \$verbose, 'all' => \$all);

To me this is inelegant. I have two "declarations" about a variable in separate locations, one the real declaration along with the default value, and the other the association of the option name (which may be different from the variable name) to the variable. If I want to rename a variable for clarity later on I have to change two items. Now perhaps this is blindingly obvious to the rest of you but I just realized that you can instead write this

use GetOpt::Long; #Adjust bractes and commas to taste GetOptions ( 'verbose' => \(my $verbose = ''), 'all' => \(my $all = ''), );

Which to me seems preferable. Everything is in one place. It says "this variable normally comes from outside", it puts the declaration of the variable next to the declaration of the option handling it is associated with, and it provides a way to document the whole lot in one place if you need to.

Any thoughts?


---
demerphq



In reply to GetOpt::Long usage style by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found