Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Bare BLOCK vrs. grep BLOCK

by ikegami (Pope)
on Jan 17, 2018 at 04:56 UTC ( #1207393=note: print w/replies, xml ) Need Help??

in reply to Bare BLOCK vrs. grep BLOCK

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:

use syntax qw( loop ); loop { ... }

The above snippet is opcode-for-opcode identical to the following:

while (1) { ... }

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1207393]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-07-20 03:22 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (423 votes). Check out past polls.