Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Deferring variables

by LanX (Saint)
on Jun 25, 2022 at 21:25 UTC ( [id://11145045]=note: print w/replies, xml ) Need Help??


in reply to Deferring variables

Sorry, I initially misunderstood your question, but reading some other replies helped.

(my main processor is (st)ill suffering from a virus attack ;-)

I tried aliasing, but no joy, after the first level you have far too much trouble maintaining.

I tried refs on hash elements, it worked but was a bit too fickle, if the data structure changed.

I tried just one data structure, denoting the links with special values like \"margin" , but again too much trouble maintaining such a mangled structure.

My suggestion is to keep it simple, an additional hash %def maps to the default and you use a simple recursive get-function.

use v5.12; use warnings; use Data::Dump qw/pp dd/; my %def = ( 'margin-left' => 'margin-horizontal', 'margin-horizontal' => 'margin', # ... ); my %par = ( 'margin' => 42, ); sub get { my $key = shift // return "UNDEFINED"; # or whatever error mecha +nism you prefer $par{$key} // get( $def{$key} ) } say get("margin-left"); # 42 $par{'margin-horizontal'} = 666; say get("margin-left"); # 666 undef $par{'margin-horizontal'}; say get("margin-left"); # 42 say get("TYPO"); # UNDEFINED

please note that I also preferred undef over "a"

Otherwise you can easily change the get code with a ternary

$par{$key} ne "a" ? $par{$key} : get( $def{$key} );

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

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

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

    No recent polls found