File: plperl.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (260 lines) | stat: -rw-r--r-- 12,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter38.PL/Perl - Perl Procedural Language</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="server-programming.html" title="PartV.Server Programming">
<link rel="prev" href="pltcl-procnames.html" title="37.8.Tcl Procedure Names">
<link rel="next" href="plperl-database.html" title="38.2.Database Access from PL/Perl">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="plperl">
<div class="titlepage"><div><div><h2 class="title">
<a name="plperl"></a>Chapter38.PL/Perl - Perl Procedural Language</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="plperl.html#plperl-funcs">38.1. PL/Perl Functions and Arguments</a></span></dt>
<dt><span class="sect1"><a href="plperl-database.html">38.2. Database Access from PL/Perl</a></span></dt>
<dt><span class="sect1"><a href="plperl-data.html">38.3. Data Values in PL/Perl</a></span></dt>
<dt><span class="sect1"><a href="plperl-global.html">38.4. Global Values in PL/Perl</a></span></dt>
<dt><span class="sect1"><a href="plperl-trusted.html">38.5. Trusted and Untrusted PL/Perl</a></span></dt>
<dt><span class="sect1"><a href="plperl-triggers.html">38.6. PL/Perl Triggers</a></span></dt>
<dt><span class="sect1"><a href="plperl-missing.html">38.7. Limitations and Missing Features</a></span></dt>
</dl>
</div>
<a name="id731256"></a><a name="id731265"></a><p>   PL/Perl is a loadable procedural language that enables you to write
   <span class="productname">PostgreSQL</span> functions in the 
   <a href="http://www.perl.com" target="_top">Perl programming language</a>.
  </p>
<p>   To install PL/Perl in a particular database, use
   <code class="literal">createlang plperl <em class="replaceable"><code>dbname</code></em></code>.
  </p>
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Tip</h3>
<p>    If a language is installed into <code class="literal">template1</code>, all subsequently
    created databases will have the language installed automatically.
   </p>
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>    Users of source packages must specially enable the build of
    PL/Perl during the installation process.  (Refer to <a href="installation.html#install-short" title="14.1.Short Version">Section14.1, &#8220;Short Version&#8221;</a> for more information.)  Users of
    binary packages might find PL/Perl in a separate subpackage.
   </p>
</div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="plperl-funcs"></a>38.1.PL/Perl Functions and Arguments</h2></div></div></div>
<p>   To create a function in the PL/Perl language, use the standard
   <a href="sql-createfunction.html">CREATE FUNCTION</a>
   syntax:

</p>
<pre class="programlisting">CREATE FUNCTION <em class="replaceable"><code>funcname</code></em> (<em class="replaceable"><code>argument-types</code></em>) RETURNS <em class="replaceable"><code>return-type</code></em> AS $$
    # PL/Perl function body
$$ LANGUAGE plperl;</pre>
<p>
   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.
  </p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>    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 <code class="literal">perldiag</code>
    man page for more details.
   </p>
</div>
<p>   The syntax of the <code class="command">CREATE FUNCTION</code> command requires
   the function body to be written as a string constant.  It is usually
   most convenient to use dollar quoting (see <a href="sql-syntax.html#sql-syntax-dollar-quoting" title="4.1.2.2.Dollar-Quoted String Constants">Section4.1.2.2, &#8220;Dollar-Quoted String Constants&#8221;</a>) for the string constant.
   If you choose to use regular single-quoted string constant syntax,
   you must escape single quote marks (<code class="literal">'</code>) and backslashes
   (<code class="literal">\</code>) used in the body of the function, typically by
   doubling them (see <a href="sql-syntax.html#sql-syntax-strings" title="4.1.2.1.String Constants">Section4.1.2.1, &#8220;String Constants&#8221;</a>).
  </p>
<p>   Arguments and results are handled as in any other Perl subroutine:
   arguments are passed in <code class="varname">@_</code>, and a result value
   is returned with <code class="literal">return</code> or as the last expression
   evaluated in the function.
  </p>
<p>   For example, a function returning the greater of two integer values
   could be defined as:

</p>
<pre class="programlisting">CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
    if ($_[0] &gt; $_[1]) { return $_[0]; }
    return $_[1];
$$ LANGUAGE plperl;</pre>
<p>
  </p>
<p>   If an SQL null value<a name="id731475"></a> is passed to a function,
   the argument value will appear as &#8220;<span class="quote">undefined</span>&#8221; 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 <code class="literal">STRICT</code> to the function definition to make
   <span class="productname">PostgreSQL</span> 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 <code class="function">perl_max</code> with
   one null and one nonnull argument to return the nonnull argument,
   rather than a null value:

</p>
<pre class="programlisting">CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
    my ($x,$y) = @_;
    if (! defined $x) {
        if (! defined $y) { return undef; }
        return $y;
    }
    if (! defined $y) { return $x; }
    if ($x &gt; $y) { return $x; }
    return $y;
$$ LANGUAGE plperl;</pre>
<p>
   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.
  </p>
<p>   Perl can return <span class="productname">PostgreSQL</span> arrays as
   references to Perl arrays.  Here is an example:

</p>
<pre class="programlisting">CREATE OR REPLACE function returns_array()
RETURNS text[][] AS $$
    return [['a"b','c,d'],['e\\f','g']];
$$ LANGUAGE plperl;

select returns_array();</pre>
<p>
  </p>
<p>   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:

</p>
<pre class="programlisting">CREATE TABLE employee (
    name text,
    basesalary integer,
    bonus integer
);

CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
    my ($emp) = @_;
    return $emp-&gt;{basesalary} + $emp-&gt;{bonus};
$$ LANGUAGE plperl;

SELECT name, empcomp(employee.*) FROM employee;</pre>
<p>
  </p>
<p>   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,

</p>
<pre class="programlisting">CREATE TYPE testrowperl AS (f1 integer, f2 text, f3 text);

CREATE OR REPLACE FUNCTION perl_row() RETURNS testrowperl AS $$
    return {f2 =&gt; 'hello', f1 =&gt; 1, f3 =&gt; 'world'};
$$ LANGUAGE plperl;

SELECT * FROM perl_row();</pre>
<p>

   Any columns in the declared result data type that are not present in the
   hash will be returned as NULLs.
  </p>
<p>    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
    <code class="function">return_next</code> as illustrated below.  Note that
    after the last <code class="function">return_next</code>, you must put
    either <code class="literal">return</code> or (better) <code class="literal">return
    undef</code>.

</p>
<pre class="programlisting">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 =&gt; 1, f2 =&gt; 'Hello', f3 =&gt; 'World' });
    return_next({ f1 =&gt; 2, f2 =&gt; 'Hello', f3 =&gt; 'PostgreSQL' });
    return_next({ f1 =&gt; 3, f2 =&gt; 'Hello', f3 =&gt; 'PL/Perl' });
    return undef;
$$ LANGUAGE plperl;</pre>
<p>

    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:

</p>
<pre class="programlisting">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 =&gt; 1, f2 =&gt; 'Hello', f3 =&gt; 'World' },
        { f1 =&gt; 2, f2 =&gt; 'Hello', f3 =&gt; 'PostgreSQL' },
        { f1 =&gt; 3, f2 =&gt; 'Hello', f3 =&gt; 'PL/Perl' }
    ];
$$ LANGUAGE plperl;

SELECT * FROM perl_set();</pre>
<p>

  </p>
<p>     <span class="application">PL/Perl</span> does not currently have full support for
     domain types: it treats a domain the same as the underlying scalar
     type.  This means that constraints associated with the domain will
     not be enforced.  This is not an issue for function arguments, but
     it is a hazard if you declare a <span class="application">PL/Perl</span> function
     as returning a domain type.
    </p>
<p>   If you wish to use the <code class="literal">strict</code> pragma with your code,
   the easiest way to do so is to <code class="command">SET</code>
   <code class="literal">plperl.use_strict</code> to true.  This parameter affects
   subsequent compilations of <span class="application">PL/Perl</span> functions, but not
   functions already compiled in the current session.  To set the
   parameter before <span class="application">PL/Perl</span> has been loaded, it is
   necessary to have added &#8220;<span class="quote"><code class="literal">plperl</code></span>&#8221; to the <a href="runtime-config-custom.html#guc-custom-variable-classes">custom_variable_classes</a> list in
   <code class="filename">postgresql.conf</code>.
  </p>
<p>   Another way to use the <code class="literal">strict</code> pragma is to put
</p>
<pre class="programlisting">use strict;</pre>
<p>
   in the function body.  But this only works in <span class="application">PL/PerlU</span>
   functions, since <code class="literal">use</code> is not a trusted operation.  In
   <span class="application">PL/Perl</span> functions you can instead do
</p>
<pre class="programlisting">BEGIN { strict-&gt;import(); }</pre>
<p>
  </p>
</div>
</div></body>
</html>