Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

I have in my program a number of parameters that can be set by the user, which I store in a hash %par. Now the thing is, some of these parameters come in more and less specific versions.

An example: We have a parameter "margin-left". In some cases, the user hasn't set a specific value for it; it then defers to the more general "margin-horizontal". If the user hasn't set a specific value for that, it in turn defers to the more general "margin".

In my current program, those "unspecified" parameters are set to "a" (for "automatic"). So when the time comes to actually draw the left margin, the program does something like

if($par{'margin-left'} ne 'a'){ $actual = $par{'margin-left'} }elsif($par{'margin-horizontal'} ne 'a'){ $actual = $par{'margin-horizontal'} } else{$actual = $par{'margin'}}

As you can see, it's a bit of a bunch every time I just need to read the margin-left value. I could of course pack all that into a "get" sub, that would save on typing, but it still seems like a slow and clumsy method.

There is a "set" sub, so in principle I could use that to track and update everything, checking whenever a parameter is set whether it has any parent / dependant parameters, but that's also quite a lot of code, and it's redundant for all the parameters that don't have any such behaviour.

Is there a better way? Maybe something with referencing / aliasing? Ideally keeping all the inner working in the "set" sub, so I can just use $par{'margin-left'} and get the right thing.


In reply to 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 scrutinizing the Monastery: (6)
As of 2022-12-02 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?