Re: So it isn't valid to take a reference to something that hasn't yet been defined

Actually, while working on Exporter::VA I discovered that it does in fact work. Taking the reference early defines a place-holder for it, and later defining it fills in that place holder. Taking a reference to the thing after it's been defined properly gives the same reference as taking such a reference "too early".

BEGIN { print \&baz, "\n" } sub baz {} print \&baz;
What you can't do is call baz from the begin block. But you can certainly stash away a reference to it for later.

I do exactly that in Exporter::VA. The import function can see a ref as a parameter, even though the implicit BEGIN block is causing this to run before the thing exists. To make this work well, at import() time it only remembers the reference, and does not try to update the contents (its changes will be clobbered by your initialization later) or rely on it being set-up just yet. Later, a function that import() put into my package is executed, and it can refer to that reference now.

So, you need to make your use statement just set-up, and "commit" the action later. In this case, sticking an END block into the calling package might do the trick.

—John


In reply to Re: Code references as use() parameters by John M. Dlugosz
in thread Code references as use() parameters by diotalevi

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.