TheDamian discusses this in his book,
Object Oriented Perl. In a nutshell,
Perl has no equivalent to C++'s
static (or
Java's for that matter), but you can achieve roughly the
same effect with properly scoped lexical variables:
use strict;
my @foo;
push @foo, Foo->new() for (0..4);
package Foo;
{
my $count = 0;
sub get_count { return $count }
sub inc_count { return ++$count }
}
sub new {
my $class = shift;
$class->inc_count();
print STDERR 'there are ', $class->get_count(), " instances\n";
my $self = {
bar => 42
};
return bless $self, $class;
}
UPDATE:
Very good point
perrin. Try adding the following code
snippet in
perrin's
example and my example
after the for loop line to see the difference in action:
print $Foo::count, "\n";
The methods
get_count() and
inc_count()
are still accessible to the client in my code, by the way.
That is not necessarily a good thing ...
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.