PostgreSQL 8.3.7 Documentation
Prev | Fast Backward | Chapter 40. PL/Perl - Perl Procedural Language | Fast Forward | Next

# 40.4. Global Values in PL/Perl

You can use the global hash `%_SHARED` to store data, including code references, between function calls for the lifetime of the current session.

Here is a simple example for shared data:

    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');

Here is a slightly more complicated example using a code reference:

    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 &$qfunc($text_to_quote);
    $$ LANGUAGE plperl;

(You could have replaced the above with the one-liner `return $_SHARED{myquote}->($_[0]);` at the expense of readability.)

Prev | Home | Next
Data Values in PL/Perl | Up | Trusted and Untrusted PL/Perl