开发者

What does Data::Util::modify_subroutine do for "around"?

I can only unders开发者_Python百科tand what it does in before/after cases,

what does it do for around?


Looks like around is like a before and after


From the documentation:

Optional arguments:

before => [subroutine(s)] called before subr.

around => [subroutine(s)] called around subr.

after => [subroutine(s)] called after subr.


It seems to be doing something like Sub::Curry according to the implementation:

static SV*
my_build_around_code(pTHX_ SV* code_ref, AV* const around){
    I32 i;
    for(i = av_len(around); i >= 0; i--){
        CV* current;
        MAGIC* mg;
        SV* const sv = validate(*av_fetch(around, i, TRUE), T_CV);
        AV* const params       = newAV();
        AV* const placeholders = newAV();

        av_store(params, 0, newSVsv(sv));       /* base proc */
        av_store(params, 1, newSVsv(code_ref)); /* first argument (next proc) */
        av_store(params, 2, &PL_sv_undef);      /* placeholder hole */

        av_store(placeholders, 2, (SV*)PL_defgv); // *_
        SvREFCNT_inc_simple_void_NN(PL_defgv);

        current = newXS(NULL /* anonymous */, XS_Data__Util_curried, __FILE__);
        mg = sv_magicext((SV*)current, (SV*)params, PERL_MAGIC_ext, &curried_vtbl, (const char*)placeholders, HEf_SVKEY);

        SvREFCNT_dec(params);       /* because: refcnt++ in sv_magicext() */
        SvREFCNT_dec(placeholders); /* because: refcnt++ in sv_magicext() */

        CvXSUBANY(current).any_ptr = (void*)mg;

        code_ref = newRV_noinc((SV*)current);
        sv_2mortal(code_ref);
    }
    return newSVsv(code_ref);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜