Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
That is not so. First of all you misunderstood what was going on with runrig's nitpickery. Consider the following piece of code:
my $anon1 = outer(1); my $anon2 = outer(2); inner(); $anon1->(3); $anon2->(4); inner(); $anon1->(); $anon2->(); sub outer { my $y = my $x = shift; sub inner { print "Inner thinks x is '$x'.\n"; } return sub { print "Anon $y thinks x is '$x'.\n"; if (@_) { $x = shift; print "Anon $y reset x to '$x'.\n"; } }; } __DATA__ Inner thinks x is '1'. Anon 1 thinks x is '1'. Anon 1 reset x to '3'. Anon 2 thinks x is '2'. Anon 2 reset x to '4'. Inner thinks x is '3'. Anon 1 thinks x is '3'. Anon 2 thinks x is '4'.
If you run this you will see what is really going on. The inner subroutine generates a closure. However there are 2 instances of the environment available, and only one global name. That global name therefore closes the subroutine over the first instance of the scope encountered. Subsequent calls to the outer function are not bound to the inner named subroutine because they are generating scopes that the innner one does not share.

This behaviour is a correct and logical resolution to the ambiguities between global naming and lexical scopes. But it tends not to do what is expected.

However you did not just misunderstand what was going on with the issue that demerphq and runrig pointed out. You added to that the accusation that, Of course it is correct to point out that Perl's closures are not exactly equivalent to what is commonly referred to as a closure. This accusation is, your protestations of obviousness notwithstanding, wrong. They work perfectly well as long as the function names are scoped lexically, just like the variables bound into the function's environment, so there is no ambiguity about which set of variables to bind the function to.

I think this has all been a misunderstanding on your part. Hopefully now you see how closures work in Perl, see how they really are the same as in other languages, understand why what demerphq noted and then runrig followed up on is a problem in Perl, and understand why the problem does not have any easy solution.

UPDATE (much later): Added the output. Also I should point out that it is exactly this issue which causes Perl to issue a "Variable ____ will not stay shared" warning in this case.


In reply to Re (tilly) 9: Why are closures cool? by tilly
in thread Why are closures cool? by mr.dunstan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-03-28 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found