use strict;
use warnings;
package RandThing;
use overload '""' => '_asStr';
sub new {
my ($class, @values) = @_;
my $self = bless {values => \@values}, $class;
$self->genValue ();
return $self;
}
sub _asStr {
my ($self) = @_;
return $self->{value};
}
sub genValue {
my ($self) = @_;
$self->{value} = $self->{values}[rand @{$self->{values}}];
return $self->{value};
}
package RandDayPart;
use parent -norequire, 'RandThing';
sub new {
my ($class) = @_;
my @values = qw(morning evening afternoon night);
return $class->SUPER::new (@values);
}
package RandLight;
use parent -norequire, 'RandThing';
sub new {
my ($class) = @_;
my @values = qw(bright dark gloomy dazzling);
return $class->SUPER::new (@values);
}
package RandWeather;
use parent -norequire, 'RandThing';
sub new {
my ($class) = @_;
my @values = qw(stormy windy rainy calm);
return $class->SUPER::new (@values);
}
1;
####
use strict;
use warnings;
use RandomThings;
my $dp = RandDayPart->new ();
my $lt = RandLight->new ();
my $wt = RandWeather->new ();
my $tense = RandThing->new ('was', 'is', 'will be');
print "It $tense a $lt and $wt $dp.\n";
$dp->genValue ();
print "It $tense a $lt and $wt $dp.\n";
##
##
It was a bright and rainy morning.
It was a bright and rainy evening.