Re: pl/perl problem

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

thanks a lot
with your example and the example of Richard it works fine

-----Message d'origine-----
De : Sean Davis [mailto:sdavis2(at)mail(dot)nih(dot)gov]
Envoyé : mardi 22 mars 2005 12:51
À : FERREIRA William (COFRAMI)
Cc : pgsql-general(at)postgresql(dot)org
Objet : Re: [GENERAL] pl/perl problem

On Mar 22, 2005, at 3:13 AM, 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 : ->>>

Here is a slightly modified version of your code that does what you
want, I think. A couple of things:

1) If you want to pass arguments to a subroutine, what you do above
won't work.
2) You have to be careful in perl when you modify variables that you
know the scope of the variables (where they will be seen versus not)
that you are modifying.
3) If you want a subroutine to modify the value of a variable passed
to it, you need to pass a REFERENCE to that variable, not the value of
the variable.

CREATE OR REPLACE FUNCTION adoc.totoTest2() RETURNS int4 AS
$BODY$
use strict; #see below for explanation
my $var = '->>>';
concat(\$var); #use a reference to the variable
elog NOTICE, $var;
return 4;

sub concat {
my $ref=shift; #get a REFERENCE to the variable
${$ref} .= 'tagada'; #this dereferences the variable and modifies it
}

$BODY$
LANGUAGE 'plperl' VOLATILE;

>  
> (for my second problem, i not able to reproduce it....i deleted the
> source code)
> but what means 'use strict' ?
>
>

See this article....

http://perl.about.com/od/perlforbeginners/l/aa081701a.htm

Sean

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 Christopher Browne 2005-03-23 14:42:18 Re: Delay INSERT
Previous Message Michael Fuhr 2005-03-23 14:40:33 Re: Delay INSERT