Memoize::Lift - lift expression evaluation to compile time
use Memoize::Lift qw(lift); $value = lift(expensive_computation());
This module supplies an operator that causes an expression to be evaluated immediately at compile time, memoising its value for use at runtime.
Evaluate EXPR at compile time and memoise its value. Whenever a
liftexpression is evaluated at runtime, it yields the value that EXPR yielded at compile time. There is one instance of this memoisation for each instance of the
liftoperator in the source.
EXPR is lexically located where the
liftoperator is, and can use static aspects of the lexical environment normally. However, because EXPR is evaluated at compile time, it cannot use any aspects of the dynamic environment as it would exist at runtime of the
liftoperator. Lexical variables visible at the location of the
liftoperator remain visible to EXPR, but referencing them is an error.
If evaluation of EXPR results in an exception, that exception will terminate compilation.
EXPR is always evaluated in scalar context, regardless of the context in which the
liftoperator appears. To memoise a list, write
B::Deparse will generate incorrect source when deparsing
lift expressions. It will show the constant value of the expression as best it can, which is not perfect if the value is non-trivial. It has no chance at all to show the original expression that yielded that value, because the expression is not kept: the value determined at compile time is built into the op tree as a constant item.
The custom parsing code required for
lift to operate is only invoked if
lift is invoked using an unqualified name. That is, referring to it as
Memoize::Lift::lift won't work. This limitation should be resolved if Devel::CallParser or something similar migrates into the core in a future version of Perl.
Andrew Main (Zefram) <firstname.lastname@example.org>
Copyright (C) 2011 Andrew Main (Zefram) <email@example.com>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.