are the variables i declared in the class Farm stored as references?

Yes, or at least, they should be (except if the value is a scalar already)... If an array is nested in another data structure (the hash $self here), it always needs to be via a reference (in Perl).

will something like my @birds = $self->{farmBirds}; successfully make a copy of the farmBirds array in the @birds array?

No, to make a copy you'd need to write

my @birds = @{ $self->{farmBirds} };

The @{ ... } dereferences the "farmBirds" attribute value, which is presumed to be an arrayref.

Here's how your code might look like when fixed:

___ Farm.pm ___

package Farm; use strict; use warnings; sub new { my $className = shift; my $self = { farmBirds => [], # initialisation not strictly required, # but good for documentation purposes... # ... }; bless $self, $className; return $self; } sub addFarmBirds { my $self = shift; push @{ $self->{farmBirds} }, @_; } sub getFarmBirds { my $self = shift; return @{ $self->{farmBirds} }; } 1;

___ some script that uses the class ___

#!/usr/bin/perl use strict; use warnings; use Farm; my $farm = Farm->new(); $farm->addFarmBirds( qw(X Y Z) ); my @birds = $farm->getFarmBirds(); print "@birds\n";

I chose to add an accessor getFarmBirds, which returns the list of farm birds that you can then copy into an array — i.e. the values will be duplicated in this case.  Having accessors is generally recommended, as it helps keep the object-internal data structures encapsulated, the advantage being that if you later need to modify them, you won't need to change every piece of code that's using the class as well... Put differently, the accessor provides a defined API that the user of the class can rely on — the internal representation of the data becomes irrelevant (e.g. you might later want to store the actual values in a database, or some such...).

(BTW, as Moose has been mentioned, I should point out that this sample is using nothing but the "conventional" Perl 5 object model. This is not meant to say, however, that looking into Moose wouldn't be a good idea...)


In reply to Re: Help!!!! POOP is confusing me! by almut
in thread Help!!!! POOP is confusing me! by bittis

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.