Error.pm and DBI.pm problem

H

Horst Walter

As a newbie to Perl I run into the following problem. With this I get
the following problem with the code below:

Can't call method "with" without a package or object reference at
autodb.pm line 163

I have tried "use Error qw:)try);" and "use Error". I am confused
since this try/catch block works in other parts of my code pretty
fine. It seems to have something to do with DBI.

Any hints would be appreciated.
Thanks!


try {
my ($dbh) = @_;
unless ($dbh->disconnect) {
die("autodb::eek:racleLogoff $DBI::errstr");
};
}
catch Error with {
my $err = shift;
print "autodb::eek:racleLogoff(..) $err";
};
 
R

Rhesa Rozendaal

Horst said:
As a newbie to Perl I run into the following problem. With this I get
the following problem with the code below:

Can't call method "with" without a package or object reference at
autodb.pm line 163

I have tried "use Error qw:)try);" and "use Error". I am confused
since this try/catch block works in other parts of my code pretty
fine. It seems to have something to do with DBI.

It has to do with the way you raise exceptions within the try block. You "die" with an error message, but "catch" is expecting an object of class Error.
Any hints would be appreciated.
Thanks!


try {
my ($dbh) = @_;
unless ($dbh->disconnect) {

die("autodb::eek:racleLogoff $DBI::errstr");

change to
Error->Throw("autodb::eek:racleLogoff $DBI::errstr");
 
H

Horst Walter

Thanks a lot for your hint.
However, if I change it to

sub oracleLogoff ($) {
my ($dbh) = @_;
if (!defined $dbh) {
return;
}

# avoid any trouble during close
try {
$dbh->disconnect;
} catch Error with {
# ignore the error
};
}


I still get the error
Error: Can't call method "with" without a package or object reference
at autodb.pm line 181. ("catch Error with") line

Regards
HW
 
A

Anno Siegel

Horst Walter said:
As a newbie to Perl I run into the following problem. With this I get
the following problem with the code below:

Can't call method "with" without a package or object reference at
autodb.pm line 163

[...]

Your code (still quoted below) is incomplete. It looks like you didn't
import the with() function.

Anno
 
R

Rhesa Rozendaal

Horst said:
Thanks a lot for your hint.
However, if I change it to

# avoid any trouble during close
try {
$dbh->disconnect;
} catch Error with {
# ignore the error
};
}

Again, that's because DBI raises the exception with die().
You could change the try block to a more generic one:

try {
$dbh->disconnect;
} otherwise {
# .. handle error
};
 
H

Horst Walter

Yes, this is what I have guessed. But I tried many things and was not
able to do it.

I used
use Error qw:)try);

as in all the examples for error.pm. I have tried several tings as
use Error qw:)try with);

but none of them worked.
It would be great if somebody had an example on this.

Cheers
HW


Horst Walter said:
As a newbie to Perl I run into the following problem. With this I get
the following problem with the code below:

Can't call method "with" without a package or object reference at
autodb.pm line 163

[...]

Your code (still quoted below) is incomplete. It looks like you didn't
import the with() function.

Anno
try {
my ($dbh) = @_;
unless ($dbh->disconnect) {
die("autodb::eek:racleLogoff $DBI::errstr");
};
}
catch Error with {
my $err = shift;
print "autodb::eek:racleLogoff(..) $err";
};
 
P

Peter Scott

Yes, this is what I have guessed. But I tried many things and was not
able to do it.

I used
use Error qw:)try);

as in all the examples for error.pm. I have tried several tings as
use Error qw:)try with);

but none of them worked.
It would be great if somebody had an example on this.

Post the shortest *complete program* you can construct that
demonstrates the problem please. I have never had problems
with Error.pm and DBI and cannot reproduce your problem.
 
H

Horst Walter

sub oracleLogoff ($) {
my ($dbh) = @_;
if (!defined $dbh) {
return;
}

# avoid any trouble during close
try {
$dbh->disconnect;
} catch Error with {
# ignore the error
};
}


Error
Error: Can't call method "with" without a package or object reference
at autodb.pm line 181. ("catch Error with") line
 
A

Anno Siegel

Don't top-post. I have moved your reply where it belongs, after what
you are replying to,
(e-mail address removed) (Peter Scott) wrote in message
sub oracleLogoff ($) {
my ($dbh) = @_;
if (!defined $dbh) {
return;
}

# avoid any trouble during close
try {
$dbh->disconnect;
} catch Error with {
# ignore the error
};
}


Error
Error: Can't call method "with" without a package or object reference
at autodb.pm line 181. ("catch Error with") line

That is *not* a complete program. A complete program is one that can
be copied and pasted into a file and run. In particular, yours doesn't
show how you load the Errors module.

Have you read the documentation of Errors.pm? Especially what it says
about an export tag named ":try"?

Anno
 
H

Horst Walter

Here is a complete one you can run. I read all I found about error.pm,
I am only not certain whether I have to include other use statements
like qw:)catch).

Error is still
Error: Can't call method "with" without a package or object reference

BTW, thanks to all for your time and efforts
-----------

use strict;
use warnings;
use Error qw:)try);
use DBI;

# global

my $db = "XXXX";
my $password = "XXXXX";
my $username = "XXXXX";
my $oracle_sid = $db;
my $dbh;

try {

unless ( $dbh = DBI->connect("dbi:Oracle:$oracle_sid", "$username",
$password, {AutoCommit => 0}) ) {
print STDERR ("autodb::eek:racleLogin login Failed as
$username\/$password\@$oracle_sid");
}
unless ($dbh->disconnect) {
print STDERR "autodb::eek:racleLogoff $DBI::errstr";
}
# force an error
my $n = 0;
print 0/$n;
} catch Error with {
my $err = shift;
print $err;
}
 
R

Rhesa Rozendaal

Horst said:
Here is a complete one you can run. I read all I found about error.pm,
I am only not certain whether I have to include other use statements
like qw:)catch).

Error is still
Error: Can't call method "with" without a package or object reference

Importing :try should be sufficient. The manual clearly states: "Error exports subroutines to perform exception handling. These will be exported if the :try tag is used in the use line."

I tried your test script, and it worked fine. Have you tried to use the "otherwise" function yet?
What version of Error are you using?

Can you try this script?

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use Error qw/:try/;

try {
this_dies();
}
catch Error with {
print Dumper(shift());
};

sub this_dies {
die "Sorry...";
}

__END__
 
P

Peter Scott

Here is a complete one you can run. I read all I found about error.pm,
I am only not certain whether I have to include other use statements
like qw:)catch).

No, you don't.
Error is still
Error: Can't call method "with" without a package or object reference

I ran the below with changes for db account and got the expected
"Illegal division by zero at..."

You have to set ORACLE_HOME, but presumably you do that outside
the script.

I tried Perl 5.6.1 and 5.004 with the same results.

All I can think is that you have a bad DBI.pm, bad DBD::Oracle.pm,
or bad Error.pm. Try creating a brand new perl on a different
computer altogether, installing the latest versions of those
modules, and point your script back at your Oracle server.
 
H

Horst Walter

Thanks a lot Peter & Rhesa for your support. I will try both of your
hints on Friday, since I am attending a conference right now.

Best regards
HW
 
H

Horst Walter

I have setup another Perl system and tested it there. As with your
envs. it is working fine there.

So I guess the problems are due to the libs on the SUN I have
originally used. When I finally figure out what the problem with the
setup is, I will post it here.

Thanks to all who have helped me so far.
Best Regards
HW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top