On Nov 14, 1:11 am, shlo...@iglu.org.il (Shlomi Fish) wrote: > 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.
Right... or simply get rid of the statement qualifier 'unless $closure' which'll work too and is what you'll have to do in any case. (technically, you could declare 'my $closure' both in/outside the loop leaving the statement qualifier as is but that's horrible ) You could just 'my $closure' solely inside the loop too. Declaring once outside the loop with an outer lexical seems less satisfactory since it loosens the 'tightest lexical scope' best practice. -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/