Sunday, March 25, 2007

クラスとメソッドを一度に呼ぶサンプル Perl

---- Game.pm ------
package Game;

use strict;
use Carp qw(croak);

sub new{
my $class = shift;
my $name = shift || croak ('set name');
bless{
name => $name,
},$class;
}

sub name{
my $self = shift;
$self->{name};
}


1;

__END__

---- Player.pm -----
package Player;

use strict;
use Carp qw(croak);
use Game;

sub new{
my $class = shift;
my $self = {};
bless $self, $class;
}

sub run{
my $self = shift;
print $self->game->name;

}

sub game{
my $self = shift;
unless (defined $self->{game}){
$self->{game}= Game->new('race');
}
return $self->{game};
}

1;

__END__



---- index.cgi------
#!/usr/bin/perl
use Player;
use strict;

Player->new->run;

No comments: