Won't that create the my variables inside the scope of your sub, which will then go out of scope pretty much immediately? I don't think the OP is asking the right question because s/he's talking about package-scope variables, which are, by their nature, global, not lexical. In that case, it becomes much easier:
Of course, if $package is supposed to be this package, then that is a bit simpler - change $package to __PACKAGE__, and you're done.for (@attributes) { no strict 'refs'; ${$package . '::' . $_} = do { ... something to do with $_ ... }; }
Update: now that diotalevi has added some extra ellipses, I can see how he's proposing to do this without the variables going out of scope. And now I think I better understand the OP's question, which has nothing to do with globals because he's trying to create a variable that is a string. And that string just happens to look like a global variable name.
In this case, I'd suggest a number of possibilities. First thing to come to my head is stupid, so I'm going to skip that one. ;-) The next idea is to just create the constants that you're going to need using constant:
Then you don't need to keep redeclaring anything.use constant { map { uc $_ => __PACKAGE__ . "::$_" } qw(name timestamp etc.) };
(I said a number of possibilities, right?) Still, use a hash. The usage would be something like $self->{$my{name}} which is still a lot lighter than $self->{__PACKAGE . '::name'}
And back to the stupid one. Create a package, say called "My" or "P" or something. Set up an AUTOLOAD function which looks at the package of the caller, and just returns that package name concatenated with '::' and then the function being called. Usage would then be $self->{P->name}
But don't do that last one.
In reply to Re^2: Computed "my" declarations?
by Tanktalus
in thread Computed "my" declarations?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |