perlreftut and perlref are the best places to start learning about references. A reference can be thought of as a pointer to an array or hash (and a few other datatypes too, but we'll stick with arrays and hashes for now). You can have multiple references (pointers) to the same hash or array, allowing them all to see the same data. Here's a quick example:

use strict; use warnings; use feature 'say'; my @arr = (); my $ref1 = \@arr; my $ref2 = $ref1; push @arr, "Hello"; # To access an array or hash via the reference # we need to use a pointy arrow... say $ref1->[0]; # says "Hello" say $ref2->[0]; # says "Hello" again # We use an @{...} to "dereference" an array ref. # That is, to treat it like an array. push @{$ref1}, "World"; say $arr[1]; # says "World" say $ref2->[1]; # says "World" again # The dereferencing syntax can be interpolated into # strings. say "@{$ref2}"; # says "Hello World" my %h = (); # You can't store an array in a hash because hash # values are always scalars. $h{foo} = @arr; # an array in scalar context # evaluates to its length. # This stores "2" in the hash. # But you can store a reference, because a reference # is a hash. $h{bar} = $ref1; # Let's print that "Hello" string again, via the hash. say $h{bar}->[0]; # says "Hello" # Now let's take a reference to that hash. my $ref3 = \%h; # We can also print "Hello" via the hash reference say $ref3->{bar}->[0]; # says "Hello" # When you've got a big chain of pointy arrows to # navigate through references, you only need the # first one. say $ref3->{bar}[0]; # says "Hello" # Here's something a little advanced. We'll # assign a reference to a localized glob. our %h2; local *h2 = $ref3; # Now %h2 (which is a real hash, not a reference to # a hash) is an ALIAS for %h. say $h2{foo}; # says "2" delete $h2{foo}; # also deletes $h{foo}.
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re^3: Array / Hash Confusion! by tobyink
in thread Array / Hash Confusion! by andy503

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.