You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Matthew Pressly <mp...@claborn.net> on 2002/01/08 23:06:31 UTC
Problem with exception handler in guide?
I am trying to get the exception class described in the guide to work, but am having trouble with "die" returning the class incorrectly.
The example in the guide was:
die My::Exception->RetCode(code => 204);
The module code is at:
http://thingy.kcilink.com/modperlguide/perl/The_My_Exception_class_in_its_e.html
with two modifications (the last "die" in sub AUTOLOAD was changed to CORE::die to prevent a perl warning message about it being ambiguous, and the missing semicolon at the end of the first line "package ..." was added).
The following script code does not work
#-------------------------------------
use My::Exception;
eval {
die My::Exception->Return(code => "abc");
};
if ($@) {
use Data::Dumper;
print Dumper($@);
}
#-------------------------------------
It generates the output:
#-------------------------------------
$VAR1 = bless( {
'text' => 'My::Exception',
'caller' => {
'line' => 19,
'filename' => 'My/Exception.pm',
'package' => 'My::Exception'
}
}, 'My::Exception::UnCaught' );
#-------------------------------------
with the class indicating that the exception was not caught. Tracing it in the debugger shows that it executes My::Exception::die using "My::Exception" as the first argument.
If I put parens around the argument to die, as follows, it works (calls AUTOLOAD first then returns result of that as first argument to My::Exception::die), returning the correctly classed object.
Code:
#-------------------------------------
use My::Exception;
eval {
die (My::Exception->Return(code => "abc"));
};
if ($@) {
use Data::Dumper;
print Dumper($@);
}
#-------------------------------------
Output:
#-------------------------------------
$VAR1 = bless( {
'caller' => {
'line' => 5,
'filename' => './exceptions2',
'package' => 'main'
},
'code' => 'abc'
}, 'My::Exception::Return' );
#-------------------------------------
It appears that "->" is too low a precedence. Is there a way around this without requiring that parentheses be used around die's arguments? I'm running this under perl5.6.0. Here is output of perl -V:
Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
Platform:
osname=linux, osvers=2.4.0, archname=i586-linux
uname='linux manson 2.4.0 #1 wed aug 2 20:22:26 gmt 2000 i686 unknown '
config_args='-ds -e -Dprefix=/usr -Di_db -Di_dbm -Di_ndbm -Di_gdbm'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
useperlio=undef d_sfio=undef uselargefiles=define
use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
Compiler:
cc='cc', optimize='-O2 -pipe', gccversion=2.95.2 19991024 (release)
cppflags='-fno-strict-aliasing -I/usr/local/include'
ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
stdchar='char', d_stdstdio=define, usevfork=false
intsize=4, longsize=4, ptrsize=4, doublesize=8
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=4, usemymalloc=n, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lc -lcrypt
libc=, so=so, useshrplib=false, libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: USE_LARGE_FILES
Built under linux
Compiled at Jan 19 2001 05:42:10
%ENV:
PERL5LIB="/home/mpressly/development/library"
@INC:
/home/mpressly/development/library
/usr/lib/perl5/5.6.0/i586-linux
/usr/lib/perl5/5.6.0
/usr/lib/perl5/site_perl/5.6.0/i586-linux
/usr/lib/perl5/site_perl/5.6.0
/usr/lib/perl5/site_perl
.
Matthew Pressly
Re: Problem with exception handler in guide?
Posted by "Aaron E. Ross" <ro...@coreference.com>.
> die (My::Exception->Return(code => "abc"));
$ perl -I/tmp -MMy::Exception -e 'die "My::Exception"->RetCode( code => 204 )'
$ My::Exception::RetCode=HASH(0x8185570)
not much better, but no parens. :)
i don't really understand the precedence problem though.
Aaron