Category: Utility
Author/Contact Info coreolyn@bereth.com
Description: I originally posted this here and thought others starting in OO Perl might also find this little dev platform a useful tool in getting their feet wet. Rotate your object changes through the following benchmark script to make the necessary judgement calls on your changes to Objects implementation(s). I know there's much that could be improved, but this for me has been a great way to 'dive' into OO. Taking book examples and breaking them down has taken much longer than starting at the basics and working up.

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';
}