Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Apache::Registry - shared variable?

by DaisyLou (Acolyte)
on Jun 20, 2013 at 14:25 UTC ( #1039983=perlquestion: print w/ replies, xml ) Need Help??
DaisyLou has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

I have a script (several actually) that is run through Mod_Perl/Apache Registry and there seems to be variable sharing/leaking going on.

Without further ado here's the essence of the script...

========================= shebang, initialization ... "do" etc. &main() sub main { my $cgi = CGI->new(); my %params = $cgi->Vars(); # Get the MAC address from the URL. my $id = $params{id}; my $sqlStmt; my $hStmt; my $hDb = AnotherModule::connectDb(); $sqlStmt = "SELECT several rows"; $hStmt = MyModule::execSql($hDb, $sqlStmt); my ($value1, $value2, $value3); my $list = ""; while ($value1, $value2, $value3) = $hStmt->fetchrow) { $list .= "$value1,$value2,$value3;"; } print "$list"; OtherModule::log($blah, $blah, "$list"); $hStmt->finish; $hDb->disconnect; exit(0); }

I know that the first comment will be about the SQL stuff not being done the official way -- and you're be right, but there doesn't appear to be an issue with that.

The issue is that $list is sometimes correct, and other times has values appended to it that are from "another thread". Using DBI persistent connections.

It appears as though this may be the issue: http://perl.apache.org/docs/general/perl_reference/perl_reference.html#my____Scoped_Variable_in_Nested_Subroutines

I've read it, but don't understand it. I've tried changing "my" to "our local", no luck.

Any recommendations on how to solve this quickly and easily are really appreciated!

Comment on Apache::Registry - shared variable?
Download Code
Re: Apache::Registry - shared variable?
by Anonymous Monk on Jun 20, 2013 at 14:48 UTC

    It appears as though this may be the issue

    It isn't, not with the code you posted, it has no nested subs, it shares no variables (unless the code you haven't shown does that)

    A problem is with CGI->Vars , you never want to use CGI->Vars, CGI->Vars is for perl4

    Also , return from main, exit after main

    main( @ARGV ); exit( 0 );
    or exit main( @ARGV );
      Thanks for the advice, and you're right -- there are no nested sub. I thought maybe mod-perl mangled it somehow to make one. I thought CGI stuff was made safe using Registry? What should be used in this case, instead, then?

        I thought CGI stuff was made safe using Registry? What should be used in this case, instead, then?

        use CGI->param not CGI->Vars , CGI->Vars has caveats

      ... Made the changes suggested, but the problem persists.

        ... Made the changes suggested, but the problem persists.

        :)

        If you want to solve this you will need to post real code that really shows the real problem

        oh? you say your code is too long? well you trim trim trim until the problem disappears, or you have short self contained compilable test case that shows the bug

        OTOH, what version of CGI.pm do you have installed?

        You could try a single fetch statement
        my $list = ""; my $ar = $hStmt->fetchall_arrayref(); for (@$ar){ $list .= (join ',',@$_).';'; } print $list;
        poj
Re: Apache::Registry - shared variable?
by tqisjim (Beadle) on Jun 20, 2013 at 17:04 UTC

    Apache::Registry as in mod_perl 1.3? I believe Apache::Registry was deprecated a long time ago.

    I wrote a patch for Apache::Registry many years ago that fixes a vexing oversight: When a module is imported, the END {} block is executed right away. This problem usually affects database handles.

    That patch, Apache::ChildExit, is still on CPAN. I don't have much time to consider your question. But I generally post this answer when I see problems with Apache::Registry.

      Also, one of Apache::Registry's shortcomings was a confusing and somewhat incomplete approach to sharing variables among sessions. Since the code is cached, the variables aren't automatically re-initialized each time the script is run. This might solve your problem:

      my $foo ; ## usually ok in perl my $foo = undef ; ## an Apache::Registry requirement?

      If you're not committed to Apache::Registry. Consider an alternative. Also, the mod_perl mailing list: http://perl.apache.org

        Oh, and to answer your other question...
        $VAR1 = { 'MOD_PERL_API_VERSION' => 2, 'MOD_PERL' => 'mod_perl/2.0.4' };

        In Apache config, though:

        PerlResponseHandler ModPerl::Registry

        Our code is "not mod_perl safe", so we were told to use Registry to work around that.

      Thanks! Is there any risk of messing up existing scripts if I install this? Test cycles around here take ages.

      That patch, Apache::ChildExit, is still on CPAN. I don't have much time to consider your question. But I generally post this answer when I see problems with Apache::Registry.

      How would this cause data duplication or stale data or data from previous session/request leaking through to a new one? Cause it wouldn't you know, its pretty much guaranteed to have nothing to do with it

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039983]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-12-26 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (171 votes), past polls