Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: Plperl and my() lexical variables bug?



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



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group