hatz has asked for the wisdom of the Perl Monks concerning the following question:
I tried to add a method to add an element to the array "_tasks". I tried this:sub new { my $class = shift; my $self = { _description => shift, _priority => shift, _type => shift, _tasks => [] }; bless $self, $class; return $self; }
but when I run the program it warns me that "push on reference is experimental at Project.pm line 40"sub addTask { my( $self, $task ) = @_; push($self->{_tasks}, $task); return $task; }
In order to avoid that, I tried the following approach:
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.sub addTask { my( $self, $task ) = @_; my @taskArray = @{ $self->{_tasks} }; push @taskArray, $task; @{ $self->{_tasks} } = @taskArray; return $task; }
Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class array, push a new element
by kennethk (Abbot) on Sep 07, 2014 at 22:44 UTC | |
by Anonymous Monk on Sep 07, 2014 at 22:53 UTC | |
by kennethk (Abbot) on Sep 07, 2014 at 23:07 UTC | |
by hatz (Initiate) on Sep 08, 2014 at 20:59 UTC |