if the original GLOB has nothing assigned to the code slot, nothing works.

That's not the behavior I'm seeing. This code works fine all by itself:

my $globref = \*GLOB; *$globref = sub { print "Glob\n" }; GLOB();

It prints "Glob\n" just as I'd expect, even though the glob had nothing at all when the reference was taken. The problem comes from the use of local in the do block when creating the globref.

A globref created from a localized glob seems to lose some of its magic when the block exits. Then, when you try to assign something to one of its slots, the whole reference gets overwritten by what you assigned, not just the particular slot:

my $globref = \do { local *GLOB }; print $globref, "\n"; # prints GLOB(0x.......) *$globref = 1; print $globref, "\n"; # prints SCALAR(0x.......)

I don't know if this is a bug, but it's certainly surprising. I'd expect local to clobber the values from the globref when the block exits, but making the globref itself act differently is kind of strange.


In reply to Re^2: Assigning to the CODE slot of a GLOB whose REF is held in a lexical? by revdiablo
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.