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

mygrep BLOCK LIST

compiles into the same opcodes as

grep BLOCK LIST

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 reply to Re: Bare BLOCK vrs. grep BLOCK by ikegami
in thread Bare BLOCK vrs. grep BLOCK by powerin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.