Consider the following:

use Benchmark; my $foo = "bar"; my $test = new tester(); my ($buffer, $i); my $a1 = new Benchmark; for( $i=1; $i<1_000_000; $i++ ) { $buffer = $foo; } my $a2 = new Benchmark; my $b1 = new Benchmark; for( $i=1; $i<1_000_000; $i++ ) { $buffer = $test->report(); } my $b2 = new Benchmark; my $diff1 = timediff($a1, $a2); my $diff2 = timediff($b1, $b2); print "Variable copying: " . timestr($diff1) . "\n"; print "Object copying: " . timestr($diff2) . "\n"; package tester; sub new { my $class = shift; my $self = { 'foo' => "bar" }; bless $self, $class; return $self; } sub report { my $self = shift; return $self->{'foo'}; }

Running this gives (on a 1ghz pc hobbled by Win2k):

Variable copying: -1 wallclock secs (-0.81 usr + 0.00 sys = -0.81 CPU +) Object copying: -5 wallclock secs (-5.15 usr + 0.00 sys = -5.15 CPU)

Considering there are a million copies made of each the time difference isnt all that great. However, if all we wanted to do was code fast programs none of us would ever write OO :)

In other words, use a variable if calling the object is too much work :)

However there are many other reasons to code this way... have a look at This file and especially this file for some basic guidelines and explanation of OO programming.


In reply to Re: AppConfig variables and OO by Caillte
in thread AppConfig variables and OO by BoredByPolitics

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.