in reply to Creating flexible method accessor
The solution to your problem is to write \$self->${path}{qw($_)}, however, please consider the following:
Instead of the string form of eval, in your case you can use closures:
sub mk_data_accessors3 { my $path = shift; for my $name (@_) { my $accessor = sub { carp "Warning: '$name' takes at most 2 arguments...\n" if +@_ > 2; my $self = shift; $self->{$path}{$name} = shift if @_; return $self->{$path}{$name}; }; # keep the scope of "no strict" as small as possible no strict 'refs'; # needed for *$name *$name = $accessor; # install the sub } } mk_data_accessors3('data','bar'); print bar({data=>{bar=>456}}), "\n"; # 456
That keeps the depth of $path limited to one level of the hash, but I'll leave the rest as an "exercise for the reader" :-)
|
|---|