in reply to OO Baggage

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