11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.22 2003/12/14 00:10:32 neilc Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.23 2004/05/16 23:22:06 neilc Exp $
33-->
44
55 <chapter id="plperl">
@@ -46,11 +46,17 @@ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.22 2003/12/14 00:10:32 neilc Exp
4646 <para>
4747 To create a function in the PL/Perl language, use the standard syntax:
4848<programlisting>
49- CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS '
49+ CREATE FUNCTION <replaceable>funcname</replaceable>
50+ (<replaceable>argument-types</replaceable>) RETURNS <replaceable>return-type</replaceable> AS $$
5051 # PL/Perl function body
51- ' LANGUAGE plperl;
52+ $$ LANGUAGE plperl;
5253</programlisting>
53- The body of the function is ordinary Perl code.
54+ The body of the function is ordinary Perl code. Since the body of
55+ the function is treated as a string by
56+ <productname>PostgreSQL</productname>, it can be specified using
57+ dollar quoting (as shown above), or via the usual single quote
58+ syntax (see <xref linkend="sql-syntax-strings"> for more
59+ information).
5460 </para>
5561
5662 <para>
@@ -65,10 +71,10 @@ CREATE FUNCTION <replaceable>funcname</replaceable> (<replaceable>argument-types
6571 could be defined as:
6672
6773<programlisting>
68- CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
74+ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
6975 if ($_[0] > $_[1]) { return $_[0]; }
7076 return $_[1];
71- ' LANGUAGE plperl;
77+ $$ LANGUAGE plperl;
7278</programlisting>
7379 </para>
7480
@@ -88,7 +94,7 @@ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
8894 rather than a null value:
8995
9096<programlisting>
91- CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
97+ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
9298 my ($a,$b) = @_;
9399 if (! defined $a) {
94100 if (! defined $b) { return undef; }
@@ -97,7 +103,7 @@ CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS '
97103 if (! defined $b) { return $a; }
98104 if ($a > $b) { return $a; }
99105 return $b;
100- ' LANGUAGE plperl;
106+ $$ LANGUAGE plperl;
101107</programlisting>
102108 </para>
103109
@@ -119,10 +125,10 @@ CREATE TABLE employee (
119125 bonus integer
120126);
121127
122- CREATE FUNCTION empcomp(employee) RETURNS integer AS '
128+ CREATE FUNCTION empcomp(employee) RETURNS integer AS $$
123129 my ($emp) = @_;
124- return $emp->{'' basesalary'' } + $emp->{'' bonus' '};
125- ' LANGUAGE plperl;
130+ return $emp->{'basesalary'} + $emp->{'bonus'};
131+ $$ LANGUAGE plperl;
126132
127133SELECT name, empcomp(employee) FROM employee;
128134</programlisting>
@@ -136,12 +142,12 @@ SELECT name, empcomp(employee) FROM employee;
136142 <tip>
137143 <para>
138144 Because the function body is passed as an SQL string literal to
139- <command>CREATE FUNCTION</command>, you have to escape single
140- quotes and backslashes within your Perl source, typically by
141- doubling them as shown in the above example. Another possible
142- approach is to avoid writing single quotes by using Perl's
143- extended quoting operators (<literal>q[]</literal>,
144- <literal>qq[]</literal>, <literal> qw[]</literal>).
145+ <command>CREATE FUNCTION</command>, you have to use dollar quoting
146+ or escape single quotes and backslashes within your Perl source,
147+ typically by doubling them. Another possible approach is to avoid
148+ writing single quotes by using Perl's extended quoting operators
149+ (<literal>q[]</literal>, <literal>qq []</literal>,
150+ <literal>qw[]</literal>).
145151 </para>
146152 </tip>
147153 </sect1>
@@ -226,11 +232,11 @@ SELECT name, empcomp(employee) FROM employee;
226232 Here is an example of a function that will not work because file
227233 system operations are not allowed for security reasons:
228234<programlisting>
229- CREATE FUNCTION badfunc() RETURNS integer AS '
235+ CREATE FUNCTION badfunc() RETURNS integer AS $$
230236 open(TEMP, ">/tmp/badfile");
231237 print TEMP "Gotcha!\n";
232238 return 1;
233- ' LANGUAGE plperl;
239+ $$ LANGUAGE plperl;
234240</programlisting>
235241 The creation of the function will succeed, but executing it will not.
236242 </para>
0 commit comments