in reply to is this a perversion of OO principles?

Seems like a good idea to me, for what it is worth..

Using an inheritance trick, polymorphism, you could subclass each of the specific MessageLibrary objects so that instead of having different methods in one class, you have many classes with a common overloaded method:

my $file_error = Error::FileOpen->new(); open MYFILE, $myfile or die $file_error->handle($myfile);
But, as far as i can tell you with my current knowledge of such problems - all you are doing is moving code from one position to another, you aren't eliminating code.

I think i'd just rather use:

open MYFILE, $myfile or die $error_messages->generate_message( 'file_open_failed', $myfile );
because you don't need to worry about adding code to tailor an error - unless you really need that, don't worry about it. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

Replies are listed 'Best First'.
Re: (jeffa) Re: is this a perversion of OO principles?
by seattlejohn (Deacon) on Jan 07, 2002 at 03:00 UTC
    Thanks for the feedback... and just to clarify, when it comes to tailoring errors I am not actually adding code within the MessageLibrary class itself, but specifying it in the object constructors, something like this:

    my $error_messages = MessageLibrary->new({ file_open_error => sub{"Couldn't open $_[0]: $!"}, bad_token => sub{"Invalid token $_[0] encountered on line $_[1]"}, bad_file_format => "Invalid file format", }); my $status_messages = MessageLibrary->new({ starting_parse_phase => "Starting parser...", generating_results => sub {"Generating results for element $_[0]"}, });
    So basically the only stuff in the class package is just a constructor, an AUTOLOAD method, and a couple of little utility methods.

    Update: While rereading this response, I realized there was an aspect of jeffa's point about not having to tailor error messages that I only partially answered. Although you can define error messages in the object constructor, the object also provides default handling for any method you might call. So you could do this: print $status_messages->undefined_msg($a_param, $another); and get a reasonable result. (You can also override this default behavior in the object constructor.)