The following works even with the commented out lines and doesn't appear to leak at all.

There's no leak since you don't create mortals. I think it would also clear mortals returns by the called function, but maybe print returns nothing in void context?

Why does it crash in a Benchmark?

PUTBACK should come before the call, although it's not even required when there are no args.

The documentation uses G_DISCARD instead of G_VOID.

You change GvSV(PL_defgv).

Do any of those matter? dunno.

The code you posted doesn't crash for me using v5.12.2 built for i686-linux.

How can it be improved?

SAVE_DEFSV; appears to be the means to save $_, and DEFSV_set(sv); appears to be the means to set it.

Does that work if there's a my $_ in scope of the callback? (Is that even possible?)

I don't think it's safe to assume the SV still has a PV slot after the callback.

You could accept strings in either formats instead of suffering from The Unicode Bug.

Are there any documentation or examples of using LvTARG macros to do the aliasing?

LvTARG is used by some types magic (e.g. lvalue substr) as they see fit. While you could use magic to effectively do aliasing, that wouldn't be as efficient as actually aliasing.

It's pretty straightforward. Check out function pp_substr and the functions related to substr in mg.c

Is there any documentation of the MULTICALL macros used by List::Util::reduce()?

Multicall is documented in "LIGHTWEIGHT CALLBACKS" in perlcall.

How can I apply a (&$) prototype to strIter()?

Using the PROTOTYPE XS directive.

Update: Mentioned SAVE_DEFSV and DEFSV_set.


In reply to Re: Help me improve my XS. by ikegami
in thread Help me improve my XS. 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.