How would you handle this without OO? Would you assign to an array on the left, like this?:
@colour = ('yellow', 'red', 'purple');
If so, you can still do that using a reference.
@{ $this->{colour} } = ('yellow', 'red', 'purple');
Then you'd return $this->{colour} just like before, and it'd be an array reference just like in the other solutions.

You can therefore think of the same thing two different ways. The other examples show the assignment of a reference to an anonymous array. The above might be said to be assigning to the array to which the scalar $this->{colour} refers. The ordering of the thoughts is different and the syntax is different. Yet the result is the same.

When I first started messing with references years ago, the version with @{ $scalar } on the left seemed simpler to grasp. That notation does not scale as nicely to more complex data structures as using the anonymous reference notations on the right of the assignment operator, though.

I personally find the array ref on the right as gwadej shows and Fletch mentions to be easier to read and less cluttered. You should definitely get used to stating things in that form, even if the one I presented helps you make the transition.


In reply to Re: array as attributes in OO perl by mr_mischief
in thread array as attributes in OO perl by IL_MARO

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.