Keep It Simple, Stupid | |
PerlMonks |
Re: references best practiceby kyle (Abbot) |
on Apr 25, 2008 at 12:23 UTC ( [id://682803]=note: print w/replies, xml ) | Need Help?? |
I'd write it the first (simple) way or the third way, not the second. First way (my @stuff = stuff_generator()): This is the way I'd write it if I'm sure that I'll never need to pass out any "out of band" data. If it's always going to be just the array, guaranteed, this is straight forwardly easy to understand. Second way (stuff_inserter(\@stuff)): I generally want a sub not to modify the things I pass into it, so I'll avoid that kind of solution if there's another one handy. It does sometimes make sense to do this, but I don't think this situation does. Third way (my $stuff_ref = stuff_generator()): I might do this if the resulting array is going to be passed around later as a reference anyway. Rather than write \@stuff some places and $stuff_ref other places, I can just keep it as a reference all the time. I've heard some folks say that they like to use references all the time because the sigils are confusing, or it offers some kind of consistency. I'm not in that camp, but it's something else to think about. Bear in mind that if your array is huge, there could be a performance consideration to copying it or just passing a reference around. I suggest you write it the way that's clearest until a profiler tells you that it's a problem.
In Section
Seekers of Perl Wisdom
|
|