Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

mod_perl - local vs my...

by smullis (Pilgrim)
on Nov 09, 2004 at 14:34 UTC ( #406344=perlquestion: print w/ replies, xml ) Need Help??
smullis has asked for the wisdom of the Perl Monks concerning the following question:

Hello all....

Help!

I have a mod_perl (v2) app that - among other things - reads a local config file based on the contents of an encrypted cookie value set by a frontend server. It all seems to work beautifully....

Except!

If I click stop / break the browser session while a page is loading then the subsequent requests seem to get themselves in a bit of a muddle (i.e. the results reflect the wrong config file).

After some reading here it seems that this code is the problem:

sub read_the_config() { open(CFG, "<$file") || log_error("Problem reading $file - $!"); while (<CFG>) { chomp; # remove newline s/\s+$//; # remove trailing space s/\s+/ /g; # collapse white spaces to ' ' next if /^ *\#/; # skip comment lines next if /^\s*$/; # skip empty lines if (/^ \S/) { # multiline options $lines[$#lines] .= $_; } else { push @lines, $_; } } close CFG; }

Are there any simple mod_perl recipes for ensuring that all variables are cleared on each subsequent page request?

Could it be as simple as using local within the function to force the variable to be undef when that block is left?

i.e.

sub read_the_config() { local $localfile = $file; open(CFG, "<$localfile") || log_error("Problem reading $file - $!"); while (<CFG>) { chomp; # remove newline s/\s+$//; # remove trailing space s/\s+/ /g; # collapse white spaces to ' ' next if /^ *\#/; # skip comment lines next if /^\s*$/; # skip empty lines if (/^ \S/) { # multiline options $lines[$#lines] .= $_; } else { push @lines, $_; } } close CFG; }

Or is there some other such Perl Magick I can employ...


Many thanks in advance for any pointers / tips / constructive criticism...

Cheers

SM

UPDATE:

Apologies - When originally writing this question I left out some of the code thinking (incorrectly it seems) that it would confuse the issue...

Here it is in full...

sub read_the_config($$$$) { my ($file, $def, $cfgref, $order) = @_; my @lines; open(CFG, "<$file") || log_error("Problem reading $file - $!"); while (<CFG>) { chomp; # remove newline s/\s+$//; # remove trailing space s/\s+/ /g; # collapse white spaces to ' ' next if /^ *\#/; # skip comment lines next if /^\s*$/; # skip empty lines next if /^TopTalker*/; if (/^ \S/) { # multiline options $lines[$#lines] .= $_; } else { push @lines, $_; } } close CFG; foreach (@lines) { do all sorts of groovy stuff....} }
I presume the suggestions of Ikegami are still relevant? i.e.:
... local *CFG; open(CFG, "<$file") || log_error("Problem reading $file - $!"); while (<CFG>) { ... return \@lines;

Cheers

SM

UPDATE 2:

After spending some time trying the solutions below I have had no luck. A Ctrl-f5 refresh (btw I have client side caching turned off and have set the relevnat Pragma: Nocache header vars) seems to make the mod_perl app randomly jump from one previously loaded config to another....

When I restart httpd it always works as expected.

Does anyone have any suggestions as to how I can force clearing all variables within the mod_perl app for subsequent request? I am using cookies for persistence but obviously I can set that each time the user instantiates the page...

....Help!!!

Comment on mod_perl - local vs my...
Select or Download Code
Re: mod_perl leaking variables?
by ikegami (Pope) on Nov 09, 2004 at 14:46 UTC

    For starters, don't use prototypes unless required. In other words, drop the () on the sub line.

    It would probably be better to pass the file name as a parameter.

    You should localize your file handle.

    I see that @lines is not declared either. I don't see any reason for this to be a global. This is probably the reason you're seeing the wrong config file.

    sub read_the_config { my ($file) = @_; my @lines; local *CFG; open(CFG, "<$file") || log_error("Problem reading $file - $!"); while (<CFG>) { ... return \@lines; }

    In newer version of Perl (don't know which exactly, but <updated>at least since 5.6.1</updated>), you can get rid of the local completely:

    sub read_the_config { my ($file) = @_; my @lines; my $cfg_fh; open($cfg_fh, "<$file") || log_error("Problem reading $file - $!"); while (<$cfg_fh>) { ... return \@lines; }

      In newer version of Perl (don't know which exactly, but probably 5.8.0 and up), you can get rid of the local completely:

      I know this sytax works in perl 5.6.1

      May the Force be with you
Re: mod_perl - local vs my...
by Taulmarill (Deacon) on Nov 09, 2004 at 15:44 UTC
    Or is there some other such Perl Magick I can employ...

    just a tiny bit, but you can write $lines[$#lines] as $lines[-1] wich i would prefere.

    for the non working part. make shure &read_the_config is realy called every time you request a page. how/where is it called?

      Hooray!!!

      that's it!

      I was caching the status of whether or not the read_the_config sub had been run yet and this state was leaking through to the other config files!!

      Thankyou very much for the inspiration.

      Cheers

      SM

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://406344]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-19 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls