473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Nov 12 '05 #1
4 2258
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.