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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |