11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.32 2004/11/21 21:17:01 tgl Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.33 2004/12/11 20:03:37 petere Exp $
33-->
44
55 <chapter id="plperl">
@@ -315,8 +315,14 @@ $$ LANGUAGE plperl;
315315 <title>Global Values in PL/Perl</title>
316316
317317 <para>
318- You can use the global hash <varname>%_SHARED</varname> to store
319- data between function calls. For example:
318+ You can use the global hash <varname>%_SHARED</varname> to store
319+ data, including code references, between function calls for the
320+ lifetime of the current session, which is bounded from below by
321+ the lifetime of the current transaction.
322+ </para>
323+
324+ <para>
325+ Here is a simple example for shared data:
320326<programlisting>
321327CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
322328 if ($_SHARED{$_[0]} = $_[1]) {
@@ -334,6 +340,34 @@ SELECT set_var('sample', 'Hello, PL/Perl! How's tricks?');
334340SELECT get_var('sample');
335341</programlisting>
336342 </para>
343+
344+ <para>
345+ Here is a slightly more complicated example using a code reference:
346+
347+ <programlisting>
348+ CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
349+ $_SHARED{myquote} = sub {
350+ my $arg = shift;
351+ $arg =~ s/(['\\])/\\$1/g;
352+ return "'$arg'";
353+ };
354+ $$ LANGUAGE plperl;
355+
356+ SELECT myfuncs(); /* initializes the function */
357+
358+ /* Set up a function that uses the quote function */
359+
360+ CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
361+ my $text_to_quote = shift;
362+ my $qfunc = $_SHARED{myquote};
363+ return &$qfunc($text_to_quote);
364+ $$ LANGUAGE plperl;
365+ </programlisting>
366+
367+ (You could have replaced the above with the one-liner
368+ <literal>return $_SHARED{myquote}->($_[0]);</literal>
369+ at the expense of readability.)
370+ </para>
337371 </sect1>
338372
339373 <sect1 id="plperl-trusted">