package Point; use strict; use warnings; # コンストラクタは便宜的に new という名前 # my $point = Point->new(x=>2, y=>3); sub new { my $class = shift(); my $self = { x => 0, y => 0, @_ }; return bless($self, $class); } # $point->x とすれば x の値が取れる # $point->x(0) とすれば x の値を 0 にできる sub x { my $self = shift(); if (@_) { $self->{x} = $_[0]; } else { return $self->{x}; } } # $point->y とすれば y の値が取れる # $point->y(0) とすれば y の値を 0 にできる sub y { my $self = shift(); if (@_) { $self->{y} = $_[0]; } else { return $self->{y}; } } # 値をクリアして 0 にする # $point->clear(); sub clear { my $self = shift(); $self->x(0); $self->y(0); } # モジュールは最後にこれを付ける 1;