<?xml version="1.0" encoding="windows-1252"?>
<node id="988657" title="Re: given and for" created="2012-08-21 05:02:32" updated="2012-08-21 05:02:32">
<type id="11">
note</type>
<author id="616540">
moritz</author>
<data>
<field name="doctext">
&lt;p&gt;First of all &lt;c&gt;for&lt;/c&gt; and &lt;c&gt;given&lt;/c&gt; are not equivalent, they differ subtly in the scoping of their [doc://$_] variable:

&lt;code&gt;
use strict;
use warnings;
use 5.010;

$_ = 42;
sub f { say }

given (23) { f }
for   (23) { f }
__END__
42
23
&lt;/code&gt;

&lt;p&gt;So &lt;c&gt;given&lt;/c&gt; creates a new lexical $_, while &lt;c&gt;for&lt;/c&gt; [doc://local]izes the existing $_, which results in dynamic scoping.

&lt;p&gt;But the real reason is that code must be readable to humans, and &lt;c&gt;for&lt;/c&gt; has the connotation of a loop, whereas &lt;c&gt;given&lt;/c&gt; makes it implicit that it's one value that is talked about. So it's all about intent.&lt;/p&gt; 

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-616540"&gt;
[http://perl6.org/|Perl 6 - the future is here, just unevenly distributed]
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
988652</field>
<field name="parent_node">
988652</field>
</data>
</node>
