In a message dated Fri, 25 Apr 2008, Moritz Lenz writes:

Paul Fenwick <pjf <at> perltraining.com.au> writes:

for ($foo) {
        when ($_ < 500)  { ++$_ }
        when ($_ > 1000) { --$_ }
        default          { say "Just right $_" }
}

Ahh... that's exactly what I was looking for.  Thanks.

Makes you wonder why the 'given' keyword was added, when for/when is so close...

I'd assume 'given' provides scalar context onto its argument, 'for'
obviously provides list context.

But I guess the main difference is that 'for' is associated with
iteration, and IMHO it feels unnatural to iterate over one item.

In 5.10, given seems to copy its argument, whereas for aliases it. (I haven't looked at the code; maybe it's COW-ing it.) If you add a C<say "Value is now $foo";> to the end of the below program, and then change C<for> to C<given> and run the program with values of $foo less than 500 or greater than 1000, you'll see the difference: with C<for>, the value remains changed after the block, with C<given> it is not.

I do not believe this distinction is true in Perl 6, is it?

Trey

Reply via email to