perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I get to this 'simple' helper?
by Corion (Patriarch) on Sep 03, 2010 at 06:02 UTC | |
|
Re: How do I get to this 'simple' helper?
by SuicideJunkie (Vicar) on Sep 02, 2010 at 21:51 UTC | |
by perl-diddler (Chaplain) on Sep 02, 2010 at 23:24 UTC | |
|
Re: How do I get to this 'simple' helper?
by bluescreen (Friar) on Sep 02, 2010 at 23:50 UTC | |
by AnomalousMonk (Archbishop) on Sep 03, 2010 at 01:49 UTC | |
|
Re: How do I get to this 'simple' helper?
by ikegami (Patriarch) on Sep 03, 2010 at 16:30 UTC | |
|
Re: How do I get to this 'simple' helper?
by perlpie (Beadle) on Sep 03, 2010 at 03:04 UTC | |
by ikegami (Patriarch) on Sep 03, 2010 at 06:17 UTC | |
by Argel (Prior) on Sep 03, 2010 at 20:03 UTC |