Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
When working with Apache::Registry it is very easy to create accidental closures.
This is due to the way Apache::Registry makes a fake package for your script, as I'll demonstrate in this tutorial.

The only indication that something is amiss (other than the unpredictable behaviour) will be the following line in your error log: Variable "$foo" will not stay shared at ...

The following code demonstrates the problem with accidental closures when using Apache::Registry.

use strict; use warnings; my $foo = 5; print "Content-type: text/plain\n"; print "Content-disposition: inline; filename=foo.txt\n\n"; printf "Package: %s\n", __PACKAGE__; printf "[%s] Before: %s\n", $$, $foo; badness(5); printf "[%s] After: %s\n", $$, $foo; sub badness { my $val = shift; printf "[%s] badness: %s\n", $$, $foo; $foo += $val; }

Apache::Registry will take the above code and create a new package for it based on the ServerName and the name of the script, and then wrap the code in a sub handler {} block.

If your script is running on "foo.com" and is named "test.pl", then this is what the above code will look like after Apache::Registry is done:

package Apache::ROOTfoo_2ecom::test_2epl; use Apache qw(exit); sub handler { #line 1 /www/foo.com/test.pl use strict; use warnings; my $foo = 5; print "Content-type: text/plain\n"; print "Content-disposition: inline; filename=foo.txt\n\n"; printf "Package: %s\n", __PACKAGE__; printf "[%s] Before: %s\n", $$, $foo; badness(5); printf "[%s] After: %s\n", $$, $foo; sub badness { my $val = shift; printf "[%s] badness: %s\n", $$, $foo; $foo += $val; } }
First run:
Package: Apache::ROOTfoo_2ecom::test_2epl [13520] Before: 5 [13520] badness: 5 [13520] After: 10
Second:
Package: Apache::ROOTfoo_2ecom::test_2epl [19331] Before: 5 [19331] badness: 5 [19331] After: 10
Third:
Package: Apache::ROOTfoo_2ecom::test_2epl [19331] Before: 5 [19331] badness: 10 [19331] After: 5
Fourth:
Package: Apache::ROOTfoo_2ecom::test_2epl [19331] Before: 5 [19331] badness: 15 [19331] After: 5
Notice how the number within the badness sub is increasing for each process, but the $foo that is seen by the instance script is never modified after 'badness' after the first execution for that process.

This is because the badness function is actually an inner function now, and it keeps a reference to the instance of $foo that was created for the first run.

Edit - example of how to avoid this issue added, per rhesa's suggestion

Thankfully it is easy to avoid these problems once you know why they occur.
Tips:

  • Keep your toplevel script minimal
  • Subroutines should only use the variables that were passed
  • Encapsulate behaviour in supporting objects
Example of a working alternative:
use strict; use warnings; my $foo = 5; print "Content-type: text/plain\n"; print "Content-disposition: inline; filename=foo.txt\n\n"; printf "Package: %s\n", __PACKAGE__; printf "[%s] Before: %s\n", $$, $foo; badness(\$foo, 5); badness(\$foo, 5); printf "[%s] After: %s\n", $$, $foo; sub badness { my ($foo,$val) = @_; printf "[%s] badness: %s\n", $$, $$foo; $$foo += $val; }

In reply to mod_perl / Apache::Registry accidental closures by imp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
    [marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
    [erix]: one would hope evolutionists haven't any prayers anyway
    [marto]: obviously someone could be religious, but not creationist
    [erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
    [erix]: yes. Deplorable marto, deplorable.
    [marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
    [marto]: meh, I've been called worse :P

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (12)
    As of 2017-07-28 15:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I came, I saw, I ...
























      Results (431 votes). Check out past polls.