Getter and setter methods suck.

Who write getter and setter methods in Perl? Oh right, those guys from languages with less flexible OO models...

sub property { my $self=shift; if (@_) { $self->{property}=shift; return $self; } else { return $self->{property}; } }

Even if they're autogenerated, they still suck because we can't interpolate them inside a string.

print "Property: @{[$obj->property]}\n";

Course it is one char more than using stright concatenation.

If you do go the tie overload route (not _such_ a bizarre course, but one I think ultimately youll reject as a Neat Idea But....), then you should carefully read the example code at the bottom of the overload documentation. It contains a full implementation of a two face tie. The trick is that for it to work you need to keep your underlying object representation at least one step away from the one you plan to expose. For instance your object could be a reference to a reference to a hash. Then you could overload the hash dereference or array dereference as you choose. Of course this will slow your program down and it will introduce nasty horrible bugs in various versions of perl that will make the tricks dangerous enough that I doubt its worth your while. *shrug*.

In case its not clear, I'm _fully_ with tachyon on this one.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re: bless + tie by demerphq
in thread bless + tie by cbraga

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.