1- <!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.72 2010/01/09 02:40:50 adunstan Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.73 2010/01/20 01:08:21 adunstan Exp $ -->
22
33 <chapter id="plperl">
44 <title>PL/Perl - Perl Procedural Language</title>
1313
1414 <para>
1515 PL/Perl is a loadable procedural language that enables you to write
16- <productname>PostgreSQL</productname> functions in the
16+ <productname>PostgreSQL</productname> functions in the
1717 <ulink url="http://www.perl.org">Perl programming language</ulink>.
1818 </para>
1919
@@ -101,7 +101,7 @@ $$ LANGUAGE plperl;
101101 linkend="sql-syntax-dollar-quoting">) for the string constant.
102102 If you choose to use escape string syntax <literal>E''</>,
103103 you must double the single quote marks (<literal>'</>) and backslashes
104- (<literal>\</>) used in the body of the function
104+ (<literal>\</>) used in the body of the function
105105 (see <xref linkend="sql-syntax-strings">).
106106 </para>
107107
@@ -141,13 +141,13 @@ $$ LANGUAGE plperl;
141141
142142<programlisting>
143143CREATE FUNCTION perl_max (integer, integer) RETURNS integer AS $$
144- my ($x,$y) = @_;
145- if (! defined $x) {
146- if (! defined $y) { return undef; }
144+ my ($x, $y) = @_;
145+ if (not defined $x) {
146+ return undef if not defined $y;
147147 return $y;
148148 }
149- if (! defined $y) { return $x; }
150- if ( $x > $y) { return $x; }
149+ return $x if not defined $y;
150+ return $x if $x > $y;
151151 return $y;
152152$$ LANGUAGE plperl;
153153</programlisting>
@@ -158,32 +158,21 @@ $$ LANGUAGE plperl;
158158
159159 <para>
160160 Anything in a function argument that is not a reference is
161- a string, which is in the standard <productname>PostgreSQL</productname>
161+ a string, which is in the standard <productname>PostgreSQL</productname>
162162 external text representation for the relevant data type. In the case of
163163 ordinary numeric or text types, Perl will just do the right thing and
164164 the programmer will normally not have to worry about it. However, in
165- other cases the argument will need to be converted into a form that is
166- more usable in Perl. For example, here is how to convert an argument of
167- type <type>bytea</> into unescaped binary
168- data:
169-
170- <programlisting>
171- my $arg = shift;
172- $arg =~ s!\\(?:\\|(\d{3}))!$1 ? chr(oct($1)) : "\\"!ge;
173- </programlisting>
174-
165+ other cases the argument will need to be converted into a form that is
166+ more usable in Perl. For example, the <function>decode_bytea</function>
167+ function can be used to convert an argument of
168+ type <type>bytea</> into unescaped binary.
175169 </para>
176170
177171 <para>
178- Similarly, values passed back to <productname>PostgreSQL</productname>
179- must be in the external text representation format. For example, here
180- is how to escape binary data for a return value of type <type>bytea</>:
181-
182- <programlisting>
183- $retval =~ s!(\\|[^ -~])!sprintf("\\%03o",ord($1))!ge;
184- return $retval;
185- </programlisting>
186-
172+ Similarly, values passed back to <productname>PostgreSQL</productname>
173+ must be in the external text representation format. For example, the
174+ <function>encode_bytea</function> function can be used to
175+ to escape binary data for a return value of type <type>bytea</>.
187176 </para>
188177
189178 <para>
@@ -322,7 +311,10 @@ BEGIN { strict->import(); }
322311 </para>
323312 </sect1>
324313
325- <sect1 id="plperl-database">
314+ <sect1 id="plperl-builtins">
315+ <title>Built-in Functions</title>
316+
317+ <sect2 id="plperl-database">
326318 <title>Database Access from PL/Perl</title>
327319
328320 <para>
@@ -340,7 +332,7 @@ BEGIN { strict->import(); }
340332 <term><literal><function>spi_query</>(<replaceable>command</replaceable>)</literal></term>
341333 <term><literal><function>spi_fetchrow</>(<replaceable>cursor</replaceable>)</literal></term>
342334 <term><literal><function>spi_prepare</>(<replaceable>command</replaceable>, <replaceable>argument types</replaceable>)</literal></term>
343- <term><literal><function>spi_exec_prepared</>(<replaceable>plan</replaceable>)</literal></term>
335+ <term><literal><function>spi_exec_prepared</>(<replaceable>plan</replaceable>, <replaceable>arguments</replaceable> )</literal></term>
344336 <term><literal><function>spi_query_prepared</>(<replaceable>plan</replaceable> [, <replaceable>attributes</replaceable>], <replaceable>arguments</replaceable>)</literal></term>
345337 <term><literal><function>spi_cursor_close</>(<replaceable>cursor</replaceable>)</literal></term>
346338 <term><literal><function>spi_freeplan</>(<replaceable>plan</replaceable>)</literal></term>
@@ -455,19 +447,19 @@ $$ LANGUAGE plperlu;
455447SELECT * from lotsa_md5(500);
456448</programlisting>
457449 </para>
458-
450+
459451 <para>
460- <literal>spi_prepare</literal>, <literal>spi_query_prepared</literal>, <literal>spi_exec_prepared</literal>,
452+ <literal>spi_prepare</literal>, <literal>spi_query_prepared</literal>, <literal>spi_exec_prepared</literal>,
461453 and <literal>spi_freeplan</literal> implement the same functionality but for prepared queries. Once
462454 a query plan is prepared by a call to <literal>spi_prepare</literal>, the plan can be used instead
463455 of the string query, either in <literal>spi_exec_prepared</literal>, where the result is the same as returned
464456 by <literal>spi_exec_query</literal>, or in <literal>spi_query_prepared</literal> which returns a cursor
465457 exactly as <literal>spi_query</literal> does, which can be later passed to <literal>spi_fetchrow</literal>.
466458 </para>
467-
459+
468460 <para>
469461 The advantage of prepared queries is that is it possible to use one prepared plan for more
470- than one query execution. After the plan is not needed anymore, it can be freed with
462+ than one query execution. After the plan is not needed anymore, it can be freed with
471463 <literal>spi_freeplan</literal>:
472464 </para>
473465
@@ -478,7 +470,7 @@ CREATE OR REPLACE FUNCTION init() RETURNS INTEGER AS $$
478470$$ LANGUAGE plperl;
479471
480472CREATE OR REPLACE FUNCTION add_time( INTERVAL ) RETURNS TEXT AS $$
481- return spi_exec_prepared(
473+ return spi_exec_prepared(
482474 $_SHARED{my_plan},
483475 $_[0],
484476 )->{rows}->[0]->{now};
@@ -493,7 +485,7 @@ SELECT init();
493485SELECT add_time('1 day'), add_time('2 days'), add_time('3 days');
494486SELECT done();
495487
496- add_time | add_time | add_time
488+ add_time | add_time | add_time
497489------------+------------+------------
498490 2005-12-10 | 2005-12-11 | 2005-12-12
499491 </programlisting>
@@ -516,7 +508,13 @@ SELECT done();
516508 </para>
517509 </listitem>
518510 </varlistentry>
511+ </variablelist>
512+ </sect2>
513+
514+ <sect2 id="plperl-utility-functions">
515+ <title>Utility functions in PL/Perl</title>
519516
517+ <variablelist>
520518 <varlistentry>
521519 <indexterm>
522520 <primary>elog</primary>
@@ -545,8 +543,143 @@ SELECT done();
545543 </para>
546544 </listitem>
547545 </varlistentry>
546+
547+ <varlistentry>
548+ <indexterm>
549+ <primary>quote_literal</primary>
550+ <secondary>in PL/Perl</secondary>
551+ </indexterm>
552+
553+ <term><literal><function>quote_literal</>(<replaceable>string</replaceable>)</literal></term>
554+ <listitem>
555+ <para>
556+ Return the given string suitably quoted to be used as a string literal in an SQL
557+ statement string. Embedded single-quotes and backslashes are properly doubled.
558+ Note that <function>quote_literal</> returns undef on undef input; if the argument
559+ might be undef, <function>quote_nullable</> is often more suitable.
560+ </para>
561+ </listitem>
562+ </varlistentry>
563+
564+ <varlistentry>
565+ <indexterm>
566+ <primary>quote_nullable</primary>
567+ <secondary>in PL/Perl</secondary>
568+ </indexterm>
569+
570+ <term><literal><function>quote_nullable</>(<replaceable>string</replaceable>)</literal></term>
571+ <listitem>
572+ <para>
573+ Return the given string suitably quoted to be used as a string literal in an SQL
574+ statement string; or, if the argument is undef, return the unquoted string "NULL".
575+ Embedded single-quotes and backslashes are properly doubled.
576+ </para>
577+ </listitem>
578+ </varlistentry>
579+
580+ <varlistentry>
581+ <indexterm>
582+ <primary>quote_ident</primary>
583+ <secondary>in PL/Perl</secondary>
584+ </indexterm>
585+
586+ <term><literal><function>quote_ident</>(<replaceable>string</replaceable>)</literal></term>
587+ <listitem>
588+ <para>
589+ Return the given string suitably quoted to be used as an identifier in
590+ an SQL statement string. Quotes are added only if necessary (i.e., if
591+ the string contains non-identifier characters or would be case-folded).
592+ Embedded quotes are properly doubled.
593+ </para>
594+ </listitem>
595+ </varlistentry>
596+
597+ <varlistentry>
598+ <indexterm>
599+ <primary>decode_bytea</primary>
600+ <secondary>in PL/Perl</secondary>
601+ </indexterm>
602+
603+ <term><literal><function>decode_bytea</>(<replaceable>string</replaceable>)</literal></term>
604+ <listitem>
605+ <para>
606+ Return the unescaped binary data represented by the contents of the given string,
607+ which should be bytea encoded.
608+ </para>
609+ </listitem>
610+ </varlistentry>
611+
612+ <varlistentry>
613+ <indexterm>
614+ <primary>encode_bytea</primary>
615+ <secondary>in PL/Perl</secondary>
616+ </indexterm>
617+
618+ <term><literal><function>encode_bytea</>(<replaceable>string</replaceable>)</literal></term>
619+ <listitem>
620+ <para>
621+ Return the bytea encoded form of the binary data contents of the given string.
622+ </para>
623+ </listitem>
624+ </varlistentry>
625+
626+ <varlistentry>
627+ <indexterm>
628+ <primary>encode_array_literal</primary>
629+ <secondary>in PL/Perl</secondary>
630+ </indexterm>
631+
632+ <term><literal><function>encode_array_literal</>(<replaceable>array</replaceable>)</literal></term>
633+ <term><literal><function>encode_array_literal</>(<replaceable>array</replaceable>, <replaceable>delimiter</replaceable>)</literal></term>
634+ <listitem>
635+ <para>
636+ Returns the contents of the referenced array as a string in array literal format
637+ (see <xref linkend="arrays-input">).
638+ Returns the argument value unaltered if it's not a reference to an array.
639+ The delimiter used between elements of the array literal defaults to "<literal>, </literal>"
640+ if a delimiter is not specified or is undef.
641+ </para>
642+ </listitem>
643+ </varlistentry>
644+
645+ <varlistentry>
646+ <indexterm>
647+ <primary>encode_array_constructor</primary>
648+ <secondary>in PL/Perl</secondary>
649+ </indexterm>
650+
651+ <term><literal><function>encode_array_constructor</>(<replaceable>array</replaceable>)</literal></term>
652+ <listitem>
653+ <para>
654+ Returns the contents of the referenced array as a string in array constructor format
655+ (see <xref linkend="sql-syntax-array-constructors">).
656+ Individual values are quoted using <function>quote_nullable</function>.
657+ Returns the argument value, quoted using <function>quote_nullable</function>,
658+ if it's not a reference to an array.
659+ </para>
660+ </listitem>
661+ </varlistentry>
662+
663+ <varlistentry>
664+ <indexterm>
665+ <primary>looks_like_number</primary>
666+ <secondary>in PL/Perl</secondary>
667+ </indexterm>
668+
669+ <term><literal><function>looks_like_number</>(<replaceable>string</replaceable>)</literal></term>
670+ <listitem>
671+ <para>
672+ Returns a true value if the content of the given string looks like a
673+ number, according to Perl, returns false otherwise.
674+ Returns undef if the argument is undef. Leading and trailing space is
675+ ignored. <literal>Inf</> and <literal>Infinity</> are regarded as numbers.
676+ </para>
677+ </listitem>
678+ </varlistentry>
679+
548680 </variablelist>
549681 </para>
682+ </sect2>
550683 </sect1>
551684
552685 <sect1 id="plperl-data">
@@ -587,7 +720,7 @@ CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
587720 return $_SHARED{$_[0]};
588721$$ LANGUAGE plperl;
589722
590- SELECT set_var('sample', 'Hello, PL/Perl! How's tricks?');
723+ SELECT set_var('sample', 'Hello, PL/Perl! How'' s tricks?');
591724SELECT get_var('sample');
592725</programlisting>
593726 </para>
@@ -701,15 +834,16 @@ $$ LANGUAGE plperl;
701834 However, not all installations are compiled with the requisite flags.
702835 If <productname>PostgreSQL</> detects that this is the case then it will
703836 not start a second interpreter, but instead create an error. In
704- consequence, in such an installation, you cannot use both
837+ consequence, in such an installation, you cannot use both
705838 <application>PL/PerlU</> and <application>PL/Perl</> in the same backend
706- process. The remedy for this is to obtain a Perl installation created
707- with the appropriate flags, namely either <literal>usemultiplicity</> or
708- both <literal>usethreads</> and <literal>useithreads</>.
709- For more details,see the <literal>perlembed</> manual page.
839+ process. The remedy for this is to obtain a Perl installation configured
840+ with the appropriate flags, namely either <literal>usemultiplicity</>
841+ or <literal>useithreads</>. <literal>usemultiplicity</> is preferred
842+ unless you actually need to use threads. For more details, see the
843+ <citerefentry><refentrytitle>perlembed</></citerefentry> man page.
710844 </para>
711845 </note>
712-
846+
713847 </sect1>
714848
715849 <sect1 id="plperl-triggers">
@@ -718,8 +852,8 @@ $$ LANGUAGE plperl;
718852 <para>
719853 PL/Perl can be used to write trigger functions. In a trigger function,
720854 the hash reference <varname>$_TD</varname> contains information about the
721- current trigger event. <varname>$_TD</> is a global variable,
722- which gets a separate local value for each invocation of the trigger.
855+ current trigger event. <varname>$_TD</> is a global variable,
856+ which gets a separate local value for each invocation of the trigger.
723857 The fields of the <varname>$_TD</varname> hash reference are:
724858
725859 <variablelist>
@@ -801,7 +935,7 @@ $$ LANGUAGE plperl;
801935 <listitem>
802936 <para>
803937 Name of the table on which the trigger fired. This has been deprecated,
804- and could be removed in a future release.
938+ and could be removed in a future release.
805939 Please use $_TD->{table_name} instead.
806940 </para>
807941 </listitem>
0 commit comments