Skip to content

Commit 774de1d

Browse files
committed
Make clearer how arguments and return values in pl/perl are escaped. This is to clarify the situation that Theo Schlossnagle recently reported on -bugs.
1 parent fab789e commit 774de1d

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

doc/src/sgml/plperl.sgml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.65 2007/05/03 15:05:56 neilc Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.66 2007/05/04 14:55:32 adunstan Exp $ -->
22

33
<chapter id="plperl">
44
<title>PL/Perl - Perl Procedural Language</title>
@@ -137,14 +137,44 @@ $$ LANGUAGE plperl;
137137
function is strict or not.
138138
</para>
139139

140+
<para>
141+
Anything in a function argument that is not a reference is
142+
a string, which is in the standard <productname>PostgreSQL</productname>
143+
external text representation for the relevant data type. In the case of
144+
ordinary numeric or text types, Perl will just do the right thing and
145+
the programmer will normally not have to worry about it. However, in
146+
other cases the argument will need to be converted into a form that is
147+
more usable in Perl. For example, here is how to convert an argument of
148+
type <type>bytea</> into unescaped binary
149+
data:
150+
151+
<programlisting>
152+
my $arg = shift;
153+
$arg =~ s!\\(\d{3})!chr(oct($1))!ge;
154+
</programlisting>
155+
156+
</para>
157+
158+
<para>
159+
Similarly, values passed back to <productname>PostgreSQL</productname>
160+
must be in the external text representation format. For example, here
161+
is how to escape binary data for a return value of type <type>bytea</>:
162+
163+
<programlisting>
164+
$retval =~ s!([^ -~])!sprintf("\\%03o",ord($1))!ge;
165+
return $retval;
166+
</programlisting>
167+
168+
</para>
169+
140170
<para>
141171
Perl can return <productname>PostgreSQL</productname> arrays as
142172
references to Perl arrays. Here is an example:
143173

144174
<programlisting>
145175
CREATE OR REPLACE function returns_array()
146176
RETURNS text[][] AS $$
147-
return [['a"b','c,d'],['e\\f','g']];
177+
return [['a&quot;b','c,d'],['e\\f','g']];
148178
$$ LANGUAGE plperl;
149179

150180
select returns_array();

0 commit comments

Comments
 (0)