This is an implementation of thunks a la Scalar::Defer, but uses Data::Swap and assignment to $_[0] in order to leave a minimal trace of the thunk.

In the case that a reference is returned from lazy { }Data::Swap can replace the thunk ref with the result ref, so all the references that pointed to the thunk are now pointing to the result (at the same address).

If a simple value is returned then the thunk is swapped with a simple scalar container, which will assign the value to $_[0] on each overloaded use.

Furthermore this module uses UNIVERSAL::ref instead of blessing to 0. Blessing to 0 pretends that everything is a non ref (ref($thunk) returns the name of the package, which evaluates as false), so deferred values that become objects don't appear to be as such.

If the class attribute is provided then isa and can will work as class methods without vivifying the object.

Any other attributes in %attrs will be used to shadow method calls. If the keys are code references they will be invoked, otherwise they will be simply returned as values. This can be useful if some of your object's properties are known in advance.

A specialization on lazy_object that can call a constructor method based on a class for you. The constructor and args arguments (method name or code ref, and array reference) will be removed from %args to create the thunk. They default to new and an empty array ref by default. Then this function delegates to lazy_object.