summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/plperl-global.html
blob: 347df24b794fb55882bd96986fb7da628b85eb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>45.4. Global Values in PL/Perl</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plperl-builtins.html" title="45.3. Built-in Functions" /><link rel="next" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">45.4. Global Values in PL/Perl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-builtins.html" title="45.3. Built-in Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 45. PL/Perl — Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPERL-GLOBAL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">45.4. Global Values in PL/Perl</h2></div></div></div><p>
    You can use the global hash <code class="varname">%_SHARED</code> to store
    data, including code references, between function calls for the
    lifetime of the current session.
  </p><p>
    Here is a simple example for shared data:
</p><pre class="programlisting">
CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
    if ($_SHARED{$_[0]} = $_[1]) {
        return 'ok';
    } else {
        return "cannot set shared variable $_[0] to $_[1]";
    }
$$ LANGUAGE plperl;

CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
    return $_SHARED{$_[0]};
$$ LANGUAGE plperl;

SELECT set_var('sample', 'Hello, PL/Perl!  How''s tricks?');
SELECT get_var('sample');
</pre><p>
  </p><p>
   Here is a slightly more complicated example using a code reference:

</p><pre class="programlisting">
CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
    $_SHARED{myquote} = sub {
        my $arg = shift;
        $arg =~ s/(['\\])/\\$1/g;
        return "'$arg'";
    };
$$ LANGUAGE plperl;

SELECT myfuncs(); /* initializes the function */

/* Set up a function that uses the quote function */

CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
    my $text_to_quote = shift;
    my $qfunc = $_SHARED{myquote};
    return &amp;$qfunc($text_to_quote);
$$ LANGUAGE plperl;
</pre><p>

   (You could have replaced the above with the one-liner
   <code class="literal">return $_SHARED{myquote}-&gt;($_[0]);</code>
   at the expense of readability.)
  </p><p>
   For security reasons, PL/Perl executes functions called by any one SQL role
   in a separate Perl interpreter for that role.  This prevents accidental or
   malicious interference by one user with the behavior of another user's
   PL/Perl functions.  Each such interpreter has its own value of the
   <code class="varname">%_SHARED</code> variable and other global state.  Thus, two
   PL/Perl functions will share the same value of <code class="varname">%_SHARED</code>
   if and only if they are executed by the same SQL role.  In an application
   wherein a single session executes code under multiple SQL roles (via
   <code class="literal">SECURITY DEFINER</code> functions, use of <code class="command">SET ROLE</code>, etc.)
   you may need to take explicit steps to ensure that PL/Perl functions can
   share data via <code class="varname">%_SHARED</code>.  To do that, make sure that
   functions that should communicate are owned by the same user, and mark
   them <code class="literal">SECURITY DEFINER</code>.  You must of course take care that
   such functions can't be used to do anything unintended.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plperl-builtins.html" title="45.3. Built-in Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl">Next</a></td></tr><tr><td width="40%" align="left" valign="top">45.3. Built-in Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 45.5. Trusted and Untrusted PL/Perl</td></tr></table></div></body></html>