Re: pl/perl problem

From: "FERREIRA William (COFRAMI)" <william(dot)ferreira(at)airbus(dot)com>
To: 'Richard Huxton' <dev(at)archonet(dot)com>
Cc: 'Sean Davis' <sdavis2(at)mail(dot)nih(dot)gov>, pgsql-general(at)postgresql(dot)org
Subject: Re: pl/perl problem
Date: 2005-03-23 14:39:30
Message-ID: 1904E3EB39448246A7ECB76DF34A70B00143B4A7@TOCOMEXC03
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

yes, it works
exactly what i needed, thanks a lot

-----Message d'origine-----
De : Richard Huxton [mailto:dev(at)archonet(dot)com]
Envoyé : mardi 22 mars 2005 12:41
À : FERREIRA William (COFRAMI)
Cc : 'Sean Davis'; pgsql-general(at)postgresql(dot)org
Objet : Re: [GENERAL] pl/perl problem

FERREIRA William (COFRAMI) wrote:
> my function is very long but i found an example with the same comportment
:
> CREATE OR REPLACE FUNCTION adoc.totoTest()
> RETURNS int4 AS
> $BODY$
> my $var = '->>>';
> &concat($var);
>
> sub concat {
> $var .= 'tagada';
> }
> elog NOTICE, $var;
> return 4;
>
> $BODY$
> LANGUAGE 'plperl' VOLATILE;
>
> first execution : ->>>tagada
> second execution : ->>>

In the example above $var in sub concat is NOT an argument provided to
the function. What you've done there is create a named closure (if I'm
getting my terms right) in which the inner $var is allocated on first
call but not afterwards. The second time you run totoTest() the outer
$var (my $var) is a new variable, whereas the inner one still refers to
the original.

If you actually want to return a concatenated string you'd want
something like:

sub concat {
my $var = shift;
return $var . 'tagada';
}

If you want to affect an outer variable you'll want something like

sub concat {
my $var_ref = shift;
$$var_ref .= 'tagada';
}

Does that help?
--
Richard Huxton
Archonet Ltd

This mail has originated outside your organization,
either from an external partner or the Global Internet.
Keep this in mind if you answer this message.

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-03-23 14:40:33 Re: Delay INSERT
Previous Message Stephan Szabo 2005-03-23 14:28:47 Re: inherited table and rules