Could someone please assist to rectify the errors in this piece of code.
(this is just a simplified version of my code, but it identifies the issues).
DROP FUNCTION perl_func(VARIADIC params character varying[]);
CREATE OR REPLACE FUNCTION perl_func(VARIADIC params character varying[])
RETURNS character varying AS
$BODY$
$val = spi_query("array_to_string($1,'###');");
$s = `echo $val`;
return $s;
$BODY$
LANGUAGE plperlu VOLATILE
COST 100;
SELECT * from perl_func('a','d');
This returns a syntax error:
ERROR: syntax error at or near "," at line 2.
CONTEXT: PL/Perl function “perl_func”
The main aim: Is to formulate the input params as a string, and use it to call some command-line program, which returns a String. Then output this string as the return of this function.
Original attempt:
The variable
$1means something different in Perl than it does in plpgsql functions. TryUnfortunately, this doesn’t work (as noted below) because it doesn’t split the strings properly. Unfortunately, plperl considers the arguments to be a single string:
And when we select it:
And to find out for certain that it’s a single string, we can turn to our old friend
Data::Dumper:And this returns:
So, the output is considered an actual string with curly braces on the end and the entries separated by commas. Okay, well…this is annoying, but at least we can deal with it:
And this gets us what we want:
Note that, for reasons I don’t understand, I had to resort to the use of
substrinstead of a simple regex replace ($array_string=~s/{}//g;), as the plperl function kept returning the curly brackets when I tried the regex replacement.I hadn’t dealt with plperl a lot before answering your questions about it, and the main thing that I’ve learned is that it’s a major pain…you might want to consider manipulating the database from Perl using the Perl
DBI.