From 87bb2ade2ce646083f39d5ab3e3307490211ad04 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 17 Feb 2011 22:11:50 -0300 Subject: Convert Postgres arrays to Perl arrays on PL/perl input arguments More generally, arrays are turned in Perl array references, and row and composite types are turned into Perl hash references. This is done recursively, in a way that's natural to every Perl programmer. To avoid a backwards compatibility hit, the string representation of each structure is also available if the function requests it. Authors: Alexey Klyukin and Alex Hunsaker. Some code cleanups by me. --- doc/src/sgml/plperl.sgml | 70 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'doc/src') diff --git a/doc/src/sgml/plperl.sgml b/doc/src/sgml/plperl.sgml index 4150998808c..a481accd868 100644 --- a/doc/src/sgml/plperl.sgml +++ b/doc/src/sgml/plperl.sgml @@ -198,6 +198,42 @@ select returns_array(); + + Perl passes PostgreSQL arrays as a blessed + PostgreSQL::InServer::ARRAY object. This object may be treated as an array + reference or a string, allowing for backwards compatibility with Perl + code written for PostgreSQL versions below 9.1 to + run. For example: + + +CREATE OR REPLACE FUNCTION concat_array_elements(text[]) RETURNS TEXT AS $$ + my $arg = shift; + my $result = ""; + return undef if (!defined $arg); + + # as an array reference + for (@$arg) { + $result .= $_; + } + + # also works as a string + $result .= $arg; + + return $result; +$$ LANGUAGE plperl; + +SELECT concat_array_elements(ARRAY['PL','/','Perl']); + + + + + Multi-dimensional arrays are represented as references to + lower-dimensional arrays of references in a way common to every Perl + programmer. + + + + Composite-type arguments are passed to the function as references to hashes. The keys of the hash are the attribute names of the @@ -740,6 +776,22 @@ SELECT release_hosts_query(); + + + encode_typed_literal + in PL/Perl + + + encode_typed_literal(value, typename) + + + Converts a Perl variable to the value of the datatype passed as a + second argument and returns a string representation of this value. + Correctly handles nested arrays and values of composite types. + + + + encode_array_constructor @@ -775,8 +827,24 @@ SELECT release_hosts_query(); + + + is_array_ref + in PL/Perl + + + is_array_ref(argument) + + + Returns a true value if the given argument may be treated as an + array reference, that is, if ref of the argument is ARRAY or + PostgreSQL::InServer::ARRAY. Returns false otherwise. + + + + - + -- cgit v1.2.3