- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- Perl/オブジェクト指向/bless(基本) へ行く。
blessを使ったPerlオブジェクト指向
クラスの宣言
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 base 'Foo'; (略) 1;
コンストラクタで親クラスのコンストラクタを呼ぶ
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{foo} = 'var';
return $self;
}