in reply to How to change referent of lexical reference?
If I understand you, you want to be able to freeze two (or more) references to the same data or datastructure and when you thaw them, have both references again refer to a single copy of the referent, rather than two separate copies.
For this to happen, you need to ensure that you pass both (all) of the data to Storable::freeze() at the same time. This is the only way that it will be able to determine that the references do indeed refer to the same data.
#! perl -slw use strict; use Data::Dumper; use Storable qw[freeze thaw]; my $hashref = { my=>'data' }; my $refA = \$hashref; my $refB = \$hashref; { my @frozen = map{ freeze $_ } $refA, $refB; my( $refA_thawed, $refB_thawed ) = map{ thaw $_ } @frozen; print Dumper $refA_thawed, $refB_thawed; } { my @data = ( $refA, $refB ); my $frozen = freeze \@data; my $thawed = thaw $frozen; my ($refA, $refB) = @$thawed; print Dumper $refA, $refB; } __END__ $VAR1 = \{ 'my' => 'data' }; $VAR2 = \{ 'my' => 'data' }; $VAR1 = \{ 'my' => 'data' }; $VAR2 = $VAR1;
In the first anon. block, the two references are frozen in separate calls to freeze() and thaw(), and so Storable has no way to recognise that they refer to the same data, and when they are thawed, they are pointed at separate copies of the data.
However, in the second block, the two references are passed to freeze() during the same call. This is achieved by simply wrapping them into a common data structure (@data). This way, you are giving Storable the opportunity to recognise that the both references refer to the same data, and it does the appropriate thing accordingly.
Now when the data is thawed(), the resultant references again point to a single copy of the referent.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to change referent of lexical reference?
by autarch (Hermit) on Sep 23, 2003 at 03:43 UTC | |
by BrowserUk (Patriarch) on Sep 23, 2003 at 04:22 UTC | |
by autarch (Hermit) on Sep 23, 2003 at 04:39 UTC | |
by BrowserUk (Patriarch) on Sep 23, 2003 at 06:49 UTC | |
by autarch (Hermit) on Sep 24, 2003 at 16:12 UTC |