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>
In reply to Re: Perl templating/macro creating using 'BEGIN'...
by Corion
in thread Perl templating/macro creating using 'BEGIN'...
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |