Object-Oriented Perl
counter-test:
#! /usr/bin/perl -w
use strict;
use Counter;
my $a = new Counter;
my $b = new Counter(42);
print $a->next(), "\n";
print $a->next(), "\n";
print $b->next(), "\n";
print $b->next(), "\n";
print $a->next(), "\n";
print $a->next(), "\n";
print $b->next(), "\n";
print $b->next(), "\n";
Counter.pm:
package Counter;
use strict;
use warnings;
sub new {
my ($type, $value) = @_;
$value = 1 unless defined $value;
my $self = { Value => $value };
return bless $self, $type;
}
sub next {
my ($self) = @_;
return $self->{Value}++;
}
1; # success