Re: Plperl and my() lexical variables bug?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Philippe Lang <philippe(dot)lang(at)attiksystem(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Plperl and my() lexical variables bug?
Date: 2006-06-23 13:49:19
Message-ID: 20060623134919.GA68363@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jun 23, 2006 at 11:33:42AM +0200, Philippe Lang wrote:
> Am I missing something maybe? It sounds like a bug with lexical variables to me...

I think what's happening is that sub init is created once with $val
referencing the lexically-scoped $val from sub foo's first invocation.
When you call foo again, foo creates a new lexically-scoped $val
but init's $val still refers to the object from foo's first call.
You can see this if you display \$val:

CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$
my $val;

sub init {
$val = $_[0];
elog(NOTICE, "1: $_[0] " . \$val);
}

init(12);
elog(NOTICE, "2: $val " . \$val);
$$ LANGUAGE plperl;

SELECT foo();
NOTICE: 1: 12 SCALAR(0x8447220)
NOTICE: 2: 12 SCALAR(0x8447220)
foo
-----

(1 row)

SELECT foo();
NOTICE: 1: 12 SCALAR(0x8447220)
NOTICE: 2: SCALAR(0x83f5c4c)
foo
-----

(1 row)

This behavior isn't specific to PL/Perl. A standalone Perl program
exhibits the same behavior, so you might find a better explanation
in a Perl-specific forum like the comp.lang.perl.misc newsgroup.

--
Michael Fuhr

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2006-06-23 14:00:02 Re: JUST NOT ADDING UP
Previous Message Rhys Stewart 2006-06-23 13:43:53 JUST NOT ADDING UP