Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: More efficient return of values from sub

by Limbic~Region (Chancellor)
on Mar 27, 2005 at 17:26 UTC ( [id://442652]=note: print w/replies, xml ) Need Help??


in reply to More efficient return of values from sub

bradcathey,
Your getinfo example doesn't reflect what expensive operations you are trying to avoid nor have you indicated what level of visibility (scope) you need. To summarize the examples provided so far:
  • Memoization: The sub is only invoked once per unique set of arguments while subsequent invocations hit cache
  • Raise Scope: The return values are elevated in scope so that they can be seen by all subs in the package. This can be simplified by storing in a hash
  • Convert Return Values To Hash: This is the solution from fglock that you appear to like. This only simplifies the retrieval process, you are still building the hash with whatever expensive operations you have each time it is called
  • Hashref, Singleton, Object: These are all variations on item 2. Depending on implementation you can defer expensive operation until requested

Without knowing more information, I would make a few assumptions and not have a getinfo() sub at all.

#package declaration if applicable my %info = ( name => expensive_op1(), address => expensive_op2(), ); # later on in the code sub routine_A { print $info{name}; }

Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-24 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found