Re: pl/perl problem - Mailing list pgsql-general

From Sean Davis
Subject Re: pl/perl problem
Date
Msg-id 69e810494f47610a3df5a8a684a99e68@mail.nih.gov
Whole thread Raw
In response to Re: pl/perl problem  ("FERREIRA William (COFRAMI)" <william.ferreira@airbus.com>)
List 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


pgsql-general by date:

Previous
From: Charlotte Pollock
Date:
Subject: Exporting to XML
Next
From: Rajarshi Mukherjee
Date:
Subject: grant problem