develooper Front page | perl.perl5.porters | Postings from December 2010

Re: [perl #80628] [PATCH] __SUB__

Thread Previous | Thread Next
From:
Zefram
Date:
December 13, 2010 04:59
Subject:
Re: [perl #80628] [PATCH] __SUB__
Message ID:
20101213125941.GF2434@lake.fysh.org
demerphq wrote:
>This is why i proposed using a $^THIS_SUB "magic tied var" for the purpose.

A variable is a poor model for this.  I think it's best accessed via
something that looks like a function call.  __SUB__ would be fine,
except for the convention of __FOO__ denoting a compile-time constant.
A current_sub() function interface would be my preference.  This can
be very easily implemented in an XS module, generating a custom op.
Something like:

    static OP *
    pp_current_sub(pTHX)
    {
	dSP;
	XPUSHs(cxstack[cxstack_ix].blk_sub.cv);
	RETURN;
    }

    static OP *
    THX_ck_entersub_current_sub(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
    {
	OP *o;
	ck_entersub_args_proto(entersubop, namegv, ckobj);
	op_free(entersubop);
	/* I haven't learned how to use the new custom-op stuff yet */
	o = newOP(OP_PUSHMARK, 0);
	o->op_type = OP_RAND;
	o->op_ppaddr = pp_current_sub;
	return o;
    }

    BOOT:
    {
	CV *cscv = get_cv("Acme::CurrentSub::current_sub", 0);
	cv_set_call_checker(cscv, THX_ck_entersub_current_sub, (SV*)cscv);
    }

    void
    current_sub(...)
    PROTOTYPE:
    CODE:
	PERL_UNUSED_VAR(items);
	croak("current_sub called as a function");

With a little more work, this can be backported to older Perl versions
that don't have cv_set_call_checker().

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About