可以多看看cpan上面的code就会了,老的模块的编写可以写成pm文件,简单的例子如下
package test;
sub new {
my $self = shift;
bless {}, $self;
}
sub get {
my $self = shift;
my $key = shift;
if (exists $self->{$key}) {
return $self->{$key};
}
}
sub set {
my $self = shift;
my ($key, $value) = @_;
$self->{$key} = $value;
}
sub inc {
my $self = shift;
my $key = shift;
if (exists $self->{$key}) {
$self->{$key}++;
}
}
sub dump {
my $self = shift;
for my $k(sort keys %$self) {
print "$k = $self->{$k}\n";
}
}
;
my $t = test->new();
$t->set('key1', 1);
$t->set('key2', 100);
print $t->dump;
$t->inc('key2');
print $t->dump;