Phil Lobbes wrote:
> Software versions:
>   mod_perl-1.99_12-2
>   perl-5.8.3-17.5.legacy
>   httpd-2.0.51-1.10.legacy
>   Fedora Core 1 (kernel 2.4.22-1.2199.8.legacy.nptl)
> 
> I have a custom application running under Apache::Registry w/mod_perl
> using strict and warnings everywhere.
> 
> I have run into a case where sometimes a subroutine variable declared
> with my() inside an OO method retains its value somehow(!?!).  I think I
> must be going crazy.  The code looks like this:
> 
>  sub validate {
>      my $app     = shift;
>      my $custid  = $app->get_session_var('id') || "";
>      my $sub_rec = $app->get_subscriber_rec($custid) if($custid);
>      my $uid_int = $sub_rec->{'uid_int'}             if($sub_rec);

Don't do that. It's doing exactly what you're asking it to since 'my' has both
compile time and run time effects. The $sub_rec and $uid_int vars are created
local to that sub at compile time. At run time their values are set depending on
your conditional. Since $sub_rec is false the second time the value is never 
reset.

It's actually a deprecated poor-man's static variable technique. Perl 5.10
should have real static variables. I make it a rule to never do conditional
declaration to avoid just this problem.

  my $cust_id = $app->get_session_var('id') || "";
  my ($sub_rec, $uid_int);
  $sub_rec = $app->get_subscriber_rec($custid) if($custid);
  $uid_int = $sub_rec->{'uid_int'}             if($sub_rec);

-- 
Michael Peters
Developer
Plus Three, LP

Reply via email to