Hi Charles,

On Sunday 14 November 2010 01:47:36 C.DeRykus wrote:
> On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote:
> Not lots shorter but you could use a closure to hide
> the calculation:
> 
> my $mask;
> for my $flags ( ... ) {
>      $mask = sub { return ($flags & $_[0]) == $_[0] }
>             unless $mask;
>      given( $flags ) {
>             when ( $mask->($one_and_three)  ) { ... }
>             when ( $mask->($zero_and_four)   ) { ... }
>             ...
>      }
> }
> 

This won't work properly because the closure traps the initial value of 
"$flags". For example:

[code]
#!/usr/bin/perl

use strict;
use warnings;

my $closure;

foreach my $name (qw(Sophie Jack Charles Dan Rachel))
{
    $closure = sub { print "Hello $name!\n" ; } unless $closure;

    $closure->();
}
[/code]

This prints "Hello Sophie!" five times. Either redeclare the closure on every 
iteration, or declare it once while using a more outer lexical variable.

Regards,

        Shlomi Fish

> --
> Charles DeRykus

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to