puterboy has asked for the wisdom of the Perl Monks concerning the following question:
which I call using:sub mk_data_accessors { for my $_ (@_) { eval qq{ sub $_ { carp "Warning: '$_' takes at most 2 arguments.. +.\n" if \@_ > 2; my \$self = shift; \$self->{data}->{qw($_)} = shift if \@_; return \$self->{data}->{qw($_)}; } }; die $@ if $@; } }
I then tried to 'generalize' by allowing the 'path' to the method to be a variable as follows:mk_data_accessors(qw(method1 method2 method3));
which I called with:sub mk_data_accessors2 { my $path = shift; for my $_ (@_) { eval qq{ sub $_ { carp "Warning: '$_' takes at most 2 arguments.. +.\n" if \@_ > 2; my \$self = shift; \$self->$path{qw($_)} = shift if \@_; return \$self->$path{qw($_)}; } }; die $@ if $@; } }
This gave me errors about %path not being defined -- presumably because $path{qw{$_}} is being interpreted as a hash element.mk_data_accessors2('{data}->', qw(method1 method2 method3));
|
|---|