Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re^3: Perl to Ruby translator?

by BUU (Prior)
on Oct 10, 2003 at 19:40 UTC ( #298382=note: print w/replies, xml ) Need Help??


in reply to Re: Re^3: Perl to Ruby translator?
in thread Perl to Ruby translator?

For at least my own education, if no one elses, how exactly is that a closure and why are people getting screwed doing it? It seems fairly simple to me..

Replies are listed 'Best First'.
Re: Re: Re: Re^3: Perl to Ruby translator?
by perrin (Chancellor) on Oct 10, 2003 at 19:51 UTC
    It's a closure because the sub foo refers to $q, which is a lexical variable delcared in the enclosing scope. Now, for the rest of the life of this Perl interpreter, the $q inside of the foo subroutine will always have the same value it did the first time this code was run, no matter what you do in the main part of this program. That means you will get crazy results where CGI.pm appears to be caching data from previous requests.
      I was going to tell you why it's not a closure, but fortunately I checked first, and of course, you are right.

      I doubt your central assertion that this sort of thing happens all the time though. How many times do you see named subroutines declared inside loops? Almost never. And I have never heard of anyone accidentally creating a closure.

      Personally, I find using closures a liberating experience, and since they make some hard things easy, actually reduce bugs.

        You're quite incorrect. In most cases perrin's example would have been file scope. That sort of accidental closure is excessively common. So much so that I seem to recall there being a sort of relaxing about the rules of scoping for file scoped my() lexicals but I don't recall what it was or where I heard it.

        Who said this was in a loop? This is a CGI script, which exhibits this problem when run in any persistent environment like mod_perl, PerlEx, etc. These are a very common target for Perl code, so this comes up all the time. I have helped several people with this exact problem on PerlMonks, and many more on the mod_perl list. They never believe me when I tell them it's a closure.

        It happens in other situations too, but this is the most common one I've seen.

Re: Re: Re: Re^3: Perl to Ruby translator?
by kelan (Deacon) on Oct 10, 2003 at 19:52 UTC

    It's a closure because it contains a reference to a lexical variable ($q) from a parent scope. It could cause problems if later you reassigned $q and thought it would change inside the sub, or thought it go away after the scope of $q ended.

    kelan


    Perl6 Grammar Student

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-02-03 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?