Chuma has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Deferring variables
by Athanasius (Archbishop) on Jun 25, 2022 at 14:24 UTC | |
Re: Deferring variables -- Getopt::Long
by Discipulus (Canon) on Jun 25, 2022 at 16:41 UTC | |
by LanX (Saint) on Jun 25, 2022 at 16:49 UTC | |
Re: Deferring variables
by kcott (Archbishop) on Jun 25, 2022 at 21:51 UTC | |
by perlfan (Vicar) on Jul 04, 2022 at 13:54 UTC | |
Re: Deferring variables
by LanX (Saint) on Jun 25, 2022 at 13:40 UTC | |
Re: Deferring variables
by Yaribz (Beadle) on Jun 25, 2022 at 16:52 UTC | |
Re: Deferring variables
by tybalt89 (Monsignor) on Jun 25, 2022 at 19:17 UTC | |
Re: Deferring variables
by LanX (Saint) on Jun 25, 2022 at 21:25 UTC |