I think this might be best asked using an example:
use strict;
use warnings;
use 5.010;
use Storable qw(nstore retrieve);
local $Storable::Deparse = 1;
local $Storable::Eval = 1;
sub sub_generator {
my ($x) = @_;
return sub {
my ($y) = @_;
return $x + $y;
};
}
my $sub = sub_generator(1000);
say $sub->(1); # gives 1001
nstore( $sub, "/tmp/sub.store" );
$sub = retrieve("/tmp/sub.store");
say $sub->(1); # gives 1
When I dump /tmp/sub.store
I see:
$VAR1 = sub {
package Storable;
use warnings;
use strict 'refs';
my($y) = @_;
return $x + $y;
}
But $x
is never defined in this sub. I would expect that the sub generated by sub_generator
will have $x
replaced with its actual value upon generation. How should I solve this?
Note this question relates to this one.
Unfortunately I don't think
Storable
works with closures. However there are other CPAN modules that will serialise a closure. For eg.Data::Dump::Streamer
This is what the serialised code looks like when printed here,
say Dump $sub;
:Update
See this thread Storable and Closures on the Perl5 porters mailing list. It confirms what I thought about
Storable
and closures./I3az/