in reply to Re: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
in thread Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
by BrowserUk (Patriarch) on Nov 17, 2004 at 08:09 UTC | |
by revdiablo (Prior) on Nov 17, 2004 at 17:53 UTC | |
by BrowserUk (Patriarch) on Nov 17, 2004 at 23:33 UTC | |
by revdiablo (Prior) on Nov 18, 2004 at 01:13 UTC |