The solution to your problem is to write \$self->${path}{qw($_)}, however, please consider the following:
- Don't use my $_ - either do for (...) {...} or for my $foo (...) {...} (lexical $_ is experimental)
- Always use warnings; use strict; (that'll give you the warning about my $_ as of Perl 5.18)
- Try very hard to avoid stringy eval - the current problems you are having with it are just the tip of the iceberg!
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" :-)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.