On Mon, Aug 14, 2006 at 04:01:47PM +0000, Luke Palmer wrote:
> What do these do?
> 
>  for 1,2 {
>      my $code = {
>          my $x;
>          BEGIN { $x = 42 }
>          $x;
>      };
>      say $code();
>  }

Assuming that variables are available immediately as
they are parsed and that BEGIN blocks disappear as soon as they
execute, I'd expect that the first $code would be equivalent to 

my $code = { my $x = 42; $x; };

and the second code would be equivalent to

my $code = { my $x ; $x; };

So, I guess the output would be 

42
            # this line intentionally left blank  :-)


>  for 1,2 {
>      my $code = {
>          state $x;
>          BEGIN { $x = 42 }  # mind you, not FIRST
>          $x++;
>      };
>      say $code();
>      say $code();
>  }

Same thing here, except because it's a state variable, it keeps it's
value between invocations, so the output would be:

42
43
        # again, blank on purpose
1

>  for 1,2 -> $x {
>      END { say $x }
>  }

For this one I'd guess that a solitary "2" is output.  The END block
closed over the $x and the last value that $x obtained was 2.

my humble guesses,

-Scott
-- 
Jonathan Scott Duff
[EMAIL PROTECTED]

Reply via email to