Kyle Burton on Mon, 15 May 2000 14:50:17 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [Chaos] OO Perl Exceptions (fwd)


Ok.  I was referred to this list by 2 of the members of another list (who I 
also think are members of PLUG).  Regardless, I've been a perl developer for 
a little while, and have done some moderatly large projects using Perl.

I've got a background with C and C++, and have found that over and over
again, when I talk to non-perl people about the language, I keep hearing
myself say things like:

"Yes, you can do network programming in Perl."
"Yes, you can do Object Oritented programming in Perl."
"Yes, you can do Refactoring (XP - Martin Fowler) in Perl."

Well, I've done all these things in other languages and in Perl.  One thing
I've been working on lately is taking a design for an application server
work-alike and building it into a generic re-useable peice of software so
other groups can use it.  You can get this [Pas - Perl Application Server]
at: 

  http://www.bgw.org/projects/pas2/

Right now, I'm using it where I work to build a prototype website.  We used 
an older generation of the same design to do a large ecommerce site, and 
had alot of success [good performance, fast development] with it.  We used 
Apache/mod_perl and HTML::Embperl against Oracle.

I'm trying to make the software/code look more and more OO as I go along,
trying to use good programming techniques, yadda yadda yadda.

We used Perl's exception handling (die, and if $@) in the original 
application, but it wasn't very good at recovery - only catching and 
reporting that there was an error -- and it worked very well.  Having
come from a C++ background, being able to identify types of exceptions
to implement recovery was alot easier when it is an object instead of
just a string (or other basic type).  I also work with alot of Java
developers who I just taught a Perl class to -- I want them to be able
to look at the Perl code and be able to grok as much of what's going on
as possible.

Ok, whith all that background, here's the original series of replies that
I sent out to the other lists...


Thanks for hearing me out.
Kyle R. Burton



---------- Forwarded message ----------
Date: Mon, 15 May 2000 14:16:55 -0400 (EDT)
From: Kyle Burton <mortis@voicenet.com>
Reply-To: chaos@neverlight.com
To: chaos@neverlight.com
Subject: Re: [Chaos] OO Perl Exceptions

Thanks, I just subscribed to that list...I'm going to lurk for a while
before I start posting stuff like this.  

I've already implemented this stuff, and decided on this syntax for now:

try {
  my $base = new Pas::Base;
  throw new Pas::Exception('Test Exception.');
};

catch(qw(Pas::Exception e)) and eval {
  print 'caught: ',$e,"\n";
  print 'caught: ',$e->error(),"\n";
  print $e->stacktrace(),"\n";
};

catch() and eval {
  print 'caught unknown: ',$@,"\n";
}

Not quite as clean as what I was shooting for, but it works out to be
close enough...


k

------------------------------------------------------------------------------
The nice thing about Windows is - It does not just crash, it displays a dialog
box and lets you press 'OK' first. 
    -- (Arno Schaefer's .sig) 
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------

On Mon, 15 May 2000, Nicolai Rosen wrote:

> Why don't you post it to philly perl mongers? Or if you don't want to
> subscribe (it's really only a few messages a week), I can post it for you.
> 
> On Mon, 15 May 2000, Kyle Burton wrote:
> > Like other languages (C++, and Java), developers are used to the convienience
> > of exception handlers.  I know from Perl's fast and looseness, that we
> > can't enforce everything at compile time, but...I've been developing
> > largish applications in Perl for a little while now, and have used Perl's
> > exception handling (eval is try, die is throw, and if($@) is catch) and
> > found it to be quite serviceable -- after having used C++'s exception
> > handling system for quite some time now.
> > 
> > In the development of the the architecture of Pas (the application server
> > work-alike) that I've been building, I decided to create a method called
> > throw() on one the base class, so things now can look like this:
> > 
> > unless($condition_to_continue_life) {
> >   $self->throw("Error, can't continue life\n");
> > }
> > 
> > which is just ducky.  Looks clean, and inside of throw(), the base class
> > uses perl's built-in caller() function to build a stack trace, discover
> > who (which module/package) threw the exception, and even at what line number.
> > 
> > Most excellent.
> > 
> > My mind wandered this weekend, and I thought about how the utility of this
> > was nice to have, would extension of this concept be a good thing?  Should
> > I look into bringing this closer to the exception handling of the other
> > languages?
> > 
> > I toyed with the idea of using exception objects this morning, and as I
> > suspected, you can assign an object to $@, eg:
> > 
> >   die new Foo::Exception("You're not going to beleive this, but...");
> > 
> > and when I did the traditional if($@), it had the object in it.  So,
> > to all the perl developers on this list (and beyond if you feel like 
> > bringing it up to other people), is this a good way of doing things?
> > Should I pursue this path or leave things as they are?
> > 
> > I think it'd be nice and possibly even useful feature to have.  Being able
> > to key off of the types of exceptions could end up making Perl software
> > good at doing error recovery.  I imagined code like this:
> > 
> > ...
> > unless(open FILE, '</etc/passwd') {
> >   $self->throw(new Pas::FileException('Error opening file: ',$!,"\n"));
> > }
> > ...
> > 
> > ...
> > eval {
> >   $foo->crack_passwords();
> > };
> > 
> > if($self->catch(Pas::FileException)) {
> >   print 'Caught FileException: ',$self->exception()->as_string(),"\n";
> >   exit 0;
> > }
> > $self->rethrow();  # throw if uncaught...
> > 
> > # equivalent of catch all...
> > # if($@) {   
> > #   die $@;
> > # }
> > 
> > 
> > what's the opinion of this group?  Is this a symantic waste, or am I
> > on to somthing here?
> > 
> > k
> > 
> > ------------------------------------------------------------------------------
> > The nice thing about Windows is - It does not just crash, it displays a dialog
> > box and lets you press 'OK' first. 
> >     -- (Arno Schaefer's .sig) 
> > mortis@voicenet.com                            http://www.voicenet.com/~mortis
> > ------------------------------------------------------------------------------
> > 
> 
> Nicolai Rosen
> nick@netaxs.com
> Earthstation/Netaxs
> http://laktar.dyndns.org/
> http://www.netaxs.com/~nick/
> 
> 
> 




**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**