I recently started experimenting with subroutine and variable attributes. For my application, I would really like to know the name of the variable (or subroutine, though that is much less important) that is getting attributed. I am using the 'attributes' module directly rather than Attribute::Handlers, since for my particular (embedded) application, A::H doesn't add anything I need.

So I am figuring out the name of the variable by scanning through the package's symbol table to find the reference to the variable that was passed to MODIFY_SCALAR_ATTRIBUTES. (I do the scan much later than the MODIFY_SCALAR_ATTRIBUTES is called, which is probably good because I'm not sure if the variable has been added to the symbol table back then.) It's working fine, but I'm wondering what sort of gotchas to watch out for. Obviously lexical variables won't work, but that's not a problem for me since for other reasons it makes much more sense for these variables to be 'our' variables anyway. If someone deleted my variable from the symbol table, could the reference be reused (I mean, the memory address that the reference was in)? I don't think so, because I have to keep the reference around in order to look it up in the symbol table. What would happen if after the variable declration, I 'use'd a module that exported that variable?

Or, even better, is there a more direct way of figuring out an attributed variable's name? It need not be fast, but it must be robust.


I work for Reactrix Systems, and am willing to admit it.

In reply to attribute gotchas by sfink

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.