Обсуждение: pl/perl function life and variable scope - concurrency problem?
Greetings, Thanks to a lot of help on this list, I've managed to get my pl/perl function working. However, I have an unexpected result. Here's a simple way to reproduce this problem: CREATE or REPLACE FUNCTION perltest(integer) returns integer as ' $MyInt = $MyInt + 1; return $MyInt; ' language plperlu; Executing it produces: chris=# select perltest(1); perltest ---------- 1 (1 row) chris=# select perltest(1); perltest ---------- 2 (1 row) chris=# select perltest(1); perltest ---------- 3 (1 row) chris=# select perltest(1); perltest ---------- 4 (1 row) Doing the right thing and initialzing variables before using them solves this. Should I worry about this? Are concurrent callings of this function protected from each other? Any info would be much appreciated. Cheers, Chris -- Christopher Murtagh Enterprise Systems Administrator ISR / Web Communications Group McGill University Montreal, Quebec Canada Tel.: (514) 398-3122 Fax: (514) 398-2017
On Wed, Nov 12, 2003 at 03:05:34PM -0500, Christopher Murtagh wrote: > CREATE or REPLACE FUNCTION perltest(integer) > returns integer as ' > $MyInt = $MyInt + 1; > return $MyInt; > ' language plperlu; Use a local variable: CREATE or REPLACE FUNCTION perltest(integer) returns integer as ' my $MyInt = $MyInt + 1; return $MyInt; ' language plperlu; -- Alvaro Herrera (<alvherre[a]dcc.uchile.cl>) "The problem with the future is that it keeps turning into the present" (Hobbes)
Christopher Murtagh <christopher.murtagh@mcgill.ca> writes: > Thanks to a lot of help on this list, I've managed to get my pl/perl > function working. However, I have an unexpected result. Here's a simple > way to reproduce this problem: > > CREATE or REPLACE FUNCTION perltest(integer) > returns integer as ' > $MyInt = $MyInt + 1; > return $MyInt; > ' language plperlu; There's a reason Perl has "my" variables. Use them. ;) -Doug
Or in other words... code to "strict" J Doug McNaught wrote: >Christopher Murtagh <christopher.murtagh@mcgill.ca> writes: > > > >> Thanks to a lot of help on this list, I've managed to get my pl/perl >>function working. However, I have an unexpected result. Here's a simple >>way to reproduce this problem: >> >>CREATE or REPLACE FUNCTION perltest(integer) >>returns integer as ' >> $MyInt = $MyInt + 1; >> return $MyInt; >>' language plperlu; >> >> > >There's a reason Perl has "my" variables. Use them. ;) > >-Doug > >---------------------------(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 > > -- Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC Postgresql support, programming shared hosting and dedicated hosting. +1-503-222-2783 - jd@commandprompt.com - http://www.commandprompt.com Editor-N-Chief - PostgreSQl.Org - http://www.postgresql.org
On Wed, 2003-11-12 at 15:44, Joshua D. Drake wrote: > Or in other words... code to "strict" :-) Yes, I do normally. I was simply running a test and came across this and it caught me by surprise. For the record, my pl/perl function is this: CREATE OR REPLACE FUNCTION htdig(text, text) RETURNS text[] AS ' my $SearchTerms = $_[0]; my $HtDigDB = $_[1]; my @Result = {}; my $Line = ''''; #open HTDIG, "/usr/local/htdig/bin/htsearch ''config=" . $HtDigDB . ";words=" . $SearchTerms . "'';matchesperpage=1000;"; open HTDIG, "/bin/cat /home/postgres/" . $SearchTerms . " |"; while(<HTDIG>) { $Line = $_; $Line =~ s/^h[^0-9]*//; chomp($Line); push @Result, $Line; } close HTDIG; return qq/{/ . (join qq/,/, @Result) . qq/}/; ' LANGUAGE plperlu; I started writing this in C and realized that this was going to be a couple hundred lines of code - compared to the 23 lines in Perl, including test lines. All this function needs to do is to take a pipe from the htsearch and grab integers in the URL that it returns. Then I'll write a pl/pgSQL wrapper that returns this array as a set. Pretty sweet, and I couldn't have done it without the help of this list. Thanks! Cheers, Chris -- Christopher Murtagh Enterprise Systems Administrator ISR / Web Communications Group McGill University Montreal, Quebec Canada Tel.: (514) 398-3122 Fax: (514) 398-2017