I don't quite see the point in having a global function instead of a global variable. To me it makes more sense to have a global variable since that needs to be declared. If you have a subroutine that you think you can remove you won't find out that it perhaps was needed until the program dies because the subroutine was missing in some obscure corner of the program. However, you'll instantly know if the variable is missing. Furthermore, if you use our() you'll get a warning if you don't use the variable. Example:
sub make_handle { ... } our $handle = make_handle(); # "... used only once"
So I consider globals a good thing when they're proper to use. One can argue that using a variable like this (and still keep the 'used only once' warning) will require you to always setup the handle. But if it's a large application and the handle will almost always be used then I see no reason not to initialize it right away.

I also believe it's faster to use a variable than to perform a sub call. But if you still want to use a sub call and really really care about performance, you might want to try a hack like this:
sub foo { my $foo = shift; return undef unless defined $foo; no warnings 'redefine'; *foo = sub { $foo }; foo(); } { my $bar; sub bar { defined $bar ? $bar : ($bar = shift); } }
&foo and &bar are analogous. But &foo runs faster, at least according my benchmark.

Cheers,
-Anomo

In reply to Re: Avoiding global object handle by Anonymous Monk
in thread Avoiding global object handle by Biker

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.