Bracing against the wind  

Thursday, February 12, 2009

mod_perl and file-scoped lexicals don't work

After months of debugging, and rewriting my code and not understanding what was going on, I discovered a horrible, sneaky bug in mod_perl.

File-scoped lexicals, when used in subroutines, are bound to the first-occurrence of the variable. Because of the way mod_perl works, this means that the first time you run a CGI program *within a given Apache process* it will work as expected.

The second time and thereafter, those file-scoped lexicals will be bound to previous instances, and will contain previous values.

OUCH! So code like this won't work:

my $var = CGI::param('var');

sub x {
print $var;

Note to self: NEVER use file-scoped lexicals in mod_perl unless you want to risk suicide and sleepless nights.


[View/Post Comments] [Digg] [] [Stumble]

Home | Email me when this weblog updates: | View Archive

(C) 2002 Erik Aronesty/DocumentRoot.Com. Right to copy, without attribution, is given freely to anyone for any reason.

Listed on BlogShares | Bloghop: the best pretty good | Blogarama | Technorati | Blogwise