Re: [CDBI] Updating data automatically after a search (trigger)

[prev] [thread] [next] [Date index for 2006/02/01]

From: Perrin Harkins
Subject: Re: [CDBI] Updating data automatically after a search (trigger)
Date: 19:35 on 01 Feb 2006
--=-TqeCGqQMAZZZpm6FgunD
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2006-02-01 at 11:03 -0800, Bill Moseley wrote:
> Can you point me to early docs on this?

It's a little hard to Google for, but it has come up on many mailing
lists and on Perlmonks.  I found an old mail I wrote about this, so here
is more explanation:

sub sticky {
   my $arg = shift;
   my $object = Some::Package->new() if $arg;
   $object ||= Other::Package->new();
}

The first time you run it with $arg = 0, you will get the expected
result.  The second time through with $arg = 0, you will find the
Other::Package object from last time in $object and it will not get a
fresh Other::Package object assigned to it.  So, if you never put
anything in $object when the conditional is false, you won't actually
trigger the bug.  Probably safest to just avoid using the "... if $arg"
syntax at all, but don't panic if you see some in your old code.

Attached is a test program that demonstrates the issue.  The hash ref
should be different every time the sub foo() is called, but it isn't.

> In perldelta for perl v5.9.1:
> 
>     A new deprecation warning, Deprecated use of my() in false
>     conditional, has been added, to warn against the use of the dubious
>     and deprecated construct

I am so happy to see this!  Fantastic!

Anyway, this one is so slippery that I think the only way to stay sane
is to NEVER declare a lexical with a postfix if statement.  Picking out
the ones where it breaks from the ones where it doesn't takes more spare
brainpower than I have.

- Perrin

--=-TqeCGqQMAZZZpm6FgunD
Content-Disposition: attachment; filename=test.pl
Content-Type: application/x-perl; name=test.pl
Content-Transfer-Encoding: 7bit

use strict;
use warnings;

foo(1);
foo(0);
foo(0);

sub foo {
    my $arg = shift;
    my $bar = {} if $arg;
    $bar ||= {};
    print "bar: $bar\n";
}

--=-TqeCGqQMAZZZpm6FgunD
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

--=-TqeCGqQMAZZZpm6FgunD--


Re: [CDBI] Updating data automatically after a search (trigger)
Perrin Harkins 19:35 on 01 Feb 2006

Generated at 17:59 on 03 Feb 2006 by mariachi v0.52