Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd89042b

Browse files
committed
Patch to the pl/perl documents that clarifies the scope of global data and
gives an example of storing a code referenceby David Fetter
1 parente82cd78 commitd89042b

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

‎doc/src/sgml/plperl.sgml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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>
321327
CREATE 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?');
334340
SELECT 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">

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp