Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
43.1. PL/Perl Functions and Arguments
Prev UpChapter 43. PL/Perl — Perl Procedural LanguageHome Next

43.1. PL/Perl Functions and Arguments#

To create a function in the PL/Perl language, use the standardCREATE FUNCTION syntax:

CREATE FUNCTIONfuncname (argument-types)RETURNSreturn-type-- function attributes can go hereAS $$    # PL/Perl function body goes here$$ LANGUAGE plperl;

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 is called in a scalar context, so it can't return a list. You can return non-scalar values (arrays, records, and sets) by returning a reference, as discussed below.

In a PL/Perl procedure, any return value from the Perl code is ignored.

PL/Perl also supports anonymous code blocks called with theDO statement:

DO $$    # PL/Perl code$$ LANGUAGE plperl;

An anonymous code block receives no arguments, and whatever value it might return is discarded. Otherwise it behaves just like a function.

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 place inside one will be nested. In general, it is far safer to create anonymous subroutines which you call via a coderef. For more information, see the entries forVariable "%s" will not stay shared andVariable "%s" is not available in theperldiag man page, or search the Internet forperl nested named subroutine.

The syntax of theCREATE FUNCTION command requires the function body to be written as a string constant. It is usually most convenient to use dollar quoting (seeSection 4.1.2.4) for the string constant. If you choose to use escape string syntaxE'', you must double any single quote marks (') and backslashes (\) used in the body of the function (seeSection 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 withreturn or as the last expression evaluated in the function.

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

CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$    if ($_[0] > $_[1]) { return $_[0]; }    return $_[1];$$ LANGUAGE plperl;

Note

Arguments will be converted from the database's encoding to UTF-8 for use inside PL/Perl, and then converted from UTF-8 back to the database encoding upon return.

If an SQL null value is passed to a function, the argument value will appear asundefined 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 addSTRICT to the function definition to makePostgreSQL 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 wantedperl_max with one null and one nonnull argument to return the nonnull argument, rather than a null value:

CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$    my ($x, $y) = @_;    if (not defined $x) {        return undef if not defined $y;        return $y;    }    return $x if not defined $y;    return $x if $x > $y;    return $y;$$ LANGUAGE plperl;

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 standardPostgreSQL 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, thedecode_bytea function can be used to convert an argument of typebytea into unescaped binary.

Similarly, values passed back toPostgreSQL must be in the external text representation format. For example, theencode_bytea function can be used to escape binary data for a return value of typebytea.

One case that is particularly important is boolean values. As just stated, the default behavior forbool values is that they are passed to Perl as text, thus either't' or'f'. This is problematic, since Perl will not treat'f' as false! It is possible to improve matters by using atransform (seeCREATE TRANSFORM). Suitable transforms are provided by thebool_plperl extension. To use it, install the extension:

CREATE EXTENSION bool_plperl;  -- or bool_plperlu for PL/PerlU

Then use theTRANSFORM function attribute for a PL/Perl function that takes or returnsbool, for example:

CREATE FUNCTION perl_and(bool, bool) RETURNS boolTRANSFORM FOR TYPE boolAS $$  my ($a, $b) = @_;  return $a && $b;$$ LANGUAGE plperl;

When this transform is applied,bool arguments will be seen by Perl as being1 or empty, thus properly true or false. If the function result is typebool, 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 (Section 43.3.1).

Perl can returnPostgreSQL arrays as references to Perl arrays. Here is an example:

CREATE OR REPLACE function returns_array()RETURNS text[][] AS $$    return [['a"b','c,d'],['e\\f','g']];$$ LANGUAGE plperl;select returns_array();

Perl passesPostgreSQL arrays as a blessedPostgreSQL::InServer::ARRAY object. This object may be treated as an array reference or a string, allowing for backward compatibility with Perl code written forPostgreSQL 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']);

Note

Multidimensional 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 composite type. Here is an example:

CREATE TABLE employee (    name text,    basesalary integer,    bonus integer);CREATE FUNCTION empcomp(employee) RETURNS integer AS $$    my ($emp) = @_;    return $emp->{basesalary} + $emp->{bonus};$$ LANGUAGE plperl;SELECT name, empcomp(employee.*) FROM employee;

A PL/Perl function can return a composite-type result using the same approach: return a reference to a hash that has the required attributes. For example:

CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$    return {f2 => 'hello', f1 => 1, f3 => 'world'};$$ LANGUAGE plperl;SELECT * FROM perl_row();

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

Similarly, output arguments of procedures can be returned as a hash reference:

CREATE PROCEDURE perl_triple(INOUT a integer, INOUT b integer) AS $$    my ($a, $b) = @_;    return {a => $a * 3, b => $b * 3};$$ LANGUAGE plperl;CALL perl_triple(5, 10);

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 queuing up the entire result set in memory. You can do this withreturn_next as illustrated below. Note that after the lastreturn_next, you must put eitherreturn or (better)return undef.

CREATE OR REPLACE FUNCTION perl_set_int(int)RETURNS SETOF INTEGER AS $$    foreach (0..$_[0]) {        return_next($_);    }    return undef;$$ LANGUAGE plperl;SELECT * FROM perl_set_int(5);CREATE OR REPLACE FUNCTION perl_set()RETURNS SETOF testrowperl AS $$    return_next({ f1 => 1, f2 => 'Hello', f3 => 'World' });    return_next({ f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' });    return_next({ f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' });    return undef;$$ LANGUAGE plperl;

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:

CREATE OR REPLACE FUNCTION perl_set_int(int) RETURNS SETOF INTEGER AS $$    return [0..$_[0]];$$ LANGUAGE plperl;SELECT * FROM perl_set_int(5);CREATE OR REPLACE FUNCTION perl_set() RETURNS SETOF testrowperl AS $$    return [        { f1 => 1, f2 => 'Hello', f3 => 'World' },        { f1 => 2, f2 => 'Hello', f3 => 'PostgreSQL' },        { f1 => 3, f2 => 'Hello', f3 => 'PL/Perl' }    ];$$ LANGUAGE plperl;SELECT * FROM perl_set();

If you wish to use thestrict pragma with your code you have a few options. For temporary global use you canSETplperl.use_strict to true. This will affect subsequent compilations ofPL/Perl functions, but not functions already compiled in the current session. For permanent global use you can setplperl.use_strict to true in thepostgresql.conf file.

For permanent use in specific functions you can simply put:

use strict;

at the top of the function body.

Thefeature pragma is also available touse if your Perl is version 5.10.0 or higher.


Prev Up Next
Chapter 43. PL/Perl — Perl Procedural Language Home 43.2. Data Values in PL/Perl
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp