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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Avoiding Globals with OO Perl
by chromatic (Archbishop) on Oct 20, 2011 at 21:15 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2011 at 22:27 UTC | |
by chromatic (Archbishop) on Oct 20, 2011 at 23:30 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2011 at 00:06 UTC | |
by chromatic (Archbishop) on Oct 21, 2011 at 01:35 UTC | |
|