Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by perrin (Chancellor)
on Oct 10, 2003 at 19:51 UTC ( [id://298388]=note: print w/replies, xml ) Need Help??


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

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.
  • Comment on Re: Re: Re: Re^3: Perl to Ruby translator?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re^3: Perl to Ruby translator?
by sandfly (Beadle) on Oct 10, 2003 at 21:23 UTC
    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.

        Well, now I understand the mechanics behind it becoming a closure, but why on earth would perl make it a closure? That doesn't make any sense to me. Why wouldn't the sub just use whatever $q it can find in the closest scope to it?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found