2) a block, once entered, never entered again (including testing for the attached condition).

Point 2 raises some questions. You talk about an attached condition. If you only talk about blocks with an attached condition, what's the difference between 1) and 2)?

Also, suppose you had a 'once-only' construct, what should the following do:

for (1 .. 4) { if ($_ % 2) {print "odd"} # Assume 'once only blocks' else {print "even"} }
Should it print "odd even even even"? But then, the else block isn't "once only". Print "odd even"? But then the conditional is still checked after being true once. Should it just print "odd"? That would violate the principle of least surprise.

Lots of potential. Still, is it useful? Is it practical? Or is it just cool?

Well, you (sort of) can do this in Perl6. Perl6 has FIRST blocks, which will be executed the first time the surrounding block is entered. So, effectively, you can make a "executed only once" block this way:

{ FIRST { ... } }
that is, have nothing in the block, except a FIRST sub-block. However, that will not prevent a conditional from being executed.

But in my opinion, the frequency of such construct isn't high enough for the extra syntactical sugar (and the subsequent pain when reset mechanisms need to be bolted on). It's really easy to simulate:

my $flag = 1; ... if ($flag && CONDITION) { $flag = 0; ... }
The then block will be executed only once, but it the programmer has an very easy mechanism to reset the 'once only' property. And depending on the scope of the flag, it's a 'once-only in the life time of the program' or 'once-only for each time the surrounding block was entered'. Or anything in between.

Sure, there's the slight overhead of an extra 'if (0 && ...)' - but if your program can't deal with that, and there's nothing else to be optimized, you probably shouldn't have used Perl in the first place.


In reply to Re^4: Doing "it" only once by Anonymous Monk
in thread 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.