|
NAMEData::Thunk - A sneakier Scalar::Defer ;-)SYNOPSISuse Data::Thunk qw(lazy); my %hash = ( foo => lazy { $expensive }, ); $hash{bar}{gorch} = $hash{foo}; $hash{bar}{gorch}->foo; # vivifies the object warn overload::StrVal($hash{foo}); # replaced with the value DESCRIPTIONThis 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. In this particular example: my $x = { foo => lazy { "blah" }, bar => lazy { [ "boink" ] }, }; $x->{quxx} = $x->{foo}; $x->{gorch} = $x->{bar}; warn $x->{bar}; warn $x->{foo}; warn $x->{quxx}; use Data::Dumper; warn Dumper($x); The resulting structure is: $VAR1 = { 'bar' => [ 'boink' ], 'foo' => 'blah', 'gorch' => $VAR1->{'bar'}, 'quxx' => 'blah' }; Whereas with Scalar::Defer the trampoline objects remain: $VAR1 = { 'bar' => bless( do{\(my $o = 25206320)}, '0' ), 'foo' => bless( do{\(my $o = 25387232)}, '0' ), 'gorch' => $VAR1->{'bar'}, 'quxx' => $VAR1->{'foo'} }; This is potentially problematic because "reftype" in Scalar::Util and "blessed" in Scalar::Util can't be fooled. With Data::Thunk the problem still exists before values are vivified, but not after. 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. EXPORTS
SEE ALSOScalar::Defer, Data::Lazy, Data::Swap, UNIVERSAL::ref.AUTHORYuval KogmanCOPYRIGHT AND LICENSEThis software is Copyright (c) 2010 by Yuval Kogman.This is free software, licensed under: The MIT (X11) License
Visit the GSP FreeBSD Man Page Interface. |