SPUG: Perl and OO and Unit Testing

dancerboy dancerboy at strangelight.com
Mon Jul 22 13:54:56 CDT 2002

At 4:58 pm -0700 2002-07-21, Asim Jalis wrote:
>I have recently been playing with Perl again. I
>lay the blame for this on Damian's talk.
>For some reason writing unit tests in Perl feels
>really weird. I feel like Perl code is simply not
>supposed to be OO in the same way as Python and
>Java are. OO at the same fine granularity seems
>wrong in Perl somehow.
>Whenever I start writing Perl with tiny trivial
>objects -- the way Java and Python make you do --
>I feel like I am being stupid and un-Perl-ish.
>This is how I feel when I use any of the OO
>techniques in Damian's book.

I can totally relate:  I feel the same vague un-ease when writing 
truly OO Perl as well.  My suggestion:  ignore the discomfort and 
create tiny, Java-esque classes anyway.

>Part of it is that Perl is built around this idea
>that you want to work on giant trees of data
>containing hashes/arrays of hashes/arrays many
>levels deep.

Keep in mind that in Perl, objects *are* references to data 
structures, so when you create objects whose data members are objects 
whose data members are objects... etc. you are, in fact, creating 
hashes/arrays of hashes/arrays many levels deep -- only you're 
"hiding" those trees (encapsulating them) behind the OO interface.

This is a Good Thing:  instead of having to write procedural code to 
traverse those trees yourself, you can write OO code that ignores the 
underlying tree structure, and let the Perl interpreter traverse the 
tree for you.

(This just made me think of a nice metaphor:  in Perl, OOP lets you 
see the forest instead of the trees.)

Also, remember that no language is perfect (no, not even Perl!  ;-) 
Sometimes you have to "fight" the language a little bit to do things 
right:  so just because something is "un-Perlish" doesn't necessarily 
mean that it's wrong.  Personally, I consider Java to be the closest 
thing to a "perfect" computer language yet invented, with Perl being 
a close second.  So whenever I can "twist" Perl to behave more like 
Java, I do so.  It may not be "Perl-ish", but so what?  I consider 
Java-esque Perl to be *better* than Perl-ish Perl.


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list