MUY Belgium
MUY Belgium

Reputation: 2452

Does memoize do a deep copy of perl objects?

I designed a function that return a table of objects. The content of those object is not defined yet, but I would like to make a deep copy between each calls (as in Java).

How does memoize restitute the return value? Does it makes a deep copy of it? Nothing say how answers are copied before being returned in the Memoize module help.

# How i solved the problem at first....
use Storable qw(dclone); 
my $saved_value ; # undef
sub get_all {
    return dclone($saved_value) if (defined $saved_value) ;
    ....
    $saved_value = dclone( $ans ) ;
    return $ans
}

Upvotes: 3

Views: 174

Answers (1)

ysth
ysth

Reputation: 98398

Try it and see?

use Memoize;
sub foo { {bar=>[1..4]} }
memoize('foo');
foo()->{'bar'}[3] = 5;
print foo()->{'bar'}[3];

prints 5, so it doesn't make a deep copy. I leave seeing if it makes even a shallow copy as an excercise.

Upvotes: 4

Related Questions