Re: [PBML] Perl Phantom Variables in FastCGI
- Hi John,
a few comments on your code:
On Thu, 27 Sep 2012 00:46:13 -0000
"johnlikeglass" <js_dziel@...> wrote:
> Take a look at the simple script below. $c should never show up,Always add "use strict;" and "use warnings;":
> right? On FastCGI servers it often does! Try loading it, calling it,
> and hitting F5 enough, and the text in $c gets displayed. Call them
> Phantom, or Zombie, but they live on after the script exits and may
> show up in the next script called. So, what's going on? Well....
> On a server configured for FastCGI, the perl operating system stays
> alive so that it does not need to be reloaded every time a script is
> called, improving performance. Unfortunately, if you are running your
> script on such a server, you have to take some extra precautions.
> Using reset 'a-z'; will clear your lower case variables,
> unfortunately, the upper case version will also clear the
> environmental variables which you may be using. One workaround on
> this is to kill the variables as the script exits, for example:
> reset 'a-z'; reset 'A-Z'; exit;
> There are other ways to do this for sure, but clearing on exit is an
> easy way to upgrade scripts you may already have written and recently
> began to misbehave.
> $a = localtime();Declare the variable using "my" and don't call it "$a":
> $test_file = 'test.txt';1. Use three-args open.
> open DATA, ">>$test_file" or die "can't open $test_file $!";
2. Use lexical file handles
3. The *DATA file handle has a special meaning in Perl, and should not
> print DATA $a.'<br>';Always use <<'EOF' or <<"EOF" for avoiding ambiguity:
> print DATA $c.'<br>';
> close (DATA);
> open DATA, "<$test_file" or die "can't open $test_file $!";
> @new_data = <DATA>;
> close (DATA);
> #output page
> print <<EOF ;
> Content-type: text/htmlIf you're going to interpolate @new_data into a string, you should slurp it
> <META http-equiv="EXPIRES" CONTENT="0">
> <meta http-equiv="pragma" content="no-cache">
as a string using File::Slurp or whatever.
> $c = 'This line should never show up';Well, with package-scope variables, no wonder why FastCGI preserves them.
Shlomi Fish http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs
Knuth is not God! Typing “God” into Google and pressing “I’m Feeling Lucky”
will not lead you to his homepage.
Please reply to list if it's a mailing list post - http://shlom.in/reply .