On Monday 26 Apr 2010 17:16:27 Tim Bowden wrote: > I've just realised I almost never use named arrays or hashes anymore. > It's almost always anonymous references instead. That lead me to wonder > what criteria experienced perl hackers have as to when to use a named > array or hash, and when to start with an anonymous ref instead. My very > informal criteria tends to be to use an anonymous ref from the start if > I'm going to be passing it to a sub, and a named array or hash > otherwise. I've found the former to be much more common. Thoughts? >
Well, if you have an array declared with "my @array;" you can always pass it to a function or return it using "\...@array". Likewise for a hash. I sometimes declare variables as bona-fide lexical arrays or hashes like that to avoid the unnecessary dereferencing (e.g: "$array_ref->[$idx]" vs. "$array[$idx]", "@{$array_ref}" vs. "@array", etc.). and because I aim to make the scope of most lexical variables as small as possible because long methods and functions are a "code smell" according to the Refactoring book: http://perl.org.il/books/0201485672.html When the references are part of a larger expression (such as values of array indices or hash values, or constant arguments to functions) I naturally use the anonymous reference syntax. Hope it helps. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Stop Using MSIE - http://www.shlomifish.org/no-ie/ God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/