gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

65 líneas
4.8KB

  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>44.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 V1.79.1" /><link rel="prev" href="plperl-builtins.html" title="44.3. Built-in Functions" /><link rel="next" href="plperl-trusted.html" title="44.5. Trusted and Untrusted PL/Perl" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">44.4. Global Values in PL/Perl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-builtins.html" title="44.3. Built-in Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 44. PL/Perl - Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 44. PL/Perl - Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-trusted.html" title="44.5. Trusted and Untrusted PL/Perl">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPERL-GLOBAL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">44.4. Global Values in PL/Perl</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.
  6. </p><p>
  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]) {
  11. return 'ok';
  12. } else {
  13. return "cannot set shared variable $_[0] to $_[1]";
  14. }
  15. $$ LANGUAGE plperl;
  16. CREATE OR REPLACE FUNCTION get_var(name text) RETURNS text AS $$
  17. return $_SHARED{$_[0]};
  18. $$ LANGUAGE plperl;
  19. SELECT set_var('sample', 'Hello, PL/Perl! How''s tricks?');
  20. SELECT get_var('sample');
  21. </pre><p>
  22. </p><p>
  23. Here is a slightly more complicated example using a code reference:
  24. </p><pre class="programlisting">
  25. CREATE OR REPLACE FUNCTION myfuncs() RETURNS void AS $$
  26. $_SHARED{myquote} = sub {
  27. my $arg = shift;
  28. $arg =~ s/(['\\])/\\$1/g;
  29. return "'$arg'";
  30. };
  31. $$ LANGUAGE plperl;
  32. SELECT myfuncs(); /* initializes the function */
  33. /* Set up a function that uses the quote function */
  34. CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS text AS $$
  35. my $text_to_quote = shift;
  36. my $qfunc = $_SHARED{myquote};
  37. return &amp;$qfunc($text_to_quote);
  38. $$ LANGUAGE plperl;
  39. </pre><p>
  40. (You could have replaced the above with the one-liner
  41. <code class="literal">return $_SHARED{myquote}-&gt;($_[0]);</code>
  42. at the expense of readability.)
  43. </p><p>
  44. For security reasons, PL/Perl executes functions called by any one SQL role
  45. in a separate Perl interpreter for that role. This prevents accidental or
  46. malicious interference by one user with the behavior of another user's
  47. PL/Perl functions. Each such interpreter has its own value of the
  48. <code class="varname">%_SHARED</code> variable and other global state. Thus, two
  49. PL/Perl functions will share the same value of <code class="varname">%_SHARED</code>
  50. if and only if they are executed by the same SQL role. In an application
  51. wherein a single session executes code under multiple SQL roles (via
  52. <code class="literal">SECURITY DEFINER</code> functions, use of <code class="command">SET ROLE</code>, etc)
  53. you may need to take explicit steps to ensure that PL/Perl functions can
  54. share data via <code class="varname">%_SHARED</code>. To do that, make sure that
  55. functions that should communicate are owned by the same user, and mark
  56. them <code class="literal">SECURITY DEFINER</code>. You must of course take care that
  57. such functions can't be used to do anything unintended.
  58. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-trusted.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">44.3. Built-in Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 44.5. Trusted and Untrusted PL/Perl</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1