Re: pl/perl problem

From: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
To: "FERREIRA William (COFRAMI)" <william(dot)ferreira(at)airbus(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pl/perl problem
Date: 2005-03-22 11:51:25
Message-ID: 69e810494f47610a3df5a8a684a99e68@mail.nih.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rajarshi Mukherjee 2005-03-22 11:57:25 grant problem
Previous Message Charlotte Pollock 2005-03-22 11:44:00 Exporting to XML