I've created a class that allows individual objects to have their own, unique, methods. I've got a good reason (I think) for doing this, but I still can't help but wonder if it's really the nifty trick I think it is, or just a Really Bad Idea. Here's the rationale that led me to this approach -- comments appreciated:

I was thinking that it might simplify some of my larger apps if I had a way to centralize some of the message strings in one place, the idea being that it's perhaps easier to manage the text in a single location rather than have to look at literals scattered across code in a bunch of different modules.

My first thought was to do this using plain old hashes, along these lines:
open MYFILE, $myfile or die $error_messages{file_open_failed};

But this has a couple of big shortcomings, one of them being that you can't easily do any interpolation on the spot. I decided that what I really wanted was to generate error messages on demand using anonymous subroutines, so I built a simple class that would let me create an object along these lines:

my $error_messages = new MessageLibrary({ file_open_failed => sub {"couldn't open file $_[0]: $!"} }); ... open MYFILE, $myfile or die $error_messages->generate_message('file_op +en_failed', $myfile);

This does the job, but I found the calling syntax a bit inelegant. I decided that I would really like to be able to write things this way:
open MYFILE, $myfile or die $error_messages->file_open_failed($myfile);

That just seems more intuitively readable to me. This turned out to be pretty easy to implement by using an AUTOLOAD method to grab the method name and use it as a key to look up the right subroutine in the hash that was passed to the constructor.

But here's where I come up against the philosophical question. I can easily imagine having a few different MessageLibrary objects in my app: one for error messages, one for debug messages, one for progress/status messages. Yet each of them would have different "methods", even though they're all objects of the same class. So my question to all interested monks is, does this seem like a ridiculous perversion of object-oriented principles? I admit that I'm basically using the arrow-notation-method-call syntax as a convenient form of syntactic sugar (I'm a big believer in writing *readable* code)... so I thought it would be wise to get some outside opinions from informed Monks before I forged ahead with this approach.

BTW, the obvious performance impact of this implementation is not a concern.


In reply to is this a perversion of OO principles? by seattlejohn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.