Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello Chuma,

> number of parameters that can be set by the user

maybe I'm confused by your post but, if I understand it correctly, you are handling user provided params, if so I'd go totally other way: set defaults, overwrite them with user provided values, validate evrything with your own logic.

Then in the rest of your program you can safely use just one variable with the right name because it was a default or user provided or set by you following your logic

use 5.0010; use strict; use warnings; use Data::Dumper; use Getopt::Long; # provide sane defaults my %defaults = ( 'margin-left' => undef, 'margin-horizontal' => undef, 'margin' => 5, ); # use then user provided parameters unless ( GetOptions ( 'margin-left=i' => \$defaults{'margin +-left'}, 'margin-horizontal=i' => \$defaults{'margin +-horizontal'}, 'margin=i' => \$defaults{'margin +'}, )) {die "$0 usage..."} # validate them once for all validate_parameters(); print Dumper \%defaults; sub validate_parameters{ # here the logic of precedence for all paramenters $defaults{'margin-left'} //= $defaults{'margin-horizontal'} // $de +faults{'margin'}; } __END__ io@COMP:C>perl params.pl $VAR1 = { 'margin-horizontal' => undef, 'margin' => 5, 'margin-left' => 5 }; io@COMP:C>perl params.pl --margin-left 33 $VAR1 = { 'margin-horizontal' => undef, 'margin-left' => 33, 'margin' => 5 }; io@COMP:C>perl params.pl --margin-horizontal 4242 $VAR1 = { 'margin-horizontal' => 4242, 'margin-left' => 4242, 'margin' => 5 };

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Deferring variables -- Getopt::Long by Discipulus
in thread Deferring variables by Chuma

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? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-12-02 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?