in reply to Re^2: 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?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Assigning to the CODE slot of a GLOB whose REF is held in a lexical?
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 |