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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.