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.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: How to change referent of lexical reference? by BrowserUk
in thread How to change referent of lexical reference? by autarch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.