Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Another object idiom: this one, people tell me is aweful. I like it a lot because it overcomes something I hate about Perl: how object instance data requires lots of funny syntax. This syntax makes it tedious to refactor code from a subroutine into a method. Like Abagail's, it uses lexicals to good effect. It requires some glue, but class fields are then normal lexical variables. The object is a blessed hash - but instead of data, the hash holds the methods. The AUTOLOAD glue just dispatches to the correct hash element. The glue sets $self, so it is not necessary to read it off of the argument list manually, should it be needed (for a method call, for example).
sub new { my $ERA; my $Strikeouts; bless { get_ERA => sub { $ERA }, set_ERA => sub { $ERA = shift }, get_Strikeouts => sub { $Strikeouts }, set_Strikeouts => sub { $Strikeouts = shift }, }, shift(); } sub AUTOLOAD { (my $method) = $AUTOLOAD =~ m/::(.*)$/; return if $method eq 'DESTROY'; our $this = shift; if(! exists $this->{$method}) { my $super = "SUPER::$method"; return $this->$super(@_); } $this->{$method}->(@_); } 1;
Primary drawbacks are a small speed penalty - but for very small data classes like this, I tend to use rather ordinary blessed arrays, anyway, or psuedohashes, or "use fields" pragmatic module (all about the same thing right now - psuedohashes will be going away I'm told). Code inheritance works - however, fields declared lexically like this aren't inherited. All data is essentially private. I find this just as well - it forces a subclass to use its parents accessors. Finally, you have to be somewhat accustomed to Functional Programming to be used to staring at indenting like that. This is just a brief restatement of the second half of Anonymous Subroutine Objects at PerlDesignPatterns.com. I treat the AUTOLOAD footer as a Wrapper Module, and just use it into my namespace.

In reply to Re: Re: Where/When is OO useful? by scrottie
in thread Where/When is OO useful? by jarich

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (12)
    As of 2014-04-18 14:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (469 votes), past polls