in reply to "when" and replacements

Even if it would accept the when, it may still not do what you expect it to. In
given ($var) { ... use of $_ ... }
$_ is a copy of $var. Any modifications to $_ will not propagate to $var. But you could write:
for ($var) { ... when clauses ... if (s{^cpan://}{http://search.cpan.org/search?query=}p) { ... last; } ... more when clauses ... }

Replies are listed 'Best First'.
Re^2: "when" and replacements
by John M. Dlugosz (Monsignor) on May 07, 2011 at 09:02 UTC
    I see. "given(EXPR) will assign the value of EXPR to $_ within the lexical scope of the block, ..." so it's not like for, map, and just about everything else that's come before.

    Perhaps the thought was "new feature, use current ideas", but that contradicts the Principle of Least Surprise.

      There is no point in a redundant implementation of the aliasing-behavior of  for ( onearg ).

      And aliasing is too powerful for many users, it can cause hard to debug errors thru side-effects.

      So if you want it use for, if you don't want it chose given.

      Cheers Rolf

      There's currently a thread on p5p where some people argue that the non-aliasing behaviour of given is a bug. 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. OTOH, I don't think I've ever written code where I wanted given to alias, nor that I wanted it to not-alias. That is, each time I use given, whether $_ aliases or not, it does not matter.
        >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)

        "least surprise" is a powerful consideration.

        In Perl 6, you can declare the iteration variable of the for to have the desired attributes. For a given you could use an expression to cause a copy to be made: given (0+$x) {... and if you write a function that just returns its argument, and you call that function copy, then you could write given (copy $x) ... nice and sugary. It would be harder to impose aliasing if the normal case didn't.

        But since it's been this way for a while now, I think any "fix" will be to use a pragma to choose the behavior.