Fantastic! A technique to avoid using global variables, but while still keeping all the problems associated with global variables!

The problem with using global variables is not an irrational fear of the our keyword. The problem is that it introduces global state. This means that if you write an app that, say, operates a blog, and has a database handle as part of its global state, it becomes very difficult to reuse any of that code when you want to write an app that manages multiple blogs (across multiple databases). All your functions are picking up the same database handle from some global place, so you can't tell one function that it needs to copy an article from blog A, and another function that the same article needs to be pasted into blog B.

Global state makes testing very difficult. If function do_something isn't passed a copy of $dbh as an argument, but instead picks it up from global state, then it becomes difficult to test do_something by passing it test/dummy database handles.

So the enemy is not global variables. The enemy is global state. Global variables are a manifestation of global state, but so are the techniques you suggest above.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re^2: Best practices passing database handles, cgi objects, etc. by tobyink
in thread Best practices passing database handles, cgi objects, etc. by xtpu2

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.