I was setting up some packages that call each other in a program, and, of course, have the common need to define accessor routines. Rather than going about using some CPAN module like Package::Pkg, I thought it should be easy to do what I want for my limited needs in a few lines of code (ok, you can stop laughing now). So I ran into a few snags.
Why isn't this as simple to do as it looks like it should be?
Instead of not doing what I want through some indirect method, perl refuses to even compile the program, saying:sub Instance_Var ($) { my $package=__PACKAGE__; my $var=$_[0]; my $accessor="$package::$var"; -> eval " sub $accessor { my $t=shift; $t->{$var}=$_[0] if @_; $t->{$var}; }"; }
Global symbol "$t" requires explicit package name at line indicated by '->'.
Hmmmm. At first and second (third?) glance, I'm not sure why and not sure how it should be structured, other than 'not that'. Rather than trying things at random until something works (or using the, above, mentioned CPAN module or another), I'd like to understand why this wouldn't work to do what I 'think' is clear from the example (I.e. creating a named accessor that accesses a variable in an assoc array passed in as argument, and optionally set's to the value of the argument it if an additional argument is passed to it).
Thanks!
In reply to How do I get to this 'simple' helper? by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |