Your solution should work. Its biggest drawback is that every object created clears Perl's cache of method lookups. Also it leaks memory - create a thousand objects and Perl now has a thousand classes that never go away.

Memoizing would solve both of those problems. Matching sub refs isn't that hard. The following code will turn your hash into a string suitable for memoizing.

use Scalar::Util qw(refaddr); sub overload_string_specification { my $overload = shift; join " ", map { ($_=>refaddr($overload->{$_})) } sort keys %$overload; }
Another solution is to overload all of the methods you may wish to be able to overload with methods that dynamically figure out the right function to call. If you only have a small number of methods that you'll be overloading this isn't a bad route. But if you could potentially wind up overloading all of the possible things in overload, then doing this basically means re-creating all of the dispatch logic in overload in your code. Which you may not want to do.

In reply to Re: Overloading different instances differently. by tilly
in thread Overloading different instances differently. by kyle

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.