### In Shoe.pm
package Shoe;
use strict;
use warnings;
# Shoes with holes in their soles.
my $num_holey = 0;
sub new {
my $class = shift;
my $self = {};
# ...
bless $self, $class;
return $self;
}
# Instance method that happens to access a class variable.
sub wear_out_sole {
my $self = shift;
$num_holey++;
print "Sole on this shoe has been worn out.\n";
Shoe->how_many_holey();
}
# Class methods.
sub how_many_holey {
my $class = shift;
print "Currently, there are $num_holey holey shoes ($class).\n";
}
sub incr_num_holey {
my $class = shift;
$num_holey++;
}
1;
####
### In Boot.pm
package Boot;
use base qw( Shoe );
use strict;
use warnings;
# Boots are like Shoes, but have a heel.
# (Heel stuff not shown, for brevity.)
sub new {
my $class = shift;
my $self = $class->SUPER::new();
# ...
return $self;
}
# Instance method that has to deal with a class variable.
sub wear_out_sole {
my $self = shift;
Shoe->incr_num_holey();
print "Sole on this BOOT has been worn out.\n";
Shoe->how_many_holey();
}
1;
####
#!/usr/bin/env perl
use strict;
use warnings;
use Boot;
my $s = Shoe->new();
$s->wear_out_sole();
my $b = Boot->new();
$b->wear_out_sole();
my $b2 = Boot->new();
$b2->wear_out_sole();