All,
There was a recent post on the Perl6 Language mailing list asking if there would be a way to identify code as "do it once only". Here is an example.
while ( <FH> ) { next if $_ eq 'foo'; # Where foo can only appear once in the file print; }
Once the match has been made, why must every subsequent line in the file be tested? I have also desired this ability in long tight running loops when performance was a concern and tried to figure out a way to make it work. I came up with the following inelegant code:
#!/usr/bin/perl use strict; use warnings; do_something(5); sub do_something { my $target = shift; my $block; my $block2 = sub { print "No need to check $_ against $target anymore\n"; }; my $block1 = sub { if ( $_ == $target ) { print "Skipping $target\n"; $block = $block2; } else { print "$_ is not $target\n"; } }; $block = $block1; for ( 1 .. 9 ) { $block->(); } }
Once the condition has been met, the code dynamically changes to no longer check for the condition. Keep in mind, to get this luxury I am now paying for the overhead of dereferencing and invoking a sub each time to get this "performance" benefit.

I was wondering what your thoughts on this are? If you think of running code as a work flow and each piece of functionality a widget then what we are talking about is a special kind of widget that can replace another widget while the work flow is running. Would building this functionality in the Perl 6 core be of great benefit? Obviously providing the functionality would open the door for other applications then just the "run once" feature. If this ability existed with negligible costs - what other things could you think of to do with it?

Cheers - L~R

Update: I removed some irrelevant pieces of the code which were leftover from my trial and error attempts


In reply to Doing "it" only once by Limbic~Region

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.