Re: perlsub - Mailing list pgsql-novice

From Nabil Sayegh
Subject Re: perlsub
Date
Msg-id 3F7F0B5C.3040604@e-trolley.de
Whole thread Raw
In response to Re: perlsub  (Oliver Elphick <olly@lfix.co.uk>)
List pgsql-novice
Oliver Elphick wrote:

> 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.

Ok, here's what I got from #perl
I need to 'eval' the string to make it an executable expression.

-------------------------8<-------------------------------
#!/usr/bin/perl



print perlsub('abc','b','123')."\n";
print perlsub('a=b','([^=]+)=(.+)','key:$1;val:$2;')."\n";



sub perlsub
{
         my ($data, $pat, $repl) = @_;
         eval "\$data =~ s/$pat/$repl/gi";
         return $data;
}
-------------------------8<-------------------------------

As a Pl/Perl Function this still doesnt work.
Seems like it's forbidden, as it would allow to execute
arbitrary code :(

-------------------------8<-------------------------------
CREATE FUNCTION perlsub(text, text, text) RETURNS text AS '
     my ($data, $pat, $repl) = @_;
     eval "\$data =~ s/$pat/$repl/gi";
     return $data
' LANGUAGE 'plperl';
-------------------------8<--------------------------------

plasma=# SELECT perlsub('a=b','([^=]+)=(.+)','key:$1;val:$2');
ERROR:  creation of function failed: 'eval "string"' trapped by
operation mask at (eval 2) line 3.

Seems like I have to hardcode the regular expression in the function :(

TFYH
--
  e-Trolley Sayegh & John, Nabil Sayegh
  Tel.: 0700 etrolley /// 0700 38765539
  Fax.: +49 69 8299381-8
  PGP : http://www.e-trolley.de


pgsql-novice by date:

Previous
From: Tim Wright
Date:
Subject: Re: Getting "connectby" installed
Next
From: sibusiso xolo
Date:
Subject: problems retrieving images in 7.3.4