Thread: exception handling in plperlu

exception handling in plperlu

From
"Jasbinder Singh Bali"
Date:
Hi,
Actually, if I rephrase my requirement, I need to catch an exception at any point ,where ever it is raised, in the perl code.
E.g during an insert, there is a foreign key contraint violation, then i need to catch this specific error and do something with it.
Hope i make some sense here.
 
Thanks,
Jas

Re: exception handling in plperlu

From
Tom Lane
Date:
"Jasbinder Singh Bali" <jsbali@gmail.com> writes:
> Actually, if I rephrase my requirement, I need to catch an exception at any
> point ,where ever it is raised, in the perl code.
> E.g during an insert, there is a foreign key contraint violation, then i
> need to catch this specific error and do something with it.

Since PG 8.0, you should be able to trap exceptions with eval{}, same as
you would do in any Perl code.

            regards, tom lane

Re: exception handling in plperlu

From
"Jasbinder Singh Bali"
Date:
How about using a try catch block?
isn't that more efficient that eval?
 
~Jas

 
On 3/15/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
"Jasbinder Singh Bali" <jsbali@gmail.com> writes:
> Actually, if I rephrase my requirement, I need to catch an exception at any
> point ,where ever it is raised, in the perl code.
> E.g during an insert, there is a foreign key contraint violation, then i
> need to catch this specific error and do something with it.

Since PG 8.0, you should be able to trap exceptions with eval{}, same as
you would do in any Perl code.

                       regards, tom lane

Re: exception handling in plperlu

From
Martijn van Oosterhout
Date:
On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:
> How about using a try catch block?
> isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: exception handling in plperlu

From
"Jasbinder Singh Bali"
Date:
just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120; port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh is used>

Jas

On 3/16/07, Martijn van Oosterhout < kleptog@svana.org> wrote:
On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:
> How about using a try catch block?
> isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout   < kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFF+oqkIB7bNG8LQkwRApyEAJ413cmp0bIuasEceWhXgTgSE6NOFACeNijH
ZHknBQrtHqg30xL8Wh219Ik=
=Xhhm
-----END PGP SIGNATURE-----


Re: exception handling in plperlu

From
"A.M."
Date:

On Mar 16, 2007, at 10:54 , Jasbinder Singh Bali wrote:

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120; port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh is used>


There is a mailing list for DBD::Pg:

But your problem in not related to the driver, rather it's a Perl issue. Your variable is defined within the block, so its scope is the eval block ("my" implies lexical scoping). If you define it outside the block and assign inside the block, then it will work.

Cheers,
M

Re: exception handling in plperlu

From
"Adam Rich"
Date:
You'll want something like this:
 
my $dbh = eval { DBI->connect(....);};
 
 
-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Jasbinder Singh Bali
Sent: Friday, March 16, 2007 9:55 AM
To: Martijn van Oosterhout; Tom Lane; pgsql-general@postgresql.org
Subject: Re: [GENERAL] exception handling in plperlu

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120; port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh is used>

Jas

On 3/16/07, Martijn van Oosterhout < kleptog@svana.org> wrote:
On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:
> How about using a try catch block?
> isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout   < kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFF+oqkIB7bNG8LQkwRApyEAJ413cmp0bIuasEceWhXgTgSE6NOFACeNijH
ZHknBQrtHqg30xL8Wh219Ik=
=Xhhm
-----END PGP SIGNATURE-----


Re: exception handling in plperlu

From
Martijn van Oosterhout
Date:
On Fri, Mar 16, 2007 at 10:54:49AM -0400, Jasbinder Singh Bali wrote:
> just wondeng why doesn't it let me put
> my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120;
> port=5432;", "", "");
> in eval
>
> says
> Global symbol "$dbh" requires explicit package name at line <where ever dbh
> is used>

Well, eval starts a new scope so the $dbh is only defined within that
block. From the errors I assume you tried the other statements outside
that scope?

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: exception handling in plperlu

From
Douglas McNaught
Date:
"Jasbinder Singh Bali" <jsbali@gmail.com> writes:

> just wondeng why doesn't it let me put
> my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120; port=5432;",
> "", "");
> in eval
>
> says
> Global symbol "$dbh" requires explicit package name at line <where ever dbh is
> used>

The my() variable goes out of scope when the eval {} is done.  Declare
the variable outside the eval {}:

my $dbh;

eval { $dbh = connect(...) };

eval { $dbh->prepare(...:) };

-Doug

Re: exception handling in plperlu

From
merlyn@stonehenge.com (Randal L. Schwartz)
Date:
>>>>> "Douglas" == Douglas McNaught <doug@mcnaught.org> writes:

Douglas> my $dbh;

Douglas> eval { $dbh = connect(...) };

Since eval returns its value (or undef if $@ has the error), you can shorten
this to:

my $dbh = eval { DBI->connect(...) };

and now either look at $@ (if you want to distinguish exception vs normal) or
just defined $dbh (if you want to know if you have a useful handle or not).

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Re: exception handling in plperlu

From
"hubert depesz lubaczewski"
Date:
On 3/16/07, Jasbinder Singh Bali <jsbali@gmail.com> wrote:
> just wondeng why doesn't it let me put
> my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask;
> host=192.168.0.120; port=5432;", "", "");
> in eval

you dont need to put ->connect in eval.
just set connect option 'raiseerror' to 0 (and printerror as well). in
this situation it will simply set errorcode. no eval{} required.

depesz