Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the item on subsequent calls. If I were using VB, I would declare static variables init along with other variables to hold the data, load the data and set init True on first call, and bypass the data load with an If init <> true then xxxx endif portion of the function on subsequent calls.

But variables listed within the {} of a Module don't persist from call to call. My workaround is to give the data an absurdly long variable name and exclude it from the the Module[{var1,var2,...} list, and use ValueQ to check if longvariablename has been evaluated. For example:

getPce[date_,column_] := Module[{dates, value, b, e},
If[! ValueQ[longvariablename], longvariablename = Import["path\\file];
value = SomeFunction[date_,column_]; (* get item(s) from longvariablename *)
]


This is a clumsy. I'm sure there is a nice Mathematica way to do this, but I've never been able to figure it out.

-

You just have to wrap the Module around those variables, to make them semi-local:

Module[{persistent},
persistent := persistent = Import["path\\file"];
SomeFunction[date_,column_]:=
(body using persistent);
getPce[date_,column_] :=
Module[{dates, value, b, e},
value = SomeFunction[...];
]
]


Here we basically used Module to create a persistent variable, a method I mentioned here and described in more detail in my third post in this Mathgroup thread. We also used memoization to remember the value in subsequent calls. Another example of a very similar use of memoization is in definePartAPI function from this answer, with the only difference that I used there Unique to create the persistent symbol, instead of Module.

-
I'll try this, and thank you. But, would you say you are using memoization to make up for a scoping problem within MMa? Or is that just the wrong way to think about the problem? (I haven't read your links yet) –  George Wolfe Oct 6 '12 at 16:44
@GeorgeWolfe No,these are 2 components of the solution: scoping serves to create a persistent variable, and memoization serves to remember the value for subsequent calls. They are independent. –  Leonid Shifrin Oct 6 '12 at 16:47