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:

for (@attributes) { no strict 'refs'; ${$package . '::' . $_} = do { ... something to do with $_ ... }; }
Of course, if $package is supposed to be this package, then that is a bit simpler - change $package to __PACKAGE__, and you're done.

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:

use constant { map { uc $_ => __PACKAGE__ . "::$_" } qw(name timestamp etc.) };
Then you don't need to keep redeclaring anything.

(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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.