in reply to time in mod_perl

I bet you're using an Apache::Registry script with a global my variable.

my $t = time; sub print_t { print("$t\n"); } print_t(); print("$t\n");

This is not a mod_perl problem, but an Apache::Registry problem. Apache::Registry puts your script in a function it executes every time the page is requested.

my $script = sub { my $t = time; sub print_t { print("$t\n"); } print_t(); print("$t\n"); }; $script->(); # A page request sleep(2); $script->(); # A page request

the problem is that print_t closed over $t, so it always refers to $t original value, even though $t changes.

1175021938 1175021938 1175021938 <- print_t still references the original $t 1175021940

The fix is to use package variables instead of lexical variables for globals in Apache::Registry scripts.

my $script = sub { our $t = time; sub print_t { print("$t\n"); } print_t(); print("$t\n"); }; $script->(); # A page request sleep(2); $script->(); # A page request
1175022050 1175022050 1175022052 1175022052

Replies are listed 'Best First'.
Re^2: time in mod_perl
by Bruce32903 (Scribe) on Mar 27, 2007 at 20:08 UTC
    I did a little Googling on "our" vs. "my". I found some material but I am not quite catching the drift. Is there an easy explination of "our" vs. "my" in the context of mod_perl programs running on Apache2?

    Thank you,
    Bruce

      Sometimes it's better to use the pm search rather than the google search (although, I understand that's changing).

      If you search on pm for doc://my and doc://our you'll get redirected to nicely colored pages at perldoc.perl.org. The document on our explains the difference quite nicely (if esoterically). I actually use this search engine in firefox as much as I don't.

      -Paul

Re^2: time in mod_perl
by Bruce32903 (Scribe) on Mar 27, 2007 at 20:18 UTC
    Also, I am getting "will not stay shared" messages in my Apache error log for both the time variable that I have "fixed" and the other variables. This error message seems to be the same scope issue we have been dealing with in this posting. This scope issue (my lack of mastering it) seems to be filling my error log up with lots of useless messages. This makes it harder to notice a useful error message. What can I do to keep my error log clean?

    Thank you,
    Bruce