Steve Bertrand <st...@ibctech.ca> wrote:
> I've got a relatively decent understanding of how references work in
> Perl (syntax-wise, especially when/how to de-ref), but I'd now like to
> ask when to use them.
> 
> Obviously memory allocation isn't an issue anymore, so when exactly
> should refs be used, and in what circumstances shouldn't they be used?

If you want to pass more than one array or hash to a subroutine, using 
references is the only way to do it.

If you prefer using your own variables in subroutines instead of only using @_ 
to access parameters, you'll quickly learn that it's more efficient to pass 
long strings or large arrays/hashes as a reference since this circumvents the 
need to copy the data.

On the other hand, passing arguments as references means that subroutines can 
have side effects and that means that your code will be harder to maintain.

And if you get down to nitpicking, you'll have to keep in mind that 
dereferencing a value on access takes time, too.

HTH,
THomas


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to