Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Class array, push a new element

by kennethk (Abbot)
on Sep 07, 2014 at 22:44 UTC ( [id://1099829]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found