1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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>43.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="43.3. Built-in Functions" /><link rel="next" href="plperl-trusted.html" title="43.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">43.4. Global Values in PL/Perl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-builtins.html" title="43.3. Built-in Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 43. PL/Perl — Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 43. PL/Perl — Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-trusted.html" title="43.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">43.4. Global Values in PL/Perl <a href="#PLPERL-GLOBAL" class="id_link">#</a></h2></div></div></div><p>
3 You can use the global hash <code class="varname">%_SHARED</code> to store
4 data, including code references, between function calls for the
5 lifetime of the current session.
7 Here is a simple example for shared data:
8 </p><pre class="programlisting">
9 CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$
10 if ($_SHARED{$_[0]} = $_[1]) {
13 return "cannot set shared variable $_[0] to $_[1]";
17 CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
18 return $_SHARED{$_[0]};
21 SELECT set_var('sample', 'Hello, PL/Perl! How''s tricks?');
22 SELECT get_var('sample');
25 Here is a slightly more complicated example using a code reference:
27 </p><pre class="programlisting">
28 CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
29 $_SHARED{myquote} = sub {
31 $arg =~ s/(['\\])/\\$1/g;
36 SELECT myfuncs(); /* initializes the function */
38 /* Set up a function that uses the quote function */
40 CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
41 my $text_to_quote = shift;
42 my $qfunc = $_SHARED{myquote};
43 return &$qfunc($text_to_quote);
47 (You could have replaced the above with the one-liner
48 <code class="literal">return $_SHARED{myquote}->($_[0]);</code>
49 at the expense of readability.)
51 For security reasons, PL/Perl executes functions called by any one SQL role
52 in a separate Perl interpreter for that role. This prevents accidental or
53 malicious interference by one user with the behavior of another user's
54 PL/Perl functions. Each such interpreter has its own value of the
55 <code class="varname">%_SHARED</code> variable and other global state. Thus, two
56 PL/Perl functions will share the same value of <code class="varname">%_SHARED</code>
57 if and only if they are executed by the same SQL role. In an application
58 wherein a single session executes code under multiple SQL roles (via
59 <code class="literal">SECURITY DEFINER</code> functions, use of <code class="command">SET ROLE</code>, etc.)
60 you may need to take explicit steps to ensure that PL/Perl functions can
61 share data via <code class="varname">%_SHARED</code>. To do that, make sure that
62 functions that should communicate are owned by the same user, and mark
63 them <code class="literal">SECURITY DEFINER</code>. You must of course take care that
64 such functions can't be used to do anything unintended.
65 </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="43.3. Built-in Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html" title="Chapter 43. PL/Perl — Perl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-trusted.html" title="43.5. Trusted and Untrusted PL/Perl">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.3. Built-in Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 43.5. Trusted and Untrusted PL/Perl</td></tr></table></div></body></html>