hsweet has asked for the wisdom of the Perl Monks concerning the following question:

Monks;

Thanks for the help on my last question Adventure Game ->object. I've figured out how to oop-a-fy my once procedural script

Except for one bit. Each object this class creates needs to keep track of a list of related things.. therefore the anonymous array, $self->{'things'} = ;

How do you put things into and display the array via a method?

sub new{ my ($class,$player ) = @_; my $self = {}; bless $self, $class; $self->{'things'} = [ ]; return $self; }

Time flies like an arrow, fruit flies like banannas

Replies are listed 'Best First'.
Re: Filling object arrays
by djantzen (Priest) on May 20, 2003 at 01:34 UTC

    You just have to dereference the array in your method body and you can do normal array operations like shift, pop, etc.

    sub addThings { my ($self, @new_things) = @_; push @{$self->{things}}, @new_things; } sub getThings { my ($self) = @_; # If the caller expects a list, then dereference the array # before returning it, otherwise just return the reference. return wantarray ? @{$self->{things}} : $self->{things}; }
    Of course you can use embedded hashes too if you can guarantee uniqueness of keys.


    "The dead do not recognize context" -- Kai, Lexx