News for hackers. Stuff that matters.
Re:The Three Great Virtues of a Programmer: Laziness, Impati... by fanf2 on Sunday June 28, 2015 @06:10PM (#9795310)
One weirdness I encountered recently with nested functions:

  sub a {
    my $v;
    sub b {
      # captures only the first
      # instance of $v
    }
  }
This is because named subroutines are created once and variable captures are resolved at that time. I expected the more normal capture semantics you get with anonymous subs, like

  sub a {
    my $v;
    my $b = sub {
      # a new $b each time
      # captures each $v
    }
  }
Re:The Three Great Virtues of a Programmer: Laziness, Impati... by AdieuToLogic on Monday June 29, 2015 @03:12AM (#9796780)
The lexical scoping rules are precisely what causes this behaviour. The first example defines a nested function which has access to the variables in scope when defined (as you remark).

It's little different than this block defined outside of any function:

  {
  my $v;

  sub b { ... }
  }
Whereas an anonymous function is "defined" each time the enclosing scope is evaluated.

For more info, see: http://www.perlmonks.org/?node_id=389319

HTH

Re:The Three Great Virtues of a Programmer: Laziness, Impati... by jbert on Sunday June 28, 2015 @06:51PM (#9795438)
Thanks - useful point. But, I think that's more of an oddity of nested, named subs than anything else. The usual anonymous subs close as you'd expect (as you say).