NOTE: This is a question about the Perl internals, the C code itself.
I want to pass the contents of the Perl stack (ie. SP) as an array to a Perl function.
- Is there an existing way to copy the stack into an AV?
- If not, how would one accomplish it? The array can be read-only.
- How does one turn an AV into a reference?
Found the crib I was looking for in pp_anonlist in pp.c
It took me a great number of tries before I finally settled on this:
This is similar to pp_anonlist, but not quite. dMARK expands to
SV **mark = PL_stack_base + (*PL_markstack_ptr--)
. MARK is used extensively but is ill defined in the documentation. Because dMARK modifies the state of the stack, it is unusable in my function which should have no side effects. TOPMARK is just *PL_markstack_ptr, no decrement. NUMARGS is effectivelySP - MARK
without the side effect.SP points at the top of the stack, but av_make() works on lists. So it's necessary to pass in
SP - NUMARGS
to ensure av_make() can read two items off the stack. Why its necessary to add one, I'm not sure.