I guess this has to do with Alter :-)

There's the GIMME_V macro which will tell whether you are in scalar or array context; there's the PL_op macro to get the current OP, which should be 166 for ego itself (method). A COP structure has a member op_next.

printf("current: %d\n", PL_op->op_type); printf("next: %d\n", PL_op->op_next->op_type); if(GIMME_V) { printf("array context\n"); } else { printf("scalar context\n"); }
perl -le 'use blib; use Alter qw(ego); $f = ego($a, { 1, 2 });' current: 166 # OP_METHOD next: 6 # OP_GVSV scalar context perl -le 'use blib; use Alter qw(ego); $f = {ego($a, { 1, 2 })}' current: 166 # OP_METHOD next: 144 # OP_ANONLIST huh? array context perl -le 'use blib; use Alter qw(ego); $f = [ego($a, { 1, 2 })]' current: 166 # OP_METHOD next: 143 # OP_LSLICE huh? array context Assertion *strp failed: file "av.c", line 392 at -e line 1. wtf???

But the next OP could also be something entirely different, depending on position...

perl -le 'use blib; use Alter qw(ego); $f = {ego($a, { 1, 2 }),1}' current: 166 # OP_METHOD next: 5 # OP_CONST array context perl -le 'use blib; use Alter qw(ego); $f = 1,ego($a, { 1, 2 }),1' current: 166 # OP_METHOD next: 141 # OP_JOIN array context

Anyways, I hope that helps and gets you on the way :-)

<update>

For your examples...

perl -le 'use blib; use Alter qw(ego); ego($a, { 1, 2 })->{"foo"} = "b +ar"' current: 166 # OP_METHOD next: 134 # OP_EXISTS scalar context Segmentation fault perl -le 'use blib; use Alter qw(ego); @{ ego( $a, { 1, 2 }) } = (1,2, +3)' current: 166 # OP_METHOD next: 178 # OP_ENTER hmm? scalar context Segmentation fault perl -le 'use blib; use Alter qw(ego); ego( $a, { 1, 2 })->[0] = (1,2, +3)' current: 166 # OP_METHOD next: 125 # OP_QUOTEMETA interesting... scalar context Segmentation fault

I guess the segfaults are quite normal, since ego() isn't type aware yet... but maybe overload() or WANT might suit you better, as ikegami and BrowserUk pointed out, since relying on how perl organizes its opcode chain is - um, well - scary?

</update>

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Autovivifying XS routine by shmem
in thread Autovivifying XS routine by Anno

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.