coreybrenner has asked for the wisdom of the Perl Monks concerning the following question:
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: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);
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:sub FETCH { my $self = shift; if (! @_) { return $self->[0]->{FETCH}; } ... }
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:$config->{FETCH} = SomeWidget->new(...); my $response = $config->FETCH->( term => [ 'range', 'of', 'values' ], ... );
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: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; }
# XXX: When dereferencing config keys, don't say ->FIRSTKEY, ->DESTROY +, ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AUTOLOAD vs. DESTROY...
by haukex (Archbishop) on Jan 08, 2023 at 10:27 UTC | |
|
Re: AUTOLOAD vs. DESTROY...
by LanX (Saint) on Jan 07, 2023 at 22:32 UTC |