blessを使ったオブジェクト指向 - 基本クラスの宣言package Foo; (略) 1; コンストラクタとインスタンスの生成 sub new {
my $class = shift;
bless {@_}, $class;
}
#!/usr/bin/perl
use Foo;
$foo = Foo->new(name => 'taro', age => 18)
print $foo->{name}; # "taro"
デストラクタ sub DESTROY {
my $self = shift;
$self->{fh}->close;
}
継承package Foo::Bar; use parent 'Foo'; (略) 1; 最近はuse baseじゃなくて、use parent推奨らしい。 コンストラクタで親クラスのコンストラクタを呼ぶ sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{foo} = 'var';
return $self;
}
|
|