Re^2: Ghostly subroutine variables haunting me in subsequent calls
by blazar (Canon) on Oct 25, 2005 at 16:59 UTC
|
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.
| [reply] |
|
|
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.
| [reply] |
|
|
Ack! It's a reasonable price to pay for such a powerful tool though, if you ask me...
| [reply] |
|
|
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.";
| [reply] |
|
|
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?
| [reply] |
Re^2: Ghostly subroutine variables haunting me in subsequent calls
by moonunit (Initiate) on Oct 26, 2005 at 12:04 UTC
|
In response to all this lively discourse, the problem continues in spite of a suitably scoped my @pileofinchis array. However, I have now found at least one problem which was indeed a scoping issue with an array.
use strict scares me :(
Thanks for your assistance | [reply] [d/l] |
|
|
use strict scares me
Disuse of strictures scares me, at least when its absence is unaccompanied by a darn good reason.
A few brief lines of code is one thing. But if you're serious about scripting in Perl, you'll need to learn to live within the criteria set by use strict;
As for your problem at hand, here's my recommendation. Boil your script and subroutine down to a minimal amount of code that will actually compile and run in such a way as to demonstrate to us the problem. Cut and paste it into a followup in this thread so that we can look it over. We need to see actual code that actually runs, that actually replicates the problem, and it needs to be under 30 lines of code. Chances are good that while you work at creating a test example you'll end up discovering the problem on your own anyway, but if not, post it and we'll help further.
| [reply] [d/l] [select] |
|
|
In response to all this lively discourse, the problem continues in spite of a suitably scoped my @pileofinchis array.
It may not be the actual problem, but as I already wrote in my reply to BrowserUk's one, I'd be curious to know what you are doing with that reference of it that you return from your sub.
use strict scares me
Not only, just like davido "disuse of strictures scares me", but your being scared of use strict; scares me too, since there's really nothing to be scared of, that is: in letting perl help you to write good code.
| [reply] [d/l] |