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

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.

Replies are listed 'Best First'.
Re^6: "when" and replacements
by LanX (Saint) on May 07, 2011 at 12:11 UTC
    > 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.

        > 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"?

        No it's always an implied "next", for one-time-loops "next" and "last" just mean the same thing.

        BTW: perlsyn already has such a complicated rule:

        Every "when" block is implicitly ended with a "break".
        and

        Instead of using "given()", you can use a "foreach()" loop. ... On exit from the "when" block, there is an implicit "next".

        > I rather have given to be different from for, then given to be another alias for for/foreach.

        My intention was to have a "foreach" variant without automatic aliasing of the variable...

        > Changing the context means that you break given(@ARRAY).

        Yep, it's too late to change "given" now ...

        I still prefer avoiding "given/when" at least until I'm smart enough for smart match.

        And passing lists of arrays is no problem with \(...)

        DB<100> @a=1..3 DB<101> @b=4..6 DB<102> for (\(@a,@b)) {print} ARRAY(0xa3dcd78)ARRAY(0xa3dcc48) DB<103> for (\(@a,@b)) {print @$_} 123456

        while "given" relies on magic:

        So "given(@foo)" is the same as "given(\@foo)"

        Cheers Rolf