Kozz has asked for the wisdom of the Perl Monks concerning the following question:
I've become very used to putting nearly all my code in subroutines, based on the principal that "If you use the code more than once, it belongs in a subroutine." However, now I am for the first time reading about how passing & returning large values to/from subroutines can be 'computationally expensive'. The solution suggests that it's better to pass & return references instead. But if a scalar/array/hash is created in the subroutine, I have no choice but to return that item itself, rather than a reference since (assuming the variable is created with 'my') the variable will be destroyed upon exiting the subroutine. Is this correct?
In addition, is it always better to use references whenever possible to reduce CPU use if there's a possibility that the values returned could be large?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Can I safely return a reference from a subroutine?
by btrott (Parson) on Jun 21, 2000 at 02:13 UTC | |
Re: Can I safely return a reference from a subroutine?
by stewbasic (Initiate) on Feb 10, 2011 at 03:29 UTC |