如何编写简单的Perl模块
答案:1 悬赏:30 手机版
解决时间 2021-04-04 12:45
- 提问者网友:藍了天白赴美
- 2021-04-03 20:19
如何编写简单的Perl模块
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-04-03 21:14
可以多看看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}
";
}
}
;
my $t = test->new();
$t->set('key1', 1);
$t->set('key2', 100);
print $t->dump;
$t->inc('key2');
print $t->dump;
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}
";
}
}
;
my $t = test->new();
$t->set('key1', 1);
$t->set('key2', 100);
print $t->dump;
$t->inc('key2');
print $t->dump;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |