Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by dragonchild (Archbishop)
on Sep 19, 2004 at 13:56 UTC ( [id://392181]=note: print w/replies, xml ) Need Help??


in reply to Re^4: 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?

What I meant was that the subroutine, if written as a named subroutine, isn't a closure. By making it lexical and anonymous you make it one though, ...

That makes no sense to me, but I have a fuzzy definition of Perl's closures, so that may not be surprising.

The confusing part is this.

{ my $foo = 1; sub get_and_inc_foo { $foo++ } }

I was always told that get_and_inc_foo() is a closure over $foo. But, it's neither anonymous nor lexical, so how does that work?

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

(Now that I think of it, isn't this the reason why you cannot have globals when using ModRegistry?)

------
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

  • Comment on Re^5: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
  • Download Code

Replies are listed 'Best First'.
Re^6: Is it possible to create a sub exclusive to a sub? (nasty memory leak)
by ihb (Deacon) on Sep 19, 2004 at 14:30 UTC

    &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!

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://392181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found