A monk on a similar quest to my own. After all the reading on Perl OO I could get my hands on, (which went far to understanding the terminology and concepts), I found when I sat down to play, much of the information made the issue far more complex than necessary to learning and utilizing Perl OO code.

Perl OO cannot be compared to Java as essentially you control the object, not a VM. This is much more fun but frought with hazards. I wish someone had provided the following code about two weeks ago, as Perl OO is much easier to understand and use from a minimalist approach rather than trying to grok all the issues at once.

Use the following bench to rotate your code changes through Tiny.pm and NotSoTiny.pm. Every keystroke change becomes immesurably more important in OO and by focusing on the effects of each keystroke your basic Perl understanding and your grokking of Perl OO will grow with it.

In the end the what where and how becomes mute, and it becomes just a variation of coding in Perl.

package Tiny; # Filename Tiny.pm # The Worlds Smallest Perl Object use strict; sub new { return bless { }, shift; } 1; ------------------------------------------------------- package NotSoTiny; # Filename NotTiny.pm use strict; sub new { return bless { value => 'A value' }, shift; } sub get_value { return shift->{value}; } 1; ------------------------------------------------------ #! /usr/bin/perl -w #Filname: bench use Benchmark qw(timethese); # perl 5.6 has 'cmpthese' # which is very cool, and # much more informative # If you have 5.6 just # s/timethese/cmpthese/g use strict; use lib ('./'); use Tiny; use NotSoTiny; my ( $roll, @roll ); my $loop = 250000; # You will want this var up here to # Adjust itineratations to avoid # warnings ###### Preload Objs ################################### # Benchmark two ways one with pre constructed objs and # another without considering construction costs. my $TinyObjBase = Tiny->new(); my $NotTinyObj = NotSoTiny->new(); ###### SubRoutine Vars ################################# # To be completely fair do the same with the Vars for # the subroutines. my $TinySub; my $NotSoTinySub; ###### End Init ########### system("clear"); my $loops; for $loops ($loop) { timethese $loops, { ##### Subroutine Comparisons TinySubPreVar => sub { $TinySub = Tiny(); }, TinySubNewVar => sub { my $NewTinySub = Tiny(); }, NotSoTinySubPreVar => sub { $NotSoTinySub = NotSoTinySub(); }, NotSoTinySubNewVar => sub { my $NewNotSoTinySub = NotSoTinySub(); }, ##### Object Comparisons TinyObjReused => sub { $TinyObjBase; }, TinyObjNew => sub { my $NewTinyObj = Tiny->new(); }, NotSoTinyObjReused => sub { $TinySub = $NotTinyObj->get_value(); }, NotSoTinyObjNew => sub { my $NotSoTinyObjNew = NotSoTiny->new(); $TinySub = $NotSoTinyObjNew->get_value(); }, }; } sub Tiny { return; } sub NotSoTinySub { return 'A value'; }

coreolyn


In reply to Re: OO Baggage by coreolyn
in thread OO Baggage by jynx

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.