in reply to Re: Catching closures
in thread Catching closures

What the OP is refering to is the fact that under mod_perl your ordinary code get turned into a subroutine (if it is run as a registry script that is) and all of a sudden the subroutines in your code will unexpectedly close over lexical variables defined outside of their scope. This gives all kinds of strange effects such as your webpages "remembering" previous values and not resetting them.

Closures are not bad, but if they happen unexpectedly they are quite bothersome and difficult to debug.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^3: Catching closures
by runrig (Abbot) on Mar 05, 2008 at 22:09 UTC
    Except that those types of closures are easy to find if you have warnings turned on.
      That is indeed the advice the mod_perl book gives.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^3: Catching closures
by Joost (Canon) on Mar 05, 2008 at 22:12 UTC
    If the OP is referring to that, he should just use warnings and look at the error log, instead of trying to prevent all the useful uses of closures.

    For the record, I intentionally use closures in a mod_perl environment all the time, and as far as I can see there aren't any more problems with closures than with other "complex" use of references.

      I'm concerned about things like putting the Apache::Request object into a closure, and getting issues like this : Calls to Apache::Request's param method sometimes end the program and return no data.

      And generally, if any other variables persist from request to request just because I used them in a closure, that would be annoying. (Of course, sometimes I want things to persist, and closures are helpful, but I don't do that in the context of calling a method for doing a database transaction).

      e.g. I wanted to avoid doing this :
      my $object; do_transaction(code => sub {....$object->update..})

      On second though, maybe the above is okay, since once the anonymous sub goes away, the reference to $object does too, so maybe I'm being unnecessarily cautious.
        Whether that code is OK all depends on what happens to $object.

        Note that potential problems with persistent data in mod_perl aren't limited to closures. All kinds of bugs can creep in if you're not taking the persistent nature of mod_perl into account. If you've done mostly CGI programming it can take a while to get comfortable with that, but it's really not much different from writing any other persistent program.

        Detecting unintended closures may be a good thing, but I suggest you just use it in development and turn it off in production and in situations where you may actually want to use closures. That way you can write and test with all kinds of (inefficient) hand holding, and run the code with good performance when it's tested.

        In any case, it's useful to have a good understanding of closures, firstly just because they're a very powerful tool, and secondly because if you know how closures work you're not very likely to make the kinds of mistakes you're detecting here.

      "intentionally" being the operative word here!

      I guess the OP does not use closures in his code and wants to guard against the unexpected ones.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James