in reply to Re^5: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
in thread Is it possible to create a sub exclusive to a sub?

&get_and_inc_foo is a closure, because it binds a lexical variable defined outside of it.

If my example is a closure, then aren't all subroutines closures over the file-scoped lexicals in the file they're defined in?

If they don't use any lexicals defined outside them, they're not closures. sub foo { 1 } isn't a closure no matter where it's defined.

my $foo; sub closure { $foo } sub not_closure { 1 }
What I mean with making the subroutine a lexical anonymous subroutine is the following.
my $foo; # lexical $foo = sub { $foo->() }; # closure local $bar; # dynamical $bar = sub { $bar->() }; # not a closure

ihb

Read argumentation in its context!

Replies are listed 'Best First'.
Re^7: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
by dragonchild (Archbishop) on Sep 19, 2004 at 18:30 UTC
    So, if I had something like:
    package main; my $foo = 1; sub foobar { $foo++; }

    Does that make foobar() a closure?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Yes.

      ihb

      Read argumentation in its context!