in reply to Re^3: closure clarity, please
in thread closure clarity, please

Different results.

According to what you said, the following should print "0" instead of "1":

my $x; BEGIN { $x = 0 } my $f = sub { $x }; ++$x; print $f->();

Let's say you didn't mean the above. According to what you said, the following should print "11" instead of "12":

for (1..2) { my $x = $_; print sub { $x }->(); }

Replies are listed 'Best First'.
Re^5: closure clarity, please
by BrowserUk (Patriarch) on Nov 24, 2009 at 17:08 UTC

    As JadeNB put it:

    * I'm intentionally being a bit sloppy here: as ikegami mentions, closures close over variables, not values; but, as the warning that you quote mentions, the actual variable that is closed over will no longer be accessible at the end of the subroutine invocation, so that there is no further way to change its value.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.