Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: reference , advantages?

by armstd (Friar)
on Jul 01, 2011 at 19:01 UTC ( #912399=note: print w/replies, xml ) Need Help??

in reply to reference , advantages?

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://912399]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-05-20 21:54 GMT
Find Nodes?
    Voting Booth?