Hi Toby,<div><br></div><div>I had a look at the latest perl 5.13.1. Someone's just fixed it!</div><div><br></div><div>From perl5131delta.pod:</div><div><br></div><div><div> "given" return values</div><div>
Starting from this release, "given" blocks returns the last evaluated</div><div> expression, or an empty list if the block was exited by "break". Thus</div><div> you can now write:</div>
<div><br></div><div> my $type = do {</div><div> given ($num) {</div><div> break when undef;</div><div> 'integer' when /^[+-]?[0-9]+$/;</div><div> 'float' when /^[+-]?[0-9]+(?:\.[0-9]+)?$/;</div>
<div> 'unknown';</div><div> }</div><div> };</div><div><br></div><div> See "Return value" in perlsyn for details.</div></div><div><br></div><div>Cheers</div><div>- David<br>
<br><div class="gmail_quote">On Thu, Jun 3, 2010 at 8:17 AM, David Warring <span dir="ltr"><<a href="mailto:david.warring@gmail.com">david.warring@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Toby,<div><br></div><div>I've noticed this. It's supposed to be a back-port of a Perl 6 feature.</div><div><br></div><div>I grokked the Perl 6 test suite and fond the following in t/spec/S04-statements/given.t</div>
<div><br></div><div>==================================</div><div><div> # from apocalypse 4</div><div> #?rakudo skip 'parsefail on each(... ; ...)'</div><div> {</div><div><span style="white-space:pre">        </span># simple example L<S04/"Switch statements" /You don't have to use an explicit default/></div>
<div><span style="white-space:pre">        </span>for each(("T", "E", 5) ; (10, 11, 5)) -> $digit, $expected {</div><div><span style="white-space:pre">        </span> my $result_a = do given $digit {</div>
<div><span style="white-space:pre">                </span>when "T" { 10 }</div><div><span style="white-space:pre">                </span>when "E" { 11 }</div><div><span style="white-space:pre">                </span>$digit</div>
<div><span style="white-space:pre">        </span> };</div><div><br></div><div><span style="white-space:pre">        </span> my $result_b = do given $digit {</div><div><span style="white-space:pre">                </span>when "T" { 10 }</div>
<div><span style="white-space:pre">                </span>when "E" { 11 }</div><div><span style="white-space:pre">                </span>default { $digit }</div><div><span style="white-space:pre">        </span> };</div>
<div><br></div><div><span style="white-space:pre">        </span> is($result_a, $expected, "result of $digit using implicit default {} is $expected");</div><div><span style="white-space:pre">        </span> is($result_b, $expected, "result of $digit using explicit default {} is $expected");</div>
<div><span style="white-space:pre">        </span>}</div><div> }</div></div><div>===================================</div><div><br></div><div>So it looks as if this is inconsistently implemented across Perl 6 & 5.</div>
<div><br></div><div>I would've thought that Perl 5 should behave the same way. Also it wouldn't hurt to back-port the 'given' tests from rakudo.</div><div>- David</div><div><div></div><div class="h5"><div>
<br><div class="gmail_quote">On Wed, Jun 2, 2010 at 3:16 PM, Toby Corkindale <span dir="ltr"><<a href="mailto:toby.corkindale@strategicdata.com.au" target="_blank">toby.corkindale@strategicdata.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">An annoyance right now is that the given/when syntax does not return the values in the code block executed, so you can't use them inside grep,etc.<br>
<br>
Has anyone else noticed a way around that? (Apart from wrapping it in an anonymous subroutine and then returing out of the when blocks)<br>
_______________________________________________<br>
Melbourne-pm mailing list<br>
<a href="mailto:Melbourne-pm@pm.org" target="_blank">Melbourne-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/melbourne-pm" target="_blank">http://mail.pm.org/mailman/listinfo/melbourne-pm</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>