I think the thing with the do {} syntax is that you aren't iterating with the do block.

You are iterating the do block itself.

For example:

$i=5; print do{ --$1 }; # prints 4 $i=5; print do{ --$i } while $i; # prints 43210

However, this is directly analogous to

$i=5; print --$1; # prints 4 $i=5; print --$i while $i; # prints 43210

In the latter example, its very clear that the while modifier form is iterating the print.

The former example, the while modifier is iterating the print also. It just so happens that the print has a do block as one of its terms. This is made clearer if you use the brackets on the print function like this.

$i=5; print( do{--$i;} ) while $i;

The confusion comes because we often format the construct like this

$i=5; do { print --$i; } while ($i);

Which makes it look like an iterate-at-least-once version of

$i = 5; while ($i) { print --$i; }

But you can show the difference by doing

my ($i, $j) = (5, 0); $j += do { print --$i; } while ($i); print $j; # prints 5.

Where the value 5 comes from the accumulation of the five 'successful' (1) return codes from the print function.

Additionally

If there is an inconsistancy with do, it's that the way it operates as an rvalue, makes it look somewhat analogous to the sort, map & grep functions, in as much as I half expected

print do( 'fred' ); to work like print do{ 'fred' } Somewhat like

 print map $_, 'fred'; is similar to print map{ $_ } 'fred';.

Actually, the error message from

 print do( 'fred' ); confuses me completly.


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: Re: Anon. array of refs to a range generated list of scalars. by BrowserUk
in thread Anon. array of refs to a range generated list of scalars. by BrowserUk

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.