Debian Codegraph @

README

raw
1 NAME
2 Scalar::Defer - Lazy evaluation in Perl
3
4 SYNOPSIS
5 use Scalar::Defer; # exports 'defer', 'lazy' and 'force'
6
7 my ($x, $y);
8 my $dv = defer { ++$x }; # a deferred value (not memoized)
9 my $lv = lazy { ++$y }; # a lazy value (memoized)
10
11 print "$dv $dv $dv"; # 1 2 3
12 print "$lv $lv $lv"; # 1 1 1
13
14 my $forced = force $dv; # force a normal value out of $dv
15
16 print "$forced $forced $forced"; # 4 4 4
17
18 DESCRIPTION
19 This module exports two functions, "defer" and "lazy", for constructing
20 values that are evaluated on demand. It also exports a "force" function
21 to force evaluation of a deferred value.
22
23 defer {...}
24 Takes a block or a code reference, and returns a deferred value. Each
25 time that value is demanded, the block is evaluated again to yield a
26 fresh result.
27
28 lazy {...}
29 Like "defer", except the value is computed at most once. Subsequent
30 evaluation will simply use the cached result.
31
32 force $value
33 Force evaluation of a deferred value to return a normal value. If $value
34 was already a normal value, then "force" simply returns it.
35
36 is_deferred $value
37 Tells whether the argument is a deferred value or not. (Lazy values are
38 deferred too.)
39
40 The "is_deferred" function is not exported by default; to import it,
41 name it explicitly in the import list.
42
43 NOTES
44 Deferred values are not considered objects ("ref" on them returns 0),
45 although you can still call methods on them, in which case the invocant
46 is always the forced value.
47
48 Unlike the "tie"-based Data::Lazy, this module operates on *values*, not
49 *variables*. Therefore, assigning another value into $dv and $lv above
50 will simply replace the value, instead of triggering a "STORE" method
51 call.
52
53 Similarily, assigning $dv or $dv into another variable will not trigger
54 a "FETCH" method, but simply propagates the deferred value over without
55 evaluationg. This makes it much faster than a "tie"-based implementation
56 -- even under the worst case scenario, where it's always immediately
57 forced after creation, this module is still twice as fast than
58 Data::Lazy.
59
60 CAVEATS
61 Bad things may happen if this module interacts with any other code which
62 fiddles with package 0.
63
64 SEE ALSO
65 Data::Thunk, which implements "lazy" values that can replace itself upon
66 forcing, leaving a minimal trace of the thunk, with some sneaky XS magic
67 in Data::Swap.
68
69 AUTHORS
70 Audrey Tang <[email protected]>
71
72 COPYRIGHT
73 Copyright 2006, 2007, 2008 by Audrey Tang <[email protected]>.
74
75 This software is released under the MIT license cited below.
76
77 The "MIT" License
78 Permission is hereby granted, free of charge, to any person obtaining a
79 copy of this software and associated documentation files (the
80 "Software"), to deal in the Software without restriction, including
81 without limitation the rights to use, copy, modify, merge, publish,
82 distribute, sublicense, and/or sell copies of the Software, and to
83 permit persons to whom the Software is furnished to do so, subject to
84 the following conditions:
85
86 The above copyright notice and this permission notice shall be included
87 in all copies or substantial portions of the Software.
88
89 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
90 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
91 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
92 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
93 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
94 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
95 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
96