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


In reply to Class array, push a new element by hatz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.