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)

In reply to Re: On References to the Unnamed by Anonymous Monk
in thread On References to the Unnamed by tlm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.