[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

RE: A common base exception object for Perl - RFC



Right.. 

I forgot to mention.. you said that 'exitcode' & 'text' were things that
already existed. Are they? Where are there? When I say 'die "foo"', where is
an exitcode coming from? Certainly 'text' does not have to exist -- thats
part of the point. Right now I am using ref-array exceptions in some code.
There is no textual representation of the exceptions. Consider:

	eval {
		die [ 1, 2 ];
	};
	if ( ... $@ ... ) {
		print STDERR lookup_text($@) if $debug;
		...
	}

lookup_text is a relatively slow operation. I am not planning on exiting on
these exceptions, and if I am not in debug mode, I dont want to lookup text
I wont use. I would like to be able to rewrite this as:

	package ME; # My Exception
	@ISA = qw(Exception);

	sub new { bless [$_[0], $_[1]] }
	sub to_string { lookup_text($_[0]) }

	package Foo;

	eval {
		die new ME(1,2);
	};
	if ( blessed $@ and $@->isa('ME') ) {
		print $@ if $debug;
		...
	}

Note that lookup_text is only invoked if $@ actually needs to be converted
to a string. Otherwise, it will just be 2 numbers & I'll process them
numerically. Also note that I dont have to actually create an 'Exception'
object in 'new' and add fields to it. If 'Exception' contains a 'text'
field, then I can be a "good object" without also defining that field.

The essential argument is:  If you think that objects should only be
accessed by implementation-independent methods, then you cant (in Perl) have
non-method attributes; because there is no implementation-independent way to
access them.


-----Original Message-----
From: Pete Jordan [mailto:pjordan1@email.mot.com]
Sent: Thursday, January 20, 2000 11:46 AM
To: Perl5 Porters
Subject: Re: A common base exception object for Perl - RFC


"Redford, John" wrote:

> With a minimal base class you gain two things. First, all exception
> objects can be subclasses of Exception, permitting:
>         $@->isa('Exception')
> as a common handler for any exception. Secondly, the author of an
> Exception subclass does not have to ever know about the '""' code.
> While simple looking, it would be a large distraction for people to
> have to read `perldoc overload` (or think they have to read it).

Agreed. The only question is how minimal is minimal? What, if any,
facilities are 99.9% certain to be useful to /all/ exception classes?

In any case, as long as the base class defines the C<to_string> method
and does the C<use overload>, subclasses just need to provide their own
C<to_string> method, yes?

Pete
-- 
use Disclaimer::Standard;	# Motorola GSM Software Factory
my $phone='+44 1793 564450';	# "'Not twisted,' Salzy once said of
my $fax='+44 1793 566918';	#  her own passion, 'it is helical.
my $mobile='+44 7973 725120';	#  That sounds better.'"


Follow-Ups from:
Pete Jordan <pjordan1@email.mot.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]