If you pass array types as arguments to the PL/Perl function, they are passed as the blessed ostgreSQL::InServer::ARRAY
objects. In Perl, bless
associates an object with a class. This object can be treated as an array reference or as a string. If you have to return an array type, you must return an array by reference. Let's take a look at the following example:
CREATE OR REPLACE FUNCTION reverse(int[]) RETURNS int[]
AS $$
my $arg = shift; # get the reference of the argument
my @rev = reverse @{$arg}; # reverse the array
return \@rev # return the array reference
$$ LANGUAGE plperl;
testdb=# select reverse(ARRAY[1,2,3,4]);
reverse
-----------
{4,3,2,1}
(1 row)
The preceding function reverses an integer array passed to the function using the reverse
function of Perl. You can take a look at the comments in the code to understand it. First, we get the reference of the passed argument. We then reverse the array using a reference notation, @{$arg...