in reply to Autovivifying XS routine
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}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Autovivifying XS routine
by Anno (Deacon) on Jun 01, 2007 at 15:48 UTC | |
by shmem (Chancellor) on Jun 01, 2007 at 16:11 UTC |