laziness, impatience, and hubris | |
PerlMonks |
Re: Bare BLOCK vrs. grep BLOCKby ikegami (Patriarch) |
on Jan 17, 2018 at 04:56 UTC ( [id://1207393]=note: print w/replies, xml ) | Need Help?? |
You are under the incorrect impression that grep (and other functions) are subroutine calls. They are actually operators or sequence of operators. grep is closer to while than it is to a sub. You can plug into the parser to cause foo BLOCK to compile into a custom sequence of ops (kinda like grep does) using Devel::CallParser. Syntax::Feature::Loop is an example of this. Syntax::Feature::Loop allows you to do the following:
The above snippet is opcode-for-opcode identical to the following:
loop BLOCK compiles as a statement (like while), but having it compile as an expression (like grep) is just a question of passing a different flag. So that means it's possible to use Devel::CallParser to make it so
compiles into the same opcodes as
But that's not quite what you asked for. You asked for run-time evaluation of sequences of opcodes that don't form the body of sub. The whole point of a sub call is that it provides a way of returning to the calling code. But you don't have that in your scenario. You'd have to append an opcode to the chain to tell the interpreter to which op to jump after it reaches the end of the sequence. We're talking about self-modifying code. You're literally asking to get mygrep to replace its own body with the opcodes of the block at run-time. Even Perl doesn't go that far.
In Section
Seekers of Perl Wisdom
|
|