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.

In reply to Re: Avoiding Globals with OO Perl by BrowserUk
in thread Avoiding Globals with OO Perl by Anonymous Monk

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.