MySQL and Perl for the Web: Chapter 3 Section 3 (3/6)
[previous][next] |
Improving Performance with mod_perl
When you initialize a variable, make sure it's initialized every time your script executes. The following bit of code sets $x
to 1, but only if some_test()
succeeds:
use vars qw($x);
$x = 1 if some_test ();
The problem is that $x
remains set to 1 even if some_test()
fails on every subsequent invocation of the script. Suppose what some_test()
does is check a user-supplied password and return true if the password is okay. After $x
gets set, it remains set, even if the next person to come along provides an incorrect password! The following code handles the situation properly:
use vars qw($x);
$x = 0;
$x = 1 if some_test ();
So does this:
use vars qw($x);
$x = some_test () ? 1 : 0;
The basic principle here is that you don't want to start making decisions based on variable values until you know they've been initialized properly.
When you're trying to determine the cause of problems due to shared script environments, you may find it useful to stop Apache and restart it using httpd -X
. The -X
option tells Apache to run as a single process rather than in the usual parent-children configuration. That makes shared-environment problems show up more quickly. (You should do this only on a development server, not a production server.)
[previous][next] |
Created: July 13, 2001
Revised: July 13, 2001