in reply to Avoiding Globals with OO Perl

Want to avoid using globals at all costs.

Besides that "at all costs" is stupidly emotive and dogmatic, why?

What benefits do you hope to accrue, or dangers do you hope to avoid, that you are prepared to expend "all costs" to achieve?

With respect to your sample code, all you've done is trade an simple, efficient and obvious global string that you can pass to your subroutines and methods, for a complicated, slow and obfuscated global object that you pass, along with a global constant used to select the actual value from within the object.

All in all, on the basis of your description, code and use-case, a completely futile exercise of creating unnecessary complexity and obfuscation in the name of some unachieved dogmatic ideal.

You asked for opinions, and that is mine. Other's MMV.

One of the "all costs":

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub new { my $class = shift; my $self = { _id => shift, _value=> shift, }; bless $self, $class; return $self; } sub setter { my ( $self, $set_name, $data ) = @_; $self->{"_$set_name"} = $data if defined($data); return $self->{"_$set_name"}; } sub getter { my ( $self, $get_name ) = @_; return $self->{"_$get_name"}; } sub test { $_ eq 'fred' } my $string; my $obj = main->new( '1', 'STORAGE' ); cmpthese -1, { a=> sub { for ( 1 .. 1000 ) { $obj->setter( 'string', "value$_" ); test( $obj->getter('string') ); } }, b=> sub { for( 1 .. 1000 ) { $string = "string$_"; test( $string ); } }, }; __END__ C:\test>junk44 Rate a b a 287/s -- -88% b 2437/s 748% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Avoiding Globals with OO Perl
by chromatic (Archbishop) on Oct 20, 2011 at 21:15 UTC
    One of the "all costs":

    What does that benchmark prove? In the unlikely case that an accessor is a bottleneck, consider hoisting an invariant out of a loop?


    Improve your skills with Modern Perl: the free book.

      There is no invariant in the loop.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I assumed you were trying to demonstrate something interesting about real code, not a made up example which exaggerates the effect of a perceived problem.


        Improve your skills with Modern Perl: the free book.