Hi Monks, I'm looking to effect kind of a transparent nested active config system that stacks together as a configured object. Basically:
my $config = config( bool => true, token => 'a single string', value => [ 'a', 'list', 'of', 'strings' ], blah => store( # This function leaves a "constructor" in the c +onfig, that generates a new object when traversed. key => val, ... # Object typed and configured by these argume +nts... ), ... ); ... my $object = Type->new($config); # As Type builds an object, it refer +ences $config->value(), $config->token()... ... my $widget = $config->{path/to/some/nested/doohickey}; # This is actu +ally a nested tree traversal underneath... my $thingy = $widget->evaluate($with_this_context);
I'd like to make this configuration overlay as transparent as possible, allowing AUTOLOAD to catch calls to dereference names in this config. The problem is, because I have this set up as a tied hash, I have a few methods that get caught before AUTOLOAD can inspect the argument list and determine how to proceed, like:
sub FETCH { my $self = shift; if (! @_) { return $self->[0]->{FETCH}; } ... }
For methods like ->FETCH, some loophole in the context can be figured out, and I can do the right thing. I can later figure out a way to do:
$config->{FETCH} = SomeWidget->new(...); my $response = $config->FETCH->( term => [ 'range', 'of', 'values' ], ... );
But, this doesn't work for FIRSTKEY, DESTROY, etc. Is there any hole in the syntax that would allow me to detect a call to ->DESTROY that should release resources, versus a call which should search for a value in a hash and return it? Maybe a feature in the interpreter that would allow me to say:
sub FIRSTKEY { my $self = shift; if (! @_) { return $self->[0]->{FIRSTKEY}; } # ... walk the tree to gather nested keys $self->[1]->{iter} = \@list; # $_[0] is an empty array provided by the machinery that calls ->F +IRSTKEY underneath. my $name = shift(@list); push(@{$_[0]}, $name); # Either way works... return $name; }
And so on? This would allow me to basically trap the corner cases and provide the proper action to my constructed objects, no matter what key is being fetched via AUTOLOAD. Ultimately, the goal is an active configuration object able to formulate answers to questions (like $config->CFLAGS) on the fly, for a simple build system prototype. I'd just like to be able to not have to say:
# XXX: When dereferencing config keys, don't say ->FIRSTKEY, ->DESTROY +, ...

In reply to AUTOLOAD vs. DESTROY... by coreybrenner

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.