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

Another example of bad syntax: the other day I tried redo within a do {} until block, but Perl complains that I could not use redo outside a loop block, which means Perl does not take do {} until as a loop block.
That is documented behaviour (see perlsyn), though I must agree not very consistent. It applies not only to do {} until but also to do {} while. perlsyn suggests a workaround like this
do {{ next if /^#/; # do something }} until /^\s*$/;
but you have to adjust this to be able to use last.

My suggestion is to not use those constructs altogether and instead emulate them with bare blocks and redo ...

# do {} while condition { # some code redo if condition; } # do {} until condition { # some code redo unless condition; }

-- Hofmator

Replies are listed 'Best First'.
Re^3: Anon. array of refs to a range generated list of scalars.
by Aristotle (Chancellor) on Jan 18, 2003 at 20:01 UTC
    Argh. Please don't use naked blocks with redo as loops, it's a nightmare to read. When I see a naked block, I expect to be looking at a private lexical scope, not an unadorned loop. If you need next, wrap the inner part with a naked block:
    do {{ $bar; baz() or next; }} while $foo;
    If you need last, wrap the whole thing:
    { do { $bar; baz() and last; } while $foo; }
    Of course, if you need both, it does get awfully clumsy:
    LOOP: { do {{ $bar; baz() or next; quux() and last LOOP; }} while $foo; }
    In that case, and if you're so inclined then maybe in the other cases as well, I propose:
    while(1) { $bar; baz() or next; quux() and last; }
    At least it documents the loop nature of the block, even if the break condition is again somewhat hidden.

    Makeshifts last the longest.

      well, some people think differently ... and you could always mark the bare block with a label, clearly stating what you want to do
      LOOP: { # some code redo LOOP if condition; } # that's actually quite close to perl6 ;-) loop { # some code last unless condition; }

      -- Hofmator

        I tried to dredge up a heated discussion we had in the monastery maybe half a year ago, where the general consensus was that you shouldn't generally use naked blocks to loop like that, but came up empty handed after a dozen queries. :-/

        Makeshifts last the longest.