# # Triangle.pm -- 三角形クラス(オブジェクトを生成する) # # sample file name: Triangle.pm.005 package Triangle; sub new { my ($class, $a, $b, $c) = @_; my $self = { a => $a, b => $b, c => $c }; bless $self, $class; return $self; } sub space { my ($self) = @_; warn "You are about to calculate the space of ", ref($self), "!!! \n"; my ($a, $b, $c) = ($self->{a}, $self->{b}, $self->{c}); my $s = ($a + $b + $c) / 2; my $space = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c)); return $space; } 1;