in reply to Re^7: XS: SvPVLV examples?
in thread XS: SvPVLV examples?

I wonder if each variables with ext magic has its own virtual table pointer.

It does! That means that having only one extension type is not as limiting as it seems!

use strict; use warnings; use Inline C => <<'__EOI__'; static const char* test1_str = "Hello, "; static int test1_mg_get(pTHX_ SV *sv, MAGIC *mg) { sv_setpvn(sv, test1_str, (STRLEN)strlen(test1_str)); return 0; } static const MGVTBL vtbl_test1 = { test1_mg_get, NULL, NULL, NULL, NULL }; SV* get_test1_var() { SV* sv = newSV_type(SVt_PVLV); sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_test1, NULL, 0); return sv; } static const char* test2_str = "World!"; static int test2_mg_get(pTHX_ SV *sv, MAGIC *mg) { sv_setpvn(sv, test2_str, (STRLEN)strlen(test2_str)); return 0; } static const MGVTBL vtbl_test2 = { test2_mg_get, NULL, NULL, NULL, NULL }; SV* get_test2_var() { SV* sv = newSV_type(SVt_PVLV); sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_test2, NULL, 0); return sv; } __EOI__ use Devel::Peek qw( Dump ); my $test1 = get_test1_var(); my $test2 = get_test2_var(); print("$test1$test2\n");

Replies are listed 'Best First'.
Re^9: XS: SvPVLV examples?
by BrowserUk (Patriarch) on Sep 25, 2009 at 03:50 UTC
    It does! That means that having only one extension type is not as limiting as it seems!

    Actually, there are two. There is also uvar magic:

    U PERL_MAGIC_uvar vtbl_uvar Available for use by ex +tensions

    You can also chain multiple magics. See the description for SvPVMG in Perlguts:

    moremagic is a pointer to another MAGIC and is used to form a single linked list of the MAGICs attached to an SV.

    For my purposes, it is the lvalueness associated with PVLVs that is of most interest.


    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.

      For my purposes, it is the lvalueness associated with PVLVs that is of most interest.

      All of magics with a setter in their vtable have lvalueness. There's nothing special about PERL_MAGIC_substr, and the use of PVLV is not a requirement.

      Note that returning a PVLV or something magical doesn't make your function an lvalue function. That's a compile-time determination indicated by the :lvalue attribute.

      Are you really asking about making an lvalue function?