Yep! Bin there, done that. Trouble is, the lvalueness doesn't survive assignment:

#! perl -slw use strict; use warnings; use Inline C => <<'__EOI__'; SV* f(SV* sv, I32 pos, I32 len) { SV* targ = newSV_type(SVt_PVLV); sv_magic(targ, NULL, PERL_MAGIC_substr, NULL, 0); LvTYPE(targ) = 'x'; LvTARG(targ) = SvREFCNT_inc_simple(sv); LvTARGOFF(targ) = pos; LvTARGLEN(targ) = len; return targ; } __EOI__ use Devel::Peek qw( Dump ); my $x = "abcde"; my $r = f($x,1,3); Dump $r; print $r; __END__ C:\test>ike2-lv.pl SV = PVNV(0x410d558) at 0x97890 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) IV = 0 NV = 0 PV = 0x40fa988 "bcd"\0 CUR = 3 LEN = 8 bcd

The effect I am after is persistant lvalueness as demonstrated here (and back up there somewhere):

C:\test>perl -MDevel::Peek=Dump -E" $s = 'fred'; $r = \substr $s, 1, 2; $$r = 'oo'; say $s; Dump $_ for $s, $r; $$r = 'eu'; say $s; Dump $_ for $s, $r " food SV = PV(0x16d6f0) at 0x97b60 REFCNT = 3 FLAGS = (POK,pPOK) PV = 0x16b3b8 "food"\0 CUR = 4 LEN = 8 SV = RV(0x97be8) at 0x97bd8 REFCNT = 2 FLAGS = (ROK) RV = 0x97c80 SV = PVLV(0xe2c48) at 0x97c80 REFCNT = 2 FLAGS = (PADMY,GMG,SMG,pPOK) IV = 0 NV = 0 PV = 0x16b478 "oo"\0 CUR = 2 LEN = 8 MAGIC = 0x103708 MG_VIRTUAL = &PL_vtbl_substr MG_TYPE = PERL_MAGIC_substr(x) TYPE = x TARGOFF = 1 TARGLEN = 2 TARG = 0x97b60 SV = PV(0x16d6f0) at 0x97b60 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x16b3b8 "food"\0 CUR = 4 LEN = 8 feud SV = PV(0x16d6f0) at 0x97b60 REFCNT = 3 FLAGS = (POK,pPOK) PV = 0x16b3b8 "feud"\0 CUR = 4 LEN = 8 SV = RV(0x97be8) at 0x97bd8 REFCNT = 2 FLAGS = (ROK) RV = 0x97c80 SV = PVLV(0xe2c48) at 0x97c80 REFCNT = 2 FLAGS = (PADMY,GMG,SMG,pPOK) IV = 0 NV = 0 PV = 0x16b478 "eu"\0 CUR = 2 LEN = 8 MAGIC = 0x103708 MG_VIRTUAL = &PL_vtbl_substr MG_TYPE = PERL_MAGIC_substr(x) TYPE = x TARGOFF = 1 TARGLEN = 2 TARG = 0x97b60 SV = PV(0x16d6f0) at 0x97b60 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x16b3b8 "feud"\0 CUR = 4 LEN = 8

But I'd like to avoid the need for taking a reference and using indirection.

And to keep this all in one thread: No to "Are you really asking about making an lvalue function?"


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

In reply to Re^10: XS: SvPVLV examples? by BrowserUk
in thread XS: SvPVLV examples? by BrowserUk

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.