You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Phil Lobbes <ph...@perkpartners.com> on 2006/05/18 20:38:12 UTC
mp 1.99_12: my() var retains value in subroutine, ideas?
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);
{
no warnings 'uninitialized';
warn("custid($custid) sub_rec($sub_rec) uid_int($uid_int)\n");
}
return $uid_int;
}
Output logged:
custid() sub_rec() uid_int(4641)
Somehow $uid_int has a value (4641 in this case) even though $sub_rec is
not set!
In theory this should be impossible, right? If $sub_rec is not set then
$uid_int could ONLY be undef, right?
I haven't yet setup a test platform that I can run this on repeatedly to
see how reproducible this really is.
I'm considering changing all code to explicitly set $sub_rec and
$uid_int to undef first before doing anything else which should make
this problem go away in theory, but if anyone sees something obvious
here I'm missing please let me know.
Sincerely,
Phil
--
Phillip E. Lobbes
phil@perkpartners.com
Re: mp 1.99_12: my() var retains value in subroutine, ideas?
Posted by Ronald J Kimball <rj...@tamias.net>.
On Thu, May 18, 2006 at 02:49:54PM -0400, Perrin Harkins wrote:
> On Thu, 2006-05-18 at 14:38 -0400, Phil Lobbes wrote:
> > mod_perl-1.99_12-2
>
> Ouch! Known bugs! Upgrade as soon as you can.
>
> > my $sub_rec = $app->get_subscriber_rec($custid) if($custid);
> > my $uid_int = $sub_rec->{'uid_int'} if($sub_rec);
>
> This is the problem. Never do an assignment with a postfix "if"
> statement. Changing these ifs to the normal style will fix the bug.
> It's a long-standing Perl bug, which I avoid by simply not using postfix
> if.
To be precise, don't use a statement modifier on a C<my> declaration. If
the conditional is false, the declared variable will not be reset, but will
instead keep its value from the previous iteration.
Ronald
Re: mp 1.99_12: my() var retains value in subroutine, ideas?
Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2006-05-18 at 14:38 -0400, Phil Lobbes wrote:
> mod_perl-1.99_12-2
Ouch! Known bugs! Upgrade as soon as you can.
> my $sub_rec = $app->get_subscriber_rec($custid) if($custid);
> my $uid_int = $sub_rec->{'uid_int'} if($sub_rec);
This is the problem. Never do an assignment with a postfix "if"
statement. Changing these ifs to the normal style will fix the bug.
It's a long-standing Perl bug, which I avoid by simply not using postfix
if.
By the way, Perl::Critic has a test for this which can detect it in your
code and warn you about it.
- Perrin
Re: mp 1.99_12: my() var retains value in subroutine, ideas?
Posted by Michael Peters <mp...@plusthree.com>.
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
SUMMARY: mp 1.99_12: my() var retains value in subroutine, ideas?
Posted by Phil Lobbes <ph...@perkpartners.com>.
I had googled a number of times, but the modperl list was able to
provide the answers to questions when I apparently couldn't google (nor
scour the archives) to find the answer. I can't believe I haven't
noticed this "unique" behavior before...
The short answer:
* This is unsupported syntax that Perl doesn't warn nor even blink at.
[It apparently should be obvious ;-)] Don't use statement modifiers
with my() declarations as the results are undefined (so much for
efficiency of syntax here and user friendliness, but I'm talking to
the wrong group for this gripe!).
For the record, I finally found this in the docs too:
Perl 5.8.6 perlsyn(1) says:
NOTE: The behaviour of a "my" statement modified with a statement
modifier conditional or loop construct (e.g. "my $x if ...") is
undefined. The value of the "my" variable may be "undef", any
previously assigned value, or possibly anything else. Don't rely on
it. Future versions of perl might do something different from the
version of perl you try it out on. Here be dragons.
Thanks everyone for their help!
Phil
--
Phillip E. Lobbes
phil@perkpartners.com
PS. As I already knew, I should upgrade the software too... if only I
could convince the client to spend a little time on that!
Phil Lobbes <ph...@perkpartners.com> 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);
>
> {
> no warnings 'uninitialized';
> warn("custid($custid) sub_rec($sub_rec) uid_int($uid_int)\n");
> }
> return $uid_int;
> }
>
> Output logged:
> custid() sub_rec() uid_int(4641)
>
> Somehow $uid_int has a value (4641 in this case) even though $sub_rec is
> not set!
>
> In theory this should be impossible, right? If $sub_rec is not set then
> $uid_int could ONLY be undef, right?
>
> I haven't yet setup a test platform that I can run this on repeatedly to
> see how reproducible this really is.
>
> I'm considering changing all code to explicitly set $sub_rec and
> $uid_int to undef first before doing anything else which should make
> this problem go away in theory, but if anyone sees something obvious
> here I'm missing please let me know.
>
> Sincerely,
> Phil
> --
> Phillip E. Lobbes
> phil@perkpartners.com
Re: mp 1.99_12: my() var retains value in subroutine, ideas?
Posted by "Philip M. Gollucci" <pg...@p6m7g8.com>.
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.
Use ? : ternnay operator, or move the if() out of the declaration.
This has been brought up before, but I forget what its called. Google this list
archives.
Also, you using ancient versions of things -- ~2.5 years ago.
--
------------------------------------------------------------------------
Philip M. Gollucci (pgollucci@p6m7g8.com) 323.219.4708
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Software Engineer - TicketMaster - http://ticketmaster.com
1024D/A79997FA F357 0FDD 2301 6296 690F 6A47 D55A 7172 A799 97F
"It takes a minute to have a crush on someone, an hour to like someone,
and a day to love someone, but it takes a lifetime to forget someone..."