The body of the function is ordinary Perl code. In fact, the
PL/Perl glue code wraps it inside a Perl subroutine. A PL/Perl
function must always return a scalar value. You can return more
complex structures (arrays, records, and sets) by returning a
reference, as discussed below. Never return a list.

Note: The use of named nested subroutines is
dangerous in Perl, especially if they refer to lexical
variables in the enclosing scope. Because a PL/Perl function
is wrapped in a subroutine, any named subroutine you create
will be nested. In general, it is far safer to create
anonymous subroutines which you call via a coderef. See the
perldiag man page for more
details.

The syntax of the CREATE FUNCTION
command requires the function body to be written as a string
constant. It is usually most convenient to use dollar quoting
(see Section
4.1.2.2) for the string constant. If you choose to use escape
string syntax E'', you must double any
single quote marks (') and backslashes
(\) used in the body of the function
(see Section
4.1.2.1).

Arguments and results are handled as in any other Perl
subroutine: arguments are passed in @_,
and a result value is returned with return or as the last expression evaluated in the
function.

For example, a function returning the greater of two integer
values could be defined as:

If an SQL null value is
passed to a function, the argument value will appear as
"undefined" in Perl. The above
function definition will not behave very nicely with null inputs
(in fact, it will act as though they are zeroes). We could add
STRICT to the function definition to
make PostgreSQL do something
more reasonable: if a null value is passed, the function will not
be called at all, but will just return a null result
automatically. Alternatively, we could check for undefined inputs
in the function body. For example, suppose that we wanted
perl_max with one null and one
nonnull argument to return the nonnull argument, rather than a
null value:

As shown above, to return an SQL null value from a PL/Perl
function, return an undefined value. This can be done whether the
function is strict or not.

Anything in a function argument that is not a reference is a
string, which is in the standard PostgreSQL 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, here is how to convert an argument
of type bytea into unescaped binary
data:

Any columns in the declared result data type that are not
present in the hash will be returned as null values.

PL/Perl functions can also return sets of either scalar or
composite types. Usually you'll want to return rows one at a
time, both to speed up startup time and to keep from queueing up
the entire result set in memory. You can do this with
return_next as illustrated below.
Note that after the last return_next, you must put either return or (better) return
undef.

For small result sets, you can return a reference to an array
that contains either scalars, references to arrays, or references
to hashes for simple types, array types, and composite types,
respectively. Here are some simple examples of returning the
entire result set as an array reference:

If you wish to use the strict pragma
with your code, the easiest way to do so is to SETplperl.use_strict to
true. This parameter affects subsequent compilations of
PL/Perl functions, but not
functions already compiled in the current session. To set the
parameter before PL/Perl has
been loaded, it is necessary to have added "plperl" to the custom_variable_classes
list in postgresql.conf.