Hello everybody!
I work with perl from time to time, and I thought that I could give a try to object programming with a small project I'm on for a few days.
My question is about the push function when the array is part of an object.
Here is my object:
sub new {
my $class = shift;
my $self = {
_description => shift,
_priority => shift,
_type => shift,
_tasks => []
};
bless $self, $class;
return $self;
}
I tried to add a method to add an element to the array "_tasks". I tried this:
sub addTask {
my( $self, $task ) = @_;
push($self->{_tasks}, $task);
return $task;
}
but when I run the program it warns me that "push on reference is experimental at Project.pm line 40"
In order to avoid that, I tried the following approach:
sub addTask {
my( $self, $task ) = @_;
my @taskArray = @{ $self->{_tasks} };
push @taskArray, $task;
@{ $self->{_tasks} } = @taskArray;
return $task;
}
I got this after a lot of tests (with Dumpers to see what my varaiables looked like), but I'd like to know if this is the right way to do it.
I don't want to start with bad methodology when building my methods.
Thank you