in reply to Re^2: Syntax question about closures and Perl 'sort'
in thread Syntax question about closures and Perl 'sort'

"isn't really a closure.."

Exactly, even though it technically is a real closure. The unnamed sub will remember the value of $type as long as the sub exists (in that sense it is a closure). But since $type isn't used anywhere in the unnamed sub, it is of no consequence.

  • Comment on Re^3: Syntax question about closures and Perl 'sort'

Replies are listed 'Best First'.
Re^4: Syntax question about closures and Perl 'sort'
by tilly (Archbishop) on Apr 04, 2009 at 04:41 UTC
    Actually that is not quite right. If a variable is not visibly used in a returned subroutine, Perl cleans it up. You can verify that with this example:
    sub create_closure { my $x = "x"; my $y = "y"; return sub { $y .= "y"; eval q{print "x: $x\ny: $y\n"}; }; } create_closure()->();
    And you will find that $x is not properly remembered, but $y is.

      Interesting fact of the disturbing kind ;-)

      The language designer or a programmer in that language would still call it a closure. That the implementation optimized it away would (normally) only interest the implementor of the language. That it can be revealed through an eval I would call an implementation bug. Ideally any optimization must not change the result of a programm execution.