I'm trying to create a variable that exists in package space that is persistent without an instantiation of any objects. I'd like to accomplish something like a public static variable.

Many of us who worked our way through the Camel book had a hard time coming to terms with the current standard advice about package variables:

don't use them.

See e.g. Dominus's excellent article Coping with Scoping.

You can still have your public static variable:

use strict; my $public_static_variable;

Write subroutines in the same file to grant read and write access to this global public static variable:

package Bar; sub get_public_static_value { return $public_static_variable; } sub set_public_static_value { my ($new_value, $options_ref) = @_; # validate the new value # respond to the options # log the modification $public_static_variable = $new_val; }

Now your client code in other files can invoke Bar::get_public_static_value() and Bar::set_public_static_value($foo), but you get the spell-checking benefits of use strict and the serenity of knowing where to focus your debugging in case the static variable acquires an unexpected value.


In reply to Re: Accessibility of package-level variables by Narveson
in thread Accessibility of package-level variables by sirrobert

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.