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

From FERREIRA William (COFRAMI)
Subject Re: pl/perl problem
Date
Msg-id 1904E3EB39448246A7ECB76DF34A70B00143B4A8@TOCOMEXC03
Whole thread Raw
In response to pl/perl problem  ("FERREIRA William (COFRAMI)" <william.ferreira@airbus.com>)
List pgsql-general

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

-----Message d'origine-----
De : Sean Davis [mailto:sdavis2@mail.nih.gov]
Envoyé : mardi 22 mars 2005 12:51
À : FERREIRA William (COFRAMI)
Cc : pgsql-general@postgresql.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.

pgsql-general by date:

Previous
From: "Magnus Hagander"
Date:
Subject: Re: Simple query takes a long time on win2K
Next
From: "FERREIRA William (COFRAMI)"
Date:
Subject: Re: pl/perl problem