in reply to Re: HOA with array slice?
in thread HOA with array slice?
This is a fairly basic implementation, you can get as fancy as you'd like.package Fruit; sub new { my $class = shift; my $self = {}; my $color = shift || ''; my $name = shift || ''; bless $self, $class; $self->color( $color ); $self->name( $name ); return $self; } sub color { my $self = shift; $self->{color} = $_[0] if $_[0]; return $self->{color}; } sub name { my $self = shift; $self->{name} = $_[0] if $_[0]; return $self->{name}; } sub print { my $self = shift; print "I am just a ", $self->color(), " ", $self->name(), "\n"; } 1; package main; my $first_fruits = Fruit->new( 'green', 'apple' ); my $second_fruits = Fruit->new( 'purple', 'plum' ); my @fruit_basket = ( $first_fruits, $second_fruits ); $second_fruits->print();
|
|---|