I wasn't particularly enamoured with it, hence my "could be refactored" comment. The reason I didn't refactor it at the time was I couldn't see a nice way how to.

Noting your "personal preferences" emphasis, I hope you don't mind if I respond with my take on things?

  1. a block pretending to be a loop, and the use of redo and last therein.

    And that repetition of 'do stuff' is a problem. In this case, having read a line at the top of the while loop, we need to

    • do some stuff (initialise out parens count from the first line)
    • enter the loop construct
    • do some more stuff (prepend the comment card and print)
    • read the next line, check for eof.
    • chomp the line we just read.
    • Do some more stuff (adjust the parens count from the new line)
    • decide whether to loop or not

    And the only way I know how to do that in perl (without artificial means like setting flags and/or double condition tests) is redo.

    You said: I'd be happier with a do block, rather than a bare block., but that doesn't work:

    #! perl -slw use strict; my $i =0; do{ print ++$i; redo if $i < 5; }; __END__ c:\test>junk2 1 Can't "redo" outside a loop block at c:\test\junk2.pl line 7.

    You'd have to do

    #! perl -slw use strict; my $i =0; do{{ print ++$i; redo if $i < 5; }}; __END__ c:\test>junk2 1 2 3 4 5

    That is, embed a bare block within the do block, and that is redundant and very obscure.

    You could adopt a Perl 6 like construct:

    LOOP:{ ... redo LOOP; }

    which could be construed as clearer. But frankly, redo in a bare block is a perfectly valid and useful construct and, I think, it is better to just become familiar with it than to obscure it. Indeed, it is actually the most flexible looping construct. It can be used to construct all many other looping constructs Perl has. Even the much decried but extremely flexible C-style for loop with its otherwise unique ability to vary multiple indexes concurrently.

    ## draw the diagonals for( my $x=0, my $y=0; $i < $xMax; $x++, $y++ ) { draw( $x, $y ); +} for( my $x=0, my $y=$yMax; $i < $xMax; $x++, $y-- ) { draw( $x, $y ); +}

    It's a little used feature, but when you need it, you need it:

  2. chomp that isn't the first thing done in the "loop". I'd have put it first, even if any subsequent print (etc) had to include \n.

    Hm. I'm not sure what the position of chomp has to do with the loop construct. The chomp has to follow the readline. The readline has to occur in the middle of the loop.

I'm still not happy with the construction I posted, but I haven't come up with a better one.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: Comment a block that match a keyword by BrowserUk
in thread Comment a block that match a keyword by yorkwu

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.