Data::Thunk is the most transparent and reliable way to do this that I know of.
However, I am not a big fan of this or any other similar modules or methods that try to hide themselves from the user. I prefer something more explicit, for example, having code that uses a value that is difficult to compute, just calls a function to retrieve it. This way, you donβt need to pre-comprehend your value, your intentions are more clearly visible, and you can also have various options to avoid recalculating the value, for example, lexical closures, perl state variables, or Memoize modules.
rafl source share