|
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 $ --> |
2 | 2 |
|
3 | 3 | <chapter id="plperl">
|
4 | 4 | <title>PL/Perl - Perl Procedural Language</title>
|
@@ -137,14 +137,44 @@ $$ LANGUAGE plperl;
|
137 | 137 | function is strict or not.
|
138 | 138 | </para>
|
139 | 139 |
|
| 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 | + |
140 | 170 | <para>
|
141 | 171 | Perl can return <productname>PostgreSQL</productname> arrays as
|
142 | 172 | references to Perl arrays. Here is an example:
|
143 | 173 |
|
144 | 174 | <programlisting>
|
145 | 175 | CREATE OR REPLACE function returns_array()
|
146 | 176 | RETURNS text[][] AS $$
|
147 |
| - return [['a"b','c,d'],['e\\f','g']]; |
| 177 | + return [['a"b','c,d'],['e\\f','g']]; |
148 | 178 | $$ LANGUAGE plperl;
|
149 | 179 |
|
150 | 180 | select returns_array();
|
|
0 commit comments