Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Perl Phantom Variables in FastCGI

Expand Messages
  • Shlomi Fish
    Hi John, a few comments on your code: On Thu, 27 Sep 2012 00:46:13 -0000 ... Always add use strict; and use warnings; :
    Message 1 of 4 , Sep 27, 2012
    • 0 Attachment
      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,
      > 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.
      >
      >
      >
      > #!/usr/local/bin/perl
      >

      Always add "use strict;" and "use warnings;":

      http://perl-begin.org/tutorials/bad-elements/#no-strict-and-warnings

      > $a = localtime();
      >

      Declare the variable using "my" and don't call it "$a":

      http://perl-begin.org/tutorials/bad-elements/#vars-a-and-b

      > $test_file = 'test.txt';
      > open DATA, ">>$test_file" or die "can't open $test_file $!";

      1. Use three-args open.

      2. Use lexical file handles

      See:

      http://perl-begin.org/tutorials/bad-elements/#open-function-style

      3. The *DATA file handle has a special meaning in Perl, and should not
      be over-rided.

      > print DATA $a.'<br>';
      > 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 ;

      Always use <<'EOF' or <<"EOF" for avoiding ambiguity:

      http://perl-begin.org/tutorials/bad-elements/#string-notation

      > Content-type: text/html
      >
      > <html><head>
      > <META http-equiv="EXPIRES" CONTENT="0">
      > <meta http-equiv="pragma" content="no-cache">
      > </head><body>
      > <p>@new_data</p>
      > </body></html>
      > EOF
      >

      If you're going to interpolate @new_data into a string, you should slurp it
      as a string using File::Slurp or whatever.

      > $c = 'This line should never show up';
      > exit;

      Well, with package-scope variables, no wonder why FastCGI preserves them.

      Regards,

      Shlomi Fish

      --
      -----------------------------------------------------------------
      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 .
    Your message has been successfully submitted and would be delivered to recipients shortly.