agreed -- who cares. this in no way, shape, or form will noticably affect any sort of perl code.

I just brought this topic up, because I wanted to understand exactly why the speed difference existed. all i really care for is being able to stash & access class variables. I find using subs/constants is much easier than using 'our' scoped package variables for stashing info -- its just a quick $self->name instead of inspecting the object and moving on from there

In any event, I did the following benchmark below to test the speed difference between the various options in writing & calling these methods

Results vary on each run, but fall into 2 general categories...

faster:

sub get() { 'a'; } use constant get_constant=> 'a';

slower:

sub get { 'a'; } sub get { return 'a'; } sub get() { return 'a'; }

and the code...
package class; use strict; sub new { my ( $proto )= @_; my $class= ref($proto) || $proto; my $self= bless ( {} , $class ); return $self; } sub get_sub { 'a'; } sub get_sub_nullproto () { 'a'; } sub get_sub_return { return 'a'; } sub get_sub_return_nullproto () { return 'a'; } use constant get_constant=> 'a'; package main; use strict; use Benchmark qw[ cmpthese ]; my $object= class->new(); cmpthese -1, { get_sub => sub { my $a= $object->get_sub; }, get_sub__paren => sub { my $a= $object->get_sub(); }, get_sub_nullproto => sub { my $a= $object->get_sub_nullproto; }, get_sub_nullproto__paren => sub { my $a= $object->get_sub_nullprot +o(); }, get_sub_return => sub { my $a= $object->get_sub_return; }, get_sub_return__paren => sub { my $a= $object->get_sub_return(); } +, get_sub_return_nullproto => sub { my $a= $object->get_sub_return_n +ullproto; }, get_sub_return_nullproto__paren => sub { my $a= $object->get_sub_r +eturn_nullproto(); }, get_constant => sub { my $a= $object->get_constant; }, get_constant__paren => sub { my $a= $object->get_constant(); }, };


In reply to Re^2: OOP - Constant Vs. Subroutine () by 2xlp
in thread OOP - Constant Vs. Subroutine by 2xlp

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.