Quoth Rainer Weikusat <
rwei...@mssgmbh.com>:
>
> ${*$fh{SCALAR}} = $name;
> It also relies on the feature that the SCALAR slot of a glob
> autovivifies like any other 'real reference', something which is
> documented as
>
> This might change in a future release.
> (perlref)
That's extremely unlikely. That sentence was (I believe) intended to
prepare people for the possibility of perl's glob creation mechanism
changing so that a glob did not necessarily have a SCALAR component.
In 5.8 and earlier, when a glob was created the SCALAR slot was filled
at the same time:
~% perl5.8.9 -MDevel::Peek -e'Dump *foo'
SV = PVGV(0x284b0068) at 0x28403d08
[...]
NAME = "foo"
NAMELEN = 3
GvSTASH = 0x2840309c "main"
GP = 0x284b7760
SV = 0x28403cb4
[...]
AV = 0x0
HV = 0x0
CV = 0x0
[...]
Notice that although AV, HV and CV are null, SV has already been filled.
This is reflected in the corresponding *foo{THING} operations:
~% perl5.8.9 -le'print for *foo{ARRAY}, *foo{SCALAR}'
SCALAR(0x28403cc0)
~%
For 5.10 this was changed, since most globs in modern Perl programs only
use the CODE slot (since most variables are now lexicals), so creating a
scalar for the SCALAR slot was a waste of memory:
~% perl5.10.0 -MDevel::Peek -e'Dump *foo'
SV = PVGV(0x800ecc630) at 0x800e8fed0
[...]
NAME = "foo"
NAMELEN = 3
GvSTASH = 0x800e0a108 "main"
GP = 0x800ebb4c0
SV = 0x0
[...]
AV = 0x0
HV = 0x0
CV = 0x0
[...]
However, in order not to break code like the example above, it was made
impossible to see this from Perl. As soon as you try to look at the
SCALAR slot, it autovivs and pretends there was a scalar there all
along, so the Perl test gives the same result as before:
~% perl5.10.0 -le'print for *foo{SCALAR}, *foo{ARRAY}'
SCALAR(0x800e0a2a0)
~%
Since the change has been made already, and in a way which preserves
backcompat, I think it extremely unlikely that this behaviour will ever
go away.
If you're worried, though, all you need to do is create a fresh scalar
and stuff it into the glob, just as you would if you wanted to use the
ARRAY or HASH slot. There isn't an explicit anonymous scalar constructor
(though I sometimes wish there was) but a lexical that's about to go out
of scope will do just fine:
*$fh = do { \my $tmp };
${*$fh{SCALAR}} = $name;
In a short method, of course, you don't need to bother with the do
block.
Ben