Re: syntax error in plperl wrapper for Perl regex replace.
Cédric Godin <[email protected]>
| Newsgroups | gmane.comp.db.postgresql.novice |
|---|---|
| Message-ID | <[email protected]> |
Le 25/08/18 à 01:55, io.sys a écrit :
Not being a perl expert either I think
the problem lies in the regex line.
You should eval it to have the expected
result (and you can use your 4th parameter again then).
eval "s/$_[1]/$_[2]/$_[3]";
Regards, Cédric
Tom,
You are right, it is a Perl's fault, not Postgre's.
You are right also with assumption, that the "s" command of Perl does not accept variable
in every of it's part. I have narrowed down the problem (btw, I could not find anything
about it is Perl docs) to the options part, ie. after last slash. Perl only accepts literals
there, not a variable of any kind. So these options must be hardcoded, not a dynamic value
constructed at runtime.
This compiles:
s/$_[1]/$_[2]/ig;
But even then, the command does not work as expected, as it does not interpret the
backreferences in a replacement string passed to it. It uses it literally. For example,
one would expect that the following call will return 'Praha':
select perl_regexp_replace ('PRAHA', '^(.)(.*)$', '$1\L$2');
But it returns:
$1\L$2
Well, Perl is praised for it's capabilities at the field of text processing, but this
experience disappointed me a little bit. Never mind, I will solve the things different way.
Thank you all for your inputs.
Regards, Roman Golis.