Chapter 21. PL/Perl - Perl
Procedural Language

PL/Perl is a loadable procedural language that enables you to
write PostgreSQL functions in
the Perl
programming language.

To install PL/Perl in a particular database, use createlang plperl dbname.

Tip: If a language is installed into template1, all subsequently created databases
will have the language installed automatically.

Note: Users of source packages must specially
enable the build of PL/Perl during the installation process
(refer to the installation instructions for more
information). Users of binary packages might find PL/Perl in
a separate subpackage.

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 non-null
argument to return the non-null argument, rather than a null
value:

There is currently no support for returning a composite-type
result value.

Tip: Because the function body is passed as an
SQL string literal to CREATE
FUNCTION, you have to escape single quotes and
backslashes within your Perl source, typically by doubling
them as shown in the above example. Another possible
approach is to avoid writing single quotes by using Perl's
extended quoting operators (q[],
qq[], qw[]).