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>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.