Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

ref returns LVALUE for a reference to a PVLV, a scalar subtype. You can read about it a bit here. It's basically a scalar that's going to have magic associated with it, and it has a few extra fields for storage the magic can use.

Magic, among other things, allows one to attach a getter and fetcher to a variable.

You might think that

substr($_, 3, 2) = 'foo';

gets converted to

substr($_, 3, 2, 'foo');

by the compiler, but it isn't. substr is truly called as is. This allows the following to work

my $ref = \substr($_, 3, 2); $$ref = 'foo';

That means substr must return a magical scalar.

There are more than one type of scalar. Some can hold an integer, some can hold a string, some can hold both. Perl automatically upgrades a scalar when necessary.

Magical scalars require extra fields to store information about what magic is attached to the scalar. The most basic scalar subtype capable of being magical is the PVMG, but there is also PVLV. The PVLV is a PVMG with four extra fields: TYPE, TARGOFF, TARGLEN and TARG.

A instance of substr in an lvalue context returns a PVLV. It uses TARG, TARGOFF and TARGLEN to store the three arguments passed to substr[1].

>perl -MDevel::Peek -e"$_ = 'abcdef'; my $ref = \substr($_, 3, 2); Dum +p($$ref);" SV = PVLV(0x4d3d24) at 0x2cb29c <--- PVLV REFCNT = 1 FLAGS = (TEMP,GMG,SMG) <--- "get" magic and "set" magic IV = 0 NV = 0 PV = 0 MAGIC = 0x49f09c MG_VIRTUAL = &PL_vtbl_substr <--- Function pointers for magic MG_TYPE = PERL_MAGIC_substr(x) <--- x = substr magic TYPE = x <--- x = substr magic TARGOFF = 3 <--- For "x", start offset of substring TARGLEN = 2 <--- For "x", length of substring TARG = 0x4a8414 <--- For "x", addr of associated scalar ($_) FLAGS = 0 SV = PV(0x2c8a6c) at 0x4a8414 <--- Dump of associated scalar ($_) REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x2cab94 "abcdef"\0 CUR = 6 LEN = 12

When you try to fetch from $$ref, the associated get magic first does something like $$ref = substr($$TARG, $TARGOFF, $TARGLEN);.

After you assign to $$ref, the associated "set" magic effectively does substr($$TARG, $TARGOFF, $TARGLEN, $$ref);.


  1. Missing and negative arguments are resolved before being assigned.

In reply to Re^3: Setting end position for the regexp engine using LVALUE by ikegami
in thread Setting end position for the regexp engine by bojinlund

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (5)
    As of 2021-01-21 19:19 GMT
    Find Nodes?
      Voting Booth?