Re: pl/perl problem

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: pl/perl problem
Дата
Msg-id 42400439.3060302@archonet.com
обсуждение исходный текст
Ответ на Re: pl/perl problem  ("FERREIRA William (COFRAMI)" <william.ferreira@airbus.com>)
Список pgsql-general
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

В списке pgsql-general по дате отправления:

Предыдущее
От: Shaun Clements
Дата:
Сообщение: PHP SQL
Следующее
От: Charlotte Pollock
Дата:
Сообщение: Exporting to XML