Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Class array, push a new element

by hatz (Initiate)
on Sep 07, 2014 at 22:40 UTC ( [id://1099828]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: Class array, push a new element
by kennethk (Abbot) on Sep 07, 2014 at 22:44 UTC
    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1099828]
Approved by thomas895
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found