in reply to Re: Ghostly subroutine variables haunting me in subsequent calls
in thread Ghostly subroutine variables haunting me in subsequent calls

In my opinion, one of the worst things you can do with respect to subroutines is allow them to accept values through broader-scope "osmosis". My opinion is that all subroutine variables should be lexically scoped to the boundries of the subroutine.
Huh?!? That's exactly what a closure does. And closures are useful!

Update: I hadn't noticed you mention closures yourself. I apologize. But in any case some caution is required IMHO before stating such a claim as the above. I.e. I wouldn't talk about "one of the worst things you can do...", but of "you must pay a lot of attention when you...", instead.

  • Comment on Re^2: Ghostly subroutine variables haunting me in subsequent calls

Replies are listed 'Best First'.
Re^3: Ghostly subroutine variables haunting me in subsequent calls
by perrin (Chancellor) on Oct 25, 2005 at 17:26 UTC
    People often create closures by accident, not realizing that they work this way. I see it a few times a week on the mod_perl list. Closures may have uses, but they are clearly a major source of bugs in perl code.
      Ack! It's a reasonable price to pay for such a powerful tool though, if you ask me...
Re^3: Ghostly subroutine variables haunting me in subsequent calls
by sauoq (Abbot) on Oct 25, 2005 at 19:14 UTC

    Presumably, if you are creating a closure for a useful purpose, you already know davido's advice doesn't apply. I agree with him that it is "one of the worst things you can do..." unless, of course, you happen to be doing it on purpose. I don't think he really needs to include that caveat... Do you?

    † Well, one of the worse in terms of creating hard to find bugs in your Perl programs, anyway. It's not like homicide or something... (-:

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^3: Ghostly subroutine variables haunting me in subsequent calls
by davido (Cardinal) on Oct 25, 2005 at 17:06 UTC

    I don't understand how you could take issue with my statement if you read the entire short paragraph, including this part:

    There are exceptions; closures for example. But you have to be careful whenever a value is passed to a sub via broader scope instead of via parameter list.

    How could you possibly name closures as an example of how my assertion is outlandish if I, in the very same paragraph named closures as one exception? I even acknowleged that there are other exceptions too. You did read the entire paragraph right? Or should I have enumerated all exceptions within the same sentence as the assertion?


    Dave