Yes, this is a very useful and cool thing to do. Other languages provide a more stable facility for this, such as Javascript, Lisp, and other functional langauges. (Yes, Javascript is more of a functional language than anything else.)

That said, the facilities for this sort of thing aren't very good in Perl. The first problem is that there isn't a stable Perl parser, other than perl. PPI does as best you can and the author will freely admit there are simple snippets that it will simply fail on. Perl 6 will do this a lot better and, I suspect, you will see a lot more of this sort of thing when Perl 6 comes out, particularly with macros.

For the record, I wrote Sub::Compose as an experiment for the pre-alpha version of Moose. When stvn and I were doing the initial spike that eventually became the pre- and post- processors for methods in Class::MOP, we were playing with restrictions on accessors. The idea was to have a bunch of simple-minded restrictions (cannot be undef, must have a certain length, must be greater than X, must be less than Y, etc). Then, you could chain those restrictions together in neat ways. Except, like everything in Perl that depends on a large number of tiny subroutines, performance was abysmal; the sub-calling overhead was just too high. So, I played around with making them all one subroutine and released that. You'll notice it's been a 0.01 for over 2 years. I honestly don't think anyone's ever used it. I've never received any email about it.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: Is it useful to edit code at execution time? by dragonchild
in thread Is it useful to edit code at execution time? by citromatik

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.