You're explicitly overwriting the globref with a scalar. <P.You need to use the right syntax to assign to the SCALAR slot of the referenced glob.

[ 7:53:35.32] P:\test>perl -l my $globref = \do { local *GLOB }; print $globref; ## Assign to the SCALAR slot in the glob referenced by $globref ${ *$globref } = 123456789; print $globref; ## Still a GLOB print ${ *$globref }; ## Contains our value ## This is where the trouble comes in. *$globref = sub{ print 'test' }; print ${ *$globref }, $/, &{ *$globref }; ^Z GLOB(0x224fc8) GLOB(0x224fc8) 123456789 Not a GLOB reference at - line 13.

However, I did just discover this.

[ 7:56:55.39] P:\test>perl -l my $globref = do { local *GLOB; \*GLOB; }; print $globref; ## Assign to the SCALAR slot in the glob referenced by $globref ${ *$globref } = 123456789; print $globref; ## Still a GLOB print ${ *$globref }; ## Contains our value ## This is where the trouble comes in. *$globref = sub{ print 'test' }; print ${ *$globref }, $/, &{ *$globref }; ^Z GLOB(0x1824284) GLOB(0x1824284) 123456789 test 123456789 1

If I return the reference to the localised GLOB from with the do block, then everything works fine, without the need to pre-assign something to the actual GLOB before hand.

I'm pretty sure that I picked up on the idiom of

my $globref = \ do{ local *GLOB; };

from a usually reliable (and wizardly:) source, but I'll need to check that.

As I understand it, localising the GLOB effectively give you access to a new (anonymous) typeglob ensuring that if the code is called multiple times, you get a new once each time, without needing to go the Symbol route of creating sequence numbered globs in a special namespace.

However, my understanding may well be wrong. This could just be what I assumed the do local construct was doing. Hmmm. Off to try and relocate some sources and refresh my memory.


In reply to Re^3: Assigning to the CODE slot of a GLOB whose REF is held in a lexical? by BrowserUk
in thread Assigning to the CODE slot of a GLOB whose REF is held in a lexical? by BrowserUk

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.