in reply to Class array, push a new element

I think you've got a bug there; are you sure that the object contains the tasks after your push? That syntax should be
sub addTask { my( $self, $task ) = @_; push @{$self->{_tasks}}, $task; return $task; }
For what you've written, the line my @taskArray = @{ $self->{_tasks} }; assigns the content of the array referenced in $self->{_tasks} to a new array @taskArray, and this new array is the one that gets the new elements.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Class array, push a new element
by Anonymous Monk on Sep 07, 2014 at 22:53 UTC
    I think you've got a bug there; are you sure that the object contains the tasks after your push?

    I think the line @{ $self->{_tasks} } = @taskArray; takes care of that?

    Anyway, your version is indeed the shorter & more efficient one.

    hatz, you may want to look at perlreftut and perlref.

      My eyes are broken. I should definitely not be working on a Sunday....

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Thank you for your answers!

        Indeed the syntax you wrote looks better :

        push @{$self->{_tasks}}, $task;

        It does not raise the warning anymore ("push on reference is experimental at Project.pm line 40").

        I will get more familiar with references and how to get an array, hash or string out of one.

        Thank you again!