Hi Monks
I have created an object that needs to store 2 hashes that point to the same referenced hash. The idea is that I want to store a "clean" and dirty "version" in the object.
The problem is, that the object uses the same pointer for both hashes, meaning that if I change the dirty hash, the clean hash is also changed. This is the code:
sub new { my ($class, $hash) = @_; my $this = {}; bless($this, $class); $this->{clean} = $hash; $this->{dirty} = \%orig_graph; print Dumper $this; die (0); return $this; }
If I print it (using the dumper) I get:
$VAR1 = bless( { 'dirty' => { 'a' => 1, 'b' => 2 }, 'clean' => $VAR1->{'dirty'} }, 'Edges' );
As you can see, clean points to dirty, so I can't change one without the other.
My question is, what can I do to store two versions like I want. I can go over all the keys in the hash and create a new hash, but I was wondering if there was something smarter, a bit like a copy constructer? Any ideas?
Thanks,
MrGuy


I can't complain, but sometimes I still do."
-- Joe Walsh

Thanks for all your help!
I used clone for a deep copy, and managed to fix my problem
Thanks again
Mrguy

In reply to Using the same hashes as different object variables by mrguy123

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.