If not, then what is the point [of references]?

References are created so you can put ANYTHING in a scalar. This is very handy, as array elements, list elements and hash values can only be scalars. It's also handy to be able to point to an array when calling a sub, instead of passing the entire array.

Imagine you want a two-dimensional array - without references, it would not be possible: you can't have an array as an array element (it would be flattened in the array assignment). Fortunately, you can fit an array in a scalar using a reference, and that way you can have multi-dimensional array.

Imagine you have a machine with a gigabyte of available memory, and a single scalar that uses 600 megabytes of it. If you would pass it to a sub, and it would be copied, you'd run out of memory. Fortunately, by using a reference you can just point to it, and pass only a few bytes. The reference can be copied, but it will still point to the same data.

Please do read perlreftut and perlref. (Yeah, I know the perlreftut link is broken. Will someone PLEASE update the on-site documentation? Until then, use perldoc perlreftut)

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk


In reply to Re: Passing references to a sub. by Juerd
in thread Passing references to a sub. by DigitalKitty

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.