in reply to closure clarity, please

Why don't the docs mention that a named sub can create a closure?

There's a whole section on it called "Persistent variables with closures".

Could someone give a blow by blow description of what's happening?

In short, named subs capture at compile time.

You seem to believe that subs are scoped to the block that contain them. That's not true. Subs are global.

Replies are listed 'Best First'.
Re^2: closure clarity, please
by 7stud (Deacon) on Nov 24, 2009 at 22:42 UTC

    I did not know that a named sub is global no matter where it is defined, but how is that relevant? I definitely was looking at the sub's containing scope to determine what variables the sub could see at the time it's closure was formed.

    However, knowing that a named sub is global no matter where it is defined, and knowing that a closure around a sub is formed at compile time doesn't give me any insight into which variables are contained in the closure. How do you figure that out?

    And why does a my variable that is in scope inside the loop's block (in my last example) not make it into the closure?

      How do you figure that out?

      First, don't put named subs inside loops or other subs. It makes no sense. The latter will even warn ("will not stay shared") if it captures anything from the sub its in.

      Then the answer is simple: All variables declared before it in a scope that hasn't been closed.

      my $x; <- this one. { my $y; <- not this one. ... } { my $z; <- this one. sub f { ... } }

      For anonymous subs, it's a different story. Feel free to use them in loops and other subs. They'll pick up the variables that are visible when the sub is executed.

      And why does a my variable that is in scope inside the loop's block

      for localizes its iterator variable. Localizing is temporary replacing a variable with a new one.

      You captured $num before the loop started. This is different from the $num of the first pass, which is different from $num of the second pass, ..., which is different from $num of the fifth pass.

      (Ok, I lied. I believe an optimisation will actually cause the variable of the first pass to be reused for the subsequent passes, but that's transparent here.)

        my $x; <- this one. { my $y; <- not this one. ... } { my $z; <- this one. sub f { ... } }
        A minor quibble: We can't entirely ignore intervening code. For example,
        my $x = 'shadowed'; # <- invisible { my $x = 'shadowing'; # <- visible sub f { say "I only see the \$x that's $x." }; }
Re^2: closure clarity, please
by 7stud (Deacon) on Nov 26, 2009 at 17:01 UTC
    doing some more experimenting...