Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: More efficient return of values from sub

by moot (Chaplain)
on Mar 26, 2005 at 02:36 UTC ( [id://442445]=note: print w/replies, xml ) Need Help??


in reply to More efficient return of values from sub

..and if Memoize won't work in your particular instance (returning values from a database, for example), you could perhaps call getinfo() outside of your routines A & B:
my ($name, $address, $other, $info, $here) = getinfo(); ... sub routine_A { ... print $name; } ...
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 @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; }
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.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://442445]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found