Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
..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.


In reply to Re: More efficient return of values from sub by moot
in thread More efficient return of values from sub by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found