Folks,
Please find enclosed a patch to the pl/perl documents that clafies the
scope of global data and gives an example of storing a code reference.
Cheers,
D
--
David Fetter [EMAIL PROTECTED] http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
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 ****
<title>Global Values in PL/Perl</title>
<para>
! You can use the global hash <varname>%_SHARED</varname> to store
! data between function calls. For example:
<programlisting>
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
if ($_SHARED{$_[0]} = $_[1]) {
--- 315,328 ----
<title>Global Values in PL/Perl</title>
<para>
! You can use the global hash <varname>%_SHARED</varname> 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.
! </para>
! <para>
! Here is a simple example for shared data:
!
<programlisting>
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');
</programlisting>
+ Here is a slightly more complicated example using a code reference:
+ <programlisting>
+ 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.
+ $$;
+ </programlisting>
</para>
</sect1>
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]