Index: doc/src/sgml/plperl.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/plperl.sgml,v
retrieving revision 2.32
diff -c -r2.32 plperl.sgml
*** doc/src/sgml/plperl.sgml 21 Nov 2004 21:17:01 -0000 2.32
--- doc/src/sgml/plperl.sgml 9 Dec 2004 20:45:54 -0000
***************
*** 315,322 ****
Global Values in PL/Perl
! You can use the global hash %_SHARED to store
! data between function calls. For example:
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
if ($_SHARED{$_[0]} = $_[1]) {
--- 315,328 ----
Global Values in PL/Perl
! You can use the global hash %_SHARED to store
! data, including code references, between function calls for the
! lifetime of the current session, which is bounded from below by
! the lifetime of the current transaction.
!
!
! Here is a simple example for shared data:
!
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
if ($_SHARED{$_[0]} = $_[1]) {
***************
*** 333,338 ****
--- 339,368 ----
SELECT set_var('sample', 'Hello, PL/Perl! How's tricks?');
SELECT get_var('sample');
+ Here is a slightly more complicated example using a code reference:
+
+ CREATE OR REPLACE FUNCTION myfuncs() RETURNS VOID LANGUAGE plperl AS $$
+ $_SHARED{myquote} = sub
+ {
+ my $arg = shift;
+ $arg =~ s/(['\\])/\\$1/g;
+ return "'$arg'";
+ };
+ $$;
+
+ SELECT myfuncts(); /* Initializes the function */
+
+ /* Set up a function that uses the quote function */
+
+ CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS TEXT LANGUAGE plperl AS $$
+ my $text_to_quote = shift;
+ my $qfunc = $_SHARED{myquote};
+ return &$qfunc($text_to_quote);
+ # You could have replaced the above with the one-liner
+ # return $_SHARED{myquote}->($_[0]);
+ # at the expense of readability, but please don't code that way.
+ $$;
+