Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Isn't it a typical use case of tied hash ? This code for example should do exactly what you want, without requiring any get or set function:
use warnings; use strict; package ParamsHash; require Tie::Hash; our @ISA='Tie::StdHash'; use constant { UNDEF_VAL => 'a' }; # configure your parameter inheritance here: our @GLOBAL_PARAMS=(qw'margin padding'); our %SUB_PARAMS=(horizontal => [qw'left right'], vertical => [qw'top bottom']); my %INHERIT; foreach my $param (@GLOBAL_PARAMS) { foreach my $subp (keys %SUB_PARAMS) { map {$INHERIT{"$param-$_"}="$param-$subp"} @{$SUB_PARAMS{$subp}}; $INHERIT{"$param-$subp"}=$param; } } sub FETCH { my ($this,$key)=@_; my $val=$this->{$key}; return $val if(defined $val && $val ne UNDEF_VAL); my $inherited=$INHERIT{$key}; return UNDEF_VAL unless(defined $inherited); return $this->FETCH($inherited); }
You can test it by adding this code at the end of the file for example:
package main; my %par; tie(%par,'ParamsHash'); print "Initial values:\n"; printSomeParams(); print "Setting 'margin' to 4:\n"; $par{margin}=4; printSomeParams(); print "Setting 'margin-horizontal' to 8:\n"; $par{'margin-horizontal'}=8; printSomeParams(); print "Setting 'margin-left' to 3:\n"; $par{'margin-left'}=3; printSomeParams(); sub printSomeParams { map {printParam($_)} (qw' margin margin-horizontal margin-left margin-right ') } sub printParam { print " $_[0] = $par{$_[0]}\n" }
This should give you following result:
Initial values: margin = a margin-horizontal = a margin-left = a margin-right = a Setting 'margin' to 4: margin = 4 margin-horizontal = 4 margin-left = 4 margin-right = 4 Setting 'margin-horizontal' to 8: margin = 4 margin-horizontal = 8 margin-left = 8 margin-right = 8 Setting 'margin-left' to 3: margin = 4 margin-horizontal = 8 margin-left = 3 margin-right = 8

In reply to Re: Deferring variables by Yaribz
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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found