Re: [PATCHES] PL/Perl document patch
David Fetter wrote: > Please find enclosed a patch to the pl/perl documents that clafies > the scope of global data and gives an example of storing a code > reference. Patch installed. -- Peter Eisentraut http://developer.postgresql.org/~petere/ ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
[PATCHES] PL/Perl document patch
Folks, Please find enclosed a patch to the pl/perl documents that clafies the scope of global data and gives an example of storing a code reference. Cheers, D -- David Fetter [EMAIL PROTECTED] http://fetter.org/ phone: +1 510 893 6100 mobile: +1 415 235 3778 Remember to vote! Index: doc/src/sgml/plperl.sgml === RCS file: /projects/cvsroot/pgsql/doc/src/sgml/plperl.sgml,v retrieving revision 2.32 diff -c -r2.32 plperl.sgml *** doc/src/sgml/plperl.sgml21 Nov 2004 21:17:01 - 2.32 --- doc/src/sgml/plperl.sgml9 Dec 2004 20:45:54 - *** *** 315,322 Global Values in PL/Perl !You can use the global hash %_SHARED to store !data between function calls. For example: CREATE OR REPLACE FUNCTION set_var(name text, val text) RETURNS text AS $$ if ($_SHARED{$_[0]} = $_[1]) { --- 315,328 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, which is bounded from below by ! the lifetime of the current transaction. ! ! ! 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]) { *** *** 333,338 --- 339,368 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 LANGUAGE plperl AS $$ + $_SHARED{myquote} = sub + { + my $arg = shift; + $arg =~ s/(['\\])/\\$1/g; + return "'$arg'"; + }; + $$; + + SELECT myfuncts(); /* Initializes the function */ + + /* Set up a function that uses the quote function */ + + CREATE OR REPLACE FUNCTION use_quote(TEXT) RETURNS TEXT LANGUAGE plperl AS $$ + my $text_to_quote = shift; + my $qfunc = $_SHARED{myquote}; + return &$qfunc($text_to_quote); + # You could have replaced the above with the one-liner + # return $_SHARED{myquote}->($_[0]); + # at the expense of readability, but please don't code that way. + $$; + ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]
Re: [PATCHES] PL/Perl document patch
Patch applied. Thanks. --- David Fetter wrote: > Kind people, > > The enclose patch clarifies and makes a more useful example for the > Global Values in PL/Perl section of the documents. > > Cheers, > D > -- > David Fetter [EMAIL PROTECTED] http://fetter.org/ > phone: +1 510 893 6100 mobile: +1 415 235 3778 > > Remember to vote! [ Attachment, skipping... ] > > ---(end of broadcast)--- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match -- Bruce Momjian| http://candle.pha.pa.us [EMAIL PROTECTED] | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup.| Newtown Square, Pennsylvania 19073 ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
[PATCHES] PL/Perl document patch
Kind people, The enclose patch clarifies and makes a more useful example for the Global Values in PL/Perl section of the documents. Cheers, D -- David Fetter [EMAIL PROTECTED] http://fetter.org/ phone: +1 510 893 6100 mobile: +1 415 235 3778 Remember to vote! ? plperl.diff Index: plperl.sgml === RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/plperl.sgml,v retrieving revision 2.26 diff -u -r2.26 plperl.sgml --- plperl.sgml 21 Jul 2004 20:44:52 - 2.26 +++ plperl.sgml 13 Aug 2004 21:42:22 - @@ -317,23 +317,25 @@ Global Values in PL/Perl - You can use the %_SHARED to store data between function calls. WHY -IS THIS A HASH, AND NOT A HASH REF? + You can use the %_SHARED to store data between function calls. For example: -CREATE OR REPLACE FUNCTION set_var(TEXT) RETURNS TEXT AS $$ -$_SHARED{first} = 'Hello, PL/Perl!'; -return 'ok'; +CREATE OR REPLACE FUNCTION set_var(name TEXT, val TEXT) RETURNS TEXT AS $$ +if ($_SHARED{$_[0]} = $_[1]) { +return 'ok'; +} else { +return "Can't set shared variable $_[0] to $_[1]"; +} $$ LANGUAGE plperl; -CREATE OR REPLACE FUNCTION get_var() RETURNS text AS $$ -return $_SHARED{first}; +CREATE OR REPLACE FUNCTION get_var(name TEXT) RETURNS text AS $$ +return $_SHARED{$_[0]}; $$ LANGUAGE plperl; -SELECT set_var('hello plperl'); -SELECT get_var(); +SELECT set_var('sample', $q$Hello, PL/Perl! How's tricks?$q$); +SELECT get_var('sample'); ---(end of broadcast)--- TIP 9: the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match