develooper Front page | perl.perl5.porters | Postings from April 2015

Is there a clever way to null out non-void subs?

Thread Next
From:
Tom Christiansen
Date:
April 22, 2015 01:08
Subject:
Is there a clever way to null out non-void subs?
Message ID:
18706.1429664876@chthon
I've been pondering a particular puzzle for about a week, and I 
keep coming back to a dive into either the core perl guts or else 
source filters.  I can't find anything on the net about it.

Thinking about how sub () {23} functions get replaced with their
return values by the compiler, and how never looks at if (0) blocks,
I took to wondering if there isn't some easy way to take code like

    debug("Hello there %s, how are you?", somebody(42));

and "retroactively" null it out so that it it ripe for pruning and
doesn't cost anything to speak of.  Preferably something that could
be done with just core facilities, but this is not a strict requirement.

You can't just do 

    *debug = sub () { 0 };

before the compiler gets to it because of the mismatched prototype.

Plus you have the problem of wasted evaluation of somebody(42).

The other easy approach of magically replacing it with 

    if (0) { debug("Hello there %s, how are you?", somebody(42)) }

would work, but nobody wants to type all that gunk.

Is there a way to do this using some dirty hacking in the C API, or 
will it require something like Filter::cpp or Text::cpp, at which point
it becomes "easy", but . . . well, text filters.

I've looked pretty hard for anything like this, including using the 
cood CPAN grepper, and the most I have come if stuff that turns something
like DEBUGGING into a void function that evals to 0 or 1 and then they
go wrapping their calls to it.

I want to avoid that and just write

    _ASSERT( length($x) % 2 == 0 );

and then have those go completely away the way you can in C.  I don't
like the existing Perl stuff, because it never goes all the way away.

Do I have to use the cpp to do this, or is there something in the 
Perl guts that would work?

Lacking that, is there anything we can add to the core to *make* it work!?

I will entertain any idea, no matter how crazy. :)  

Thanks much,

--tom

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