from a PL/Perl
function, return an undefined value. This can be done whether the
function is strict or not.
</para>
<para>
Anything in a function argument that is not a reference is
a string, which is in the standard <productname>PostgreSQL</productname>
external text representation for the relevant data type. In the case of
ordinary numeric or text types, Perl will just do the right thing and
the programmer will normally not have to worry about it. However, in
other cases the argument will need to be converted into a form that is
more usable in Perl. For example, the <function>decode_bytea</function>
function can be used to convert an argument of
type <type>bytea</type> into unescaped binary.
</para>
<para>
Similarly, values passed back to <productname>PostgreSQL</productname>
must be in the external text representation format. For example, the
<function>encode_bytea</function> function can be used to
escape binary data for a return value of type <type>bytea</type>.
</para>
<para>
One case that is particularly important is boolean values. As just
stated, the default behavior for <type>bool</type> values is that they
are passed to Perl as text, thus either <literal>'t'</literal>
or <literal>'f'</literal>. This is problematic, since Perl will not
treat <literal>'f'</literal> as false! It is possible to improve matters
by using a <quote>transform</quote> (see
<xref linkend="sql-createtransform"/>). Suitable transforms are provided
by the <filename>bool_plperl</filename> extension. To use it, install
the extension:
<programlisting>
CREATE EXTENSION bool_plperl; -- or bool_plperlu for PL/PerlU
</programlisting>
Then use the <literal>TRANSFORM</literal> function attribute for a
PL/Perl function that takes or returns <type>bool</type>, for example:
<programlisting>
CREATE FUNCTION perl_and(bool, bool) RETURNS bool
TRANSFORM FOR TYPE bool
AS $$
my ($a, $b) = @_;
return $a && $b;
$$ LANGUAGE plperl;
</programlisting>
When this transform is applied, <type>bool</type> arguments will be seen
by Perl as being <literal>1</literal> or empty, thus properly true or
false. If the function result is type <type>bool</type>, it will be true
or false according to whether Perl would evaluate the returned value as
true.
Similar transformations are also performed for boolean query arguments
and results of SPI queries performed inside the function
(<xref linkend="plperl-database"/>).
</para>
<para>
Perl can return <productname>PostgreSQL</productname> arrays as
references to Perl arrays. Here is an example:
<programlisting>
CREATE OR REPLACE function returns_array()
RETURNS text[][] AS $$
return [['a"b','c,d'],['e\\f','g']];
$$ LANGUAGE plperl;
select returns_array();
</programlisting>
</para>
<para>
Perl passes <productname>PostgreSQL</productname> arrays as a blessed
<type>PostgreSQL::InServer::ARRAY</type> object. This object may be treated as an array
reference or a string, allowing for backward compatibility with Perl
code written for <productname>PostgreSQL</productname> versions below 9.1 to
run. For example:
<programlisting>
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']);
</programlisting>
<note>
<para>
Multidimensional arrays are represented as references to
lower-dimensional arrays of references in a way common to every Perl
programmer.
</para>
</note>
</para>
<para>
Composite-type arguments are passed to the function as references
to hashes. The keys of the hash are the attribute names of the
composite