By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

pl/perl function life and variable scope - concurrency problem?

P: n/a

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

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2

P: n/a
Christopher Murtagh <ch*****************@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

Nov 12 '05 #3

P: n/a
Or in other words... code to "strict"

J
Doug McNaught wrote:
Christopher Murtagh <ch*****************@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

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4

P: n/a
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

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.