in reply to Re^3: "when" and replacements
in thread "when" and replacements

>I kind of like the idea that since one can use when clauses in a for, given doesn't alias. If you want aliases, write for; otherwise, write given.

Yeah sounds like perfect orthogonal design. Though one has to keep in mind that leaving the block demands different statements.

use feature qw(switch); given (1) { print; break; print } for (1) { print; last; print }

Cheers Rolf

UPDATE:

confusingly this works well:

$\="\n"; for ("012") { if (/0/) {print "0 matched"}; # prints 0 when (/1/) {print "1 matched"}; # prints 1 when (/2/) {print "2 matched"}; # nix if (/3/) {print "3 matched"}; # nada }

but the perlsyn says Every "when" block is implicitly ended with a "break".

Consequently "break" should also leave loop-blocks, or the documentation should be updated.

IMHO break is needless and should be abandoned in favor of last. (which linguistically still makes sense)

Replies are listed 'Best First'.
Re^5: "when" and replacements
by JavaFan (Canon) on May 07, 2011 at 12:01 UTC
    IMHO break is needless and should be abandoned in favor of last. (which linguistically still makes sense)
    But break doesn't act as last. It acts as next. But using next in a given/when to end the given would be odd - I'd expect next to act as continue does now.
      > It acts as next.

      good catch! :)

      > But using next in a given/when to end the given would be odd

      Yes and no. Your implying that last (which doesn't look odd) couldn't be used because next is possible.

      There are many grammatically correct combinations in perl that look odd, so nobody uses them.

      Cheers Rolf

      UPDATE: consequently full orthogonality would imply allowing multiple args in given(LIST)

        Your implying that last (which doesn't look odd) couldn't be used because next is possible.
        So, then the rule would be "when using when, there is an implied last when used inside a given, but there's an implied next when used inside a for"?

        Not sure if that's an improvement.

        consequently full orthogonality would imply allowing multiple args in given(LIST)
        Uhm, do realize that given(EXPR) evaluates EXPR in scalar context, while for(EXPR) evaluates in EXPR in list context. As such, given(LIST) isn't meaningful. Changing the context means that you break given(@ARRAY).

        If you want given to behave exactly as for, write for. I rather have given to be different from for, then given to be another alias for for/foreach.