Lazy evaluation in perl
use Scalar::Defer; # exports 'defer', 'lazy' and 'force' my ($x, $y); my $dv = defer { ++$x }; # a deferred value (not memoized) my $lv = lazy { ++$y }; # a lazy value (memoized) print "$dv $dv $dv"; # 1 2 3 print "$lv $lv $lv"; # 1 1 1 my $forced = force $dv; # force a normal value out of $dv print "$forced $forced $forced"; # 4 4 4
This module exports two functions, \*(C`defer\*(C' and \*(C`lazy\*(C', for constructing values that are evaluated on demand. It also exports a \*(C`force\*(C' function to force evaluation of a deferred value.
Takes a block or a code reference, and returns a deferred value. Each time that value is demanded, the block is evaluated again to yield a fresh result.
Like \*(C`defer\*(C', except the value is computed at most once. Subsequent evaluation will simply use the cached result. Force evaluation of a deferred value to return a normal value. If $value was already a normal value, then \*(C`force\*(C' simply returns it. Tells whether the argument is a deferred value or not. (Lazy values are deferred too.)
The \*(C`is_deferred\*(C' function is not exported by default; to import it, name it explicitly in the import list.
Deferred values are not considered objects (\*(C`ref\*(C' on them returns 0), although you can still call methods on them, in which case the invocant is always the forced value.
Unlike the \*(C`tie\*(C'-based Data::Lazy, this module operates on values, not variables. Therefore, assigning another value into $dv and $lv above will simply replace the value, instead of triggering a \*(C`STORE\*(C' method call.
Similarily, assigning $dv or $dv into another variable will not trigger a \*(C`FETCH\*(C' method, but simply propagates the deferred value over without evaluationg. This makes it much faster than a \*(C`tie\*(C'-based implementation \*(-- even under the worst case scenario, where it's always immediately forced after creation, this module is still twice as fast than Data::Lazy.
Bad things may happen if this module interacts with any other code which fiddles with package 0.
Performance of creating new deferred or lazy values can be quite poor under perl 5.8.9. This is due a bugfix since 5.8.8, where re-blessing an overloaded object caused bad interactions with other references to the same value. 5.8.9's solution involves walking the arenas to find all other references to the same object, which can cause \*(C`bless\*(C' (and thus \*(L"defer\*(R" in Scalar::Defer to be up to three orders of magnitude slower than usual. perl 5.10.0 and higher do not suffer from this problem.
Data::Thunk, which implements \*(C`lazy\*(C' values that can replace itself upon forcing, leaving a minimal trace of the thunk, with some sneaky \s-1XS\s0 magic in Data::Swap.
Audrey Tang <[email protected]>
Copyright 2006, 2007, 2008, 2009 by Audrey Tang <[email protected]>.
This software is released under the \s-1MIT\s0 license cited below. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \*(L"Software\*(R"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
\s-1THE\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R", \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0 \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0 \s-1THE\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTABILITY\s0, \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1AND\s0 \s-1NONINFRINGEMENT\s0. \s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1OR\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1CLAIM\s0, \s-1DAMAGES\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1AN\s0 \s-1ACTION\s0 \s-1OF\s0 \s-1CONTRACT\s0, \s-1TORT\s0 \s-1OR\s0 \s-1OTHERWISE\s0, \s-1ARISING\s0 \s-1FROM\s0, \s-1OUT\s0 \s-1OF\s0 \s-1OR\s0 \s-1IN\s0 \s-1CONNECTION\s0 \s-1WITH\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1OR\s0 \s-1THE\s0 \s-1USE\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1DEALINGS\s0 \s-1IN\s0 \s-1THE\s0 \s-1SOFTWARE\s0.