in reply to Perl templating/macro creating using 'BEGIN'...
BEGIN { sub main_package_vars { foreach(@_) { eval "sub $_ { my \$p = shift; \$p->{$_} = \$_[0] if \@_; \$p->{$_}; }" } } }
Why do you put this in a BEGIN block? It serves no purpose, at least when Perl looks at it.
Also, why are you using string-eval, when the plain eval works just as well. Also see Class::Accessor.
sub main_package_vars { foreach my $name (@_) { no strict 'refs'; *{ $name } = sub { my $p = shift; $p->{$name} = $_[0] if @_; $p->{$name}; }" } }
In this next snippet from MyPackage, I don't understand why you use string-eval, and also why you try to execute the same statement twice:
BEGIN { eval "*package_vars2 = \*main::main_package_vars"; *package_vars3 = \*main::main_package_vars; }
The subroutine package_vars3 does not exist:
package_vars3( qw(one two three) );
What problem are you trying to solve? Do you want to have a hash using which you get/set variables?
Maybe an object based on a plain hash using AUTOLOAD is already enough? If you want control over what keys get stored, again, see Class::Accessor.</c>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl templating/macro creating using 'BEGIN'...
by perl-diddler (Chaplain) on Sep 27, 2010 at 17:39 UTC | |
by Corion (Patriarch) on Sep 27, 2010 at 18:11 UTC | |
Re^2: Perl templating/macro creating using 'BEGIN'...
by perl-diddler (Chaplain) on Sep 27, 2010 at 17:57 UTC | |
by Corion (Patriarch) on Sep 27, 2010 at 18:06 UTC | |
by perl-diddler (Chaplain) on Sep 27, 2010 at 20:25 UTC | |
by Corion (Patriarch) on Sep 27, 2010 at 20:29 UTC | |
by perl-diddler (Chaplain) on Sep 28, 2010 at 02:36 UTC |
In Section
Seekers of Perl Wisdom