What are references

Every variable needs a memory address.

C has pointers to handle that type of data. It has also pointer arithmetic.

A reference is a safer abstraction of pointers, no dangerous arithmetics, no change after memory reorganization.

What's different in Perl

In contrast to most languages does Perl have explicit reference operators and reference data type (sub type of scalars)

@a is the list form of an array. (That's also important for list context.)

$a=\@a is the reference type of that array. But both variables "refer" to the identical array! \ is just an operator to expose the embedded reference.

Semantic/syntactic differences

@b=@a does a list copy of the content , hence $b[0]=42 won't affect $a[0] . Both variables (used to) have the same content, but at different memory locations.

$b=$a=\@b copies the array "object" itself, i.e. $b "points" to the same "address" Hence $b->[0]=42 means $a->[0]==42

Comparison to other languages

Arrays, Hashes etc in JS, Python and Ruby are effectively glorified references.

And array[0] there is implicitly de-referencing, where you need an explicit -> in Perl for $array_ref->[0]

And when passing an array to a sub func(array) there you risk a modification at a distance there, while func(scalar) is only copying the content.

That JS code is equivalent to func($array_ref) in Perl, in contrast to calling func(@array) which is passing a flattened LIST of elements.

All of this applies equally to %hashes as well, just that flattening happens to a list of (key, value) pairs.

Conclusion

Perl is explicit and consistent here, whenever you don't copy references you are copying the content.

With the added cost of more operators and syntax needed.

But try to do achieve something simple like a copy of content @a = @b in JS!

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

UPDATES

expanded with headlines


In reply to Re^9: Preparing data for Template (what are references) by LanX
in thread Preparing data for Template by Bod

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.