in reply to Private Variables and FastCGI / ModPerl

- Its really not an option --. There must be some way to encapsulate the main functions of the program to automagically my (private) / localize anything used within the encapsulation. Really love to hear your thoughts on this one. _Kevin

They call that a rewrite, anything else is just prayer

  • Comment on Re: Private Variables and FastCGI / ModPerl

Replies are listed 'Best First'.
Re^2: Private Variables and FastCGI / ModPerl
by expresspotato (Beadle) on Feb 11, 2010 at 04:59 UTC
    Using local $var seems to be the fix for such problems. Although it may/may not work with mod_perl, it seems to work fine within the FCGI loop.
    use FCGI; while (FCGI::accept >= 0) { local $cnt; local $last_elapsed; ############### &http(0); &head(); &t(''); print "OK"; &t(''); &t(''); my $cc; while ($cc < 1000){ &test; $cc++; } } sub test(){ print "CNT: $cnt"; $cnt++; }
Re^2: Private Variables and FastCGI / ModPerl
by expresspotato (Beadle) on Feb 11, 2010 at 03:55 UTC
    Thank you for your prompt response. Alright perhaps you can help me with this then... How can I keep variables results within a subroutine as if they were global? The following sub is used to see how long portions of code take to execute. But how can I keep $last_elapsed within the sub?
    use Time::HiRes qw(tv_interval); &t('Reference Time'); sleep(2); &t('Did something'); sub t(){ my $t0 = [gettimeofday]; my $elapsed = tv_interval ( $t0 ); my $elapsed_o = $elapsed; $elapsed = $elapsed - $last_elapsed; if ($last_elapsed){printf ("(%s)%f<br>\n",$_[0],$elapsed);} $last_elapsed = $elapsed_o; }
    Hope to hear from you, _Kevin
      #!/usr/bin/perl -- use strict; use warnings; use Time::Elapse; Main(@ARGV); exit(0); sub Main { Time::Elapse->lapse( my $now = 'Reference Time' ); print "$now\n"; sleep 2; print "$now\n"; sleep 1; $now = 'Did something'; print "$now\n"; } ## end sub Main __END__ 16:00:00.000064 [Reference Time] 16:00:02.000000 [Reference Time] 16:00:00.000069 [Did something]
        Although that does fix the problems with that specific subroutine, it doesn't tell my how to keep a value live within a subroutine without wiping it each time the subroutine is called... local does fix the problem, using local $elapsed_time, within the FCGI loop and outside the subroutine.