.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Scalar::Defer 3pm" .TH Scalar::Defer 3pm "2018-07-08" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Scalar::Defer \- Lazy evaluation in Perl .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Scalar::Defer; # exports \*(Aqdefer\*(Aq, \*(Aqlazy\*(Aq and \*(Aqforce\*(Aq \& \& 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 .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module exports two functions, \f(CW\*(C`defer\*(C'\fR and \f(CW\*(C`lazy\*(C'\fR, for constructing values that are evaluated on demand. It also exports a \f(CW\*(C`force\*(C'\fR function to force evaluation of a deferred value. .SS "defer {...}" .IX Subsection "defer {...}" 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. .SS "lazy {...}" .IX Subsection "lazy {...}" Like \f(CW\*(C`defer\*(C'\fR, except the value is computed at most once. Subsequent evaluation will simply use the cached result. .ie n .SS "force $value" .el .SS "force \f(CW$value\fP" .IX Subsection "force $value" Force evaluation of a deferred value to return a normal value. If \f(CW$value\fR was already a normal value, then \f(CW\*(C`force\*(C'\fR simply returns it. .ie n .SS "is_deferred $value" .el .SS "is_deferred \f(CW$value\fP" .IX Subsection "is_deferred $value" Tells whether the argument is a deferred value or not. (Lazy values are deferred too.) .PP The \f(CW\*(C`is_deferred\*(C'\fR function is not exported by default; to import it, name it explicitly in the import list. .SH "NOTES" .IX Header "NOTES" Deferred values are not considered objects (\f(CW\*(C`ref\*(C'\fR on them returns \f(CW0\fR), although you can still call methods on them, in which case the invocant is always the forced value. .PP Unlike the \f(CW\*(C`tie\*(C'\fR\-based Data::Lazy, this module operates on \fIvalues\fR, not \fIvariables\fR. Therefore, assigning another value into \f(CW$dv\fR and \f(CW$lv\fR above will simply replace the value, instead of triggering a \f(CW\*(C`STORE\*(C'\fR method call. .PP Similarly, assigning \f(CW$dv\fR or \f(CW$dv\fR into another variable will not trigger a \f(CW\*(C`FETCH\*(C'\fR method, but simply propagates the deferred value over without evaluationg. This makes it much faster than a \f(CW\*(C`tie\*(C'\fR\-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. .SH "CAVEATS" .IX Header "CAVEATS" Bad things may happen if this module interacts with any other code which fiddles with package \f(CW0\fR. .PP 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 \f(CW\*(C`bless\*(C'\fR (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. .SH "SEE ALSO" .IX Header "SEE ALSO" Data::Thunk, which implements \f(CW\*(C`lazy\*(C'\fR values that can replace itself upon forcing, leaving a minimal trace of the thunk, with some sneaky \s-1XS\s0 magic in Data::Swap. .SH "AUTHORS" .IX Header "AUTHORS" Audrey Tang .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2006, 2007, 2008, 2009 by Audrey Tang . .PP This software is released under the \s-1MIT\s0 license cited below. .ie n .SS "The ""\s-1MIT""\s0 License" .el .SS "The ``\s-1MIT''\s0 License" .IX Subsection "The MIT License" 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: .PP The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. .PP \&\s-1THE SOFTWARE IS PROVIDED \*(L"AS IS\*(R", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\s0