Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.

--Dave


Comment on Re: reference , advantages?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://912399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (16)
As of 2014-12-18 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls