I've got a module that I'm working on where I am pre-caching a number of very large constants, created with Math::BigFloat. My problem is this: I've got 10 or 12 of these very large constants, but for the most part, only one or two of them are going to used at any given time. That being the case, I don't want to automatically dump all of them into memory, only the ones that a user asks for. For example, I've got the square root of 2 and the square root of 5 ($_sqrt2_ and $_sqrt5_, respectively) as two of these constants. Let's say I only need $_sqrt2_. What would be the best method for creating these via Math::BigFloat "on demand?" My first thought was to make a CONSTANT() routine where the user would pass the name of the constant they want and have it created at that time. It seems to me, though, that there should be a better way. I did some digging through the search here, but didn't find anything (perhaps I was searching for the wrong thing?). Any suggestions?

Many thanks.
___________________
Kurt

UPDATE (11-Feb-2002 10:21:38 AM): By way of further clarification, the problem isn't that I'm concerned about the calculation time of these constants. I'm pre-calculating them and I'm subsequently hard-coding them as Math::BigFloat objects. My concern is a memory issue since some of these constants are very, very large. I suppose a few extra kb of memory probably isn't going to make much of a difference, but it is The Principle of The Thing. My original plan was to use an accessor method similar to that described by hossman to avoid dumping all of these constants into memory if only one or two (or possibly even none) are going to be used in a given script. Forgive me for showing my ignorance here, but does the Tie::Scalar method that japhy used accomplish this? I've never used Tie::Scalar before, so I'm a little in the dark, even after reading the docs.

In reply to Pre-caching large constants by sifukurt

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.