I do suppose the OP is more interested in sub refs, but hey. Since everyone else wants to talk about refs in general...

The thing I like about references is that they can be used consistently throughout.

You cannot build a list of lists, you can only build a list of ARRAYrefs. Same for associative arrays. Might as well start with an ARRAYref of ARRAYrefs, and avoid the confusion of inconsistent referencing based on context.

I've tried to adopt a coding style of using refs everywhere, and dereferencing where necessary. Ok, maybe not so much for subs or globs, but definitely for arrays and hashes. I typcially name my reference vars by the type of structure they represent for clarity (list/hash). This way anyone maintaining my code can see that everything is done consistently. No need to change syntax when passing stuff around, or refactoring a structure, etc. Too many bugs I've seen came from inconsistent dereferencing, since Perl is perfectly happy treating refs, arrays, and hashes all as scalars. No syntax error here, just crap code. Sorry.

Sometimes its hard to resist the pull of non-refs... It's just so convenient sometimes that builtins like sort/map/split return structures. Initializing references from the return of those builtins is just awkward stylistically/syntactically. Oh well, that's consistent too at least.

--Dave


In reply to Re: reference , advantages? by armstd
in thread reference , advantages? by dbs

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.