in reply to On References to the Unnamed

I find it quite surprising that \"$a" creates a reference to an lvalue, since "$a" isn't an lvalue. This might be a bug, or an artifact of the implementation of perl. Unless I'd find a place where this is documented, I wouldn't count on it working on a next version of perl.

If I want a scalar to an anonymous scalar, I use:

\do {my $foo}; # Or do {\my $foo};
which is an well-known technique, and before we had auto-vivifying filehandles, similar to how we created references to filehandles (\do {local *FOO}).

As for sizes, \"$$" takes 2.5 times the memory \do {my $foo} takes. And even \"" takes more than twice the memory. This is because \do {my $foo} is just an SV, pointing to nothing. (The flag ROK says it's a reference). But \"" still has to point to something.

#!/usr/bin/perl use strict; use warnings; use Devel::Size 'total_size'; use Devel::Peek; my $a = \"$$"; my $b = \""; my $c = \do {my $foo}; print total_size($a), "\n"; print total_size($b), "\n"; print total_size($c), "\n"; print "-----\n"; print Dump($a); print Dump($c); __END__ 30 25 12 ----- SV = RV(0x81c1df4) at 0x8191e58 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x8184180 SV = PV(0x8184494) at 0x8184180 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x8189cf8 "23192"\0 CUR = 5 LEN = 6 SV = RV(0x81c1dfc) at 0x8191eac REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x8191f84 SV = NULL(0x0) at 0x8191f84 REFCNT = 2 FLAGS = (PADBUSY,PADMY)