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

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Reply via email to