Hi Ovid,
On Wed, Apr 07, 2010 at 12:04:08AM -0700, Ovid wrote:
> --- On Wed, 7/4/10, Lutz Gehlen <[email protected]> wrote:
> > What I need a central place for is the definition of the
> > actual
> > error messages. With my module Exception::EasyThrow, I can
> > write at
> > the beginning of my module:
> >
> > use Exception::EasyThrow
> > (var_ud => 'Variable %s is undefined. We
> > are doomed.',
> > ... => ...);
> >
> > and then later
> >
> > var_ud('foo') if(!defined($foo));
>
> Note, in your code above, you can still have different error
> messages in different packages. You've made things a touch easier,
> but not really solved the underlying problem.
I usually handle this by having a central module defining the error
functions and offering them for import.
> What about this proposal for Exception::Class?
>
> package My::Exceptions;
>
> use Exception::Class (
> 'MyException',
> 'IORead' => {
> isa => 'MyException',
> alias => 'throw_io_read',
> format => [ 'Cannot open %s for reading: %s' ],
> },
> );
>
> And then:
>
> use My::Exceptions 'throw_io_read';
>
> open my $fh, '<', $filename or throw_io_read $filename, $!;
I think that an inclusion into Exception::Class is definitely worth
thinking about. I even contacted Dave about it once, but I fear I
wasn't very clear about what I had in mind, so he suggested that I
publish my module first and he would consider it for
Exception::Class. Dave, what do you think about Ovid's suggestion?
One thing that always troubled me when I thought about a good way to
add my desired behaviour to Exception::Class and that is also an
issue with the 'format' suggestion is that I think that an own class
for each message might be a bit of an overkill. If I think of
exception hierarchies I rather think of a class like
My::Exception::IO (instead of IORead in your example). A read
failure would lead to an instance of the IO class with a message
about the read failure.
Therefore I went for a different way just for throwing the
exception. I don't claim to have made up my mind completely about
this. As mentioned before, I started this thread to raise a
discussion. But this is why I did not go your way so far.
Thanks for your comments,
Lutz