Re: perlsub

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Nabil Sayegh <postgresql(at)e-trolley(dot)de>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: perlsub
Date: 2003-10-04 06:20:10
Message-ID: 1065248410.4459.95.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Sat, 2003-10-04 at 00:29, Nabil Sayegh wrote:
> Hello again,
>
> now that perlsub works i need to know how backreferences work with
> plperl. $1 - $9 doesn't work. Any idea ?
>
> perlsub:
> -----------------------------------------------
> CREATE FUNCTION perlsub(text, text, text) RETURNS text AS '
> my ($data, $pat, $repl) = @_;
> $data =~ s/$pat/$repl/;
> return $data
> ' LANGUAGE 'plperl';
> -----------------------------------------------
>
> SELECT perlsub('a=b','([^=]+)=(.+)','key:$1;val:$2');
>
> Any idea ? Just a matter of quoting ?

It's a problem with Perl itself rather than with PL/Perl. I tried that
in a Perl script and it produces the same result. I couldn't find any
way of including $ in the replacement string as a metacharacter.

You need to consult a Perl guru.

When you do get an example that works in a script, remember to double
any single quotes and backslashes when you create the function.

> Also this example doesn't work with * instead of + :(

I don't see any difference:

junk=# SELECT perlsub('a=b','([^=]*)=(.*)','key:$1;val:$2');
perlsub
---------------
key:$1;val:$2
(1 row)

junk=# SELECT perlsub('a=b','([^=]+)=(.+)','key:$1;val:$2');
perlsub
---------------
key:$1;val:$2
(1 row)

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"For the word of God is quick, and powerful, and
sharper than any twoedged sword, piercing even to the
dividing asunder of soul and spirit, and of the joints
and marrow, and is a discerner of the thoughts and
intents of the heart." Hebrews 4:12

In response to

  • perlsub at 2003-10-03 23:29:00 from Nabil Sayegh

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Nabil Sayegh 2003-10-04 18:03:08 Re: perlsub
Previous Message Nabil Sayegh 2003-10-03 23:29:00 perlsub