MySQL and Perl for the Web: Chapter 3 Section 3 (3/6) | WebReference

MySQL and Perl for the Web: Chapter 3 Section 3 (3/6)

To page 1To page 2current pageTo page 4To page 5To page 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.)

To page 1To page 2current pageTo page 4To page 5To page 6
[previous][next]


Created: July 13, 2001
Revised: July 13, 2001

URL: https://webreference.com/programming/perl/mysqlperl/chap3/3/3.html