XP is just a number | |
PerlMonks |
Re: mod_perl / Apache::Registry accidental closuresby rhesa (Vicar) |
on Jul 21, 2006 at 01:43 UTC ( [id://562756]=note: print w/replies, xml ) | Need Help?? |
Good explanation. I'd suggest adding something that explicitly explains how to
avoid this problem.
The basic solution is to wrap all top-level code that isn't inside a subroutine in a new sub, and then fixing all the problems with global variables. In this case, something like:
That will not run, so we need to fix the reference to $foo in badness:
Real-life scripts will probably be harder, so make sure to run perl -c your_script.pl early and often :^) Update: As ikegami pointed out, using my $foo = shift in badness runs, but it doesn't act the same. The (*)clever trick suggested by ikegami is a good way to circumvent that. Later iterations should probably move to a more functional approach, by making the subroutines free of side effects.
In Section
Tutorials
|
|