or, depending on your architecture, store the values temporarily somewhere, perhaps in an instance variable or similar. For example if you don't know/ care which of routines A & B get called first, something like this might be helpful (which does unfortunately use a lexical global):my ($name, $address, $other, $info, $here) = getinfo(); ... sub routine_A { ... print $name; } ...
That way you're only calling getinfo() once, and then only if needed. Really, your chosen solution will depend a lot on whether you have access to the internals of getinfo(), whether getinfo() can be memoized, and the architecture surrounding these few functions.my @vars; sub routine_A { ... if (!@vars) { @vars = getinfo() } my $name = $vars[0]; print $name; } sub routine_B { ... if (!@vars) { @vars = getinfo() } my $address = $vars[1]; print $address; }
Update: (still coming off the meds). Of course this sort of caching is what Memoize is for.. I wrote this under the impression that getinfo() might be called elsewhere in the code, not just directly in this subset of functions. Essentially though you'll want to cache the returned values somewhere outside of the routines.
In reply to Re: More efficient return of values from sub
by moot
in thread More efficient return of values from sub
by bradcathey
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |