Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How do I create static class variables (class data)?

by btrott (Parson)
on Apr 21, 2000 at 03:37 UTC ( #8299=categorized question: print w/replies, xml ) Need Help??
Contributed by btrott on Apr 21, 2000 at 03:37 UTC
Q&A  > object-oriented programming

Answer: How do I create static class variables (class data)?
contributed by btrott

Perl doesn't have a special syntax for creating such variables. All you have to do, basically, is declare a lexical variable at the start of the file in which you're declaring your class, like

package Foo; my $Bar = 0;
It's important to declare it with my rather than as a package global, because the latter could be accessed directly by scripts using your class--and you don't want that (well, most likely you don't).

You shouldn't just use this variable directly in your class, though, because your class won't be very inheritable. Subclasses that inherit your methods will be altering *your* class variable rather than their own; you don't want this.

To fix this, take a reference to the data and store it in your objects. For example, say that you have an object that's really a hash reference--to give it access to your class data, you could do this:

$self->{'_BAR'} = \$Bar;
in your constructor. When you want to access the class data, then, just use it like you would a regular reference:
print "\$Bar is currently ", ${ $self->{'_BAR'} };
And there you go! Your very own static member.

For more information and for some practical uses of such a variable, take a look at perltoot.

Answer: How do I create static class variables (class data)?
contributed by chromatic

The new keyword our (introduced in Perl 5.6) provides a package scope for variables. It is demonstrated in perltootc.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (7)
    As of 2020-09-29 06:17 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (146 votes). Check out past polls.