in reply to Re: hash ref mind blow
in thread hash ref mind blow

Yup, perfectly clear! Thank you! But I've just realized that I don't how to do one thing... how can I make %hash and %copy be in the same mem location? So that when I add a new key to %copy, %hash also changes. I'm just lazy about working with a ref directly as in
$$p{c}=3;
or
$p->{c}=3;
I just want
$copy{c}=3;
To change the value of %hash.
-rod

Replies are listed 'Best First'.
Re^3: hash ref mind blow
by Corion (Patriarch) on Sep 24, 2008 at 16:53 UTC

    For global variables, just do a glob assignment:

    *copy = \%hash;

    then, %copy and %hash are the same data structure. Otherwise, that is, for lexical variables, use Data::Alias.

      Make sure to localize package variables (such as *copy).

      our %copy; local *copy = \%hash;

      or

      use Data::Alias; alias my %alias = %hash;
Re^3: hash ref mind blow
by Anonymous Monk on Sep 25, 2008 at 08:55 UTC
    Just so you know, Data::Dumper is part of every perl
    #!/usr/bin/perl -- use strict; my %hash = (); $hash{a}{drinks}=1; $hash{b}{drinks}=2; my $p = \%hash; my %copy = %{ $p }; use Data::Dumper; print Dumper( \%hash, \%copy, $p ); __END__ $VAR1 = { 'a' => { 'drinks' => 1 }, 'b' => { 'drinks' => 2 } }; $VAR2 = { 'a' => $VAR1->{'a'}, 'b' => $VAR1->{'b'} }; $VAR3 = $VAR1;
      Just so you know, Data::Dumper is part of every perl
      Every modern Perl, of course:
      $ corelist Data::Dumper Data::Dumper was first released with perl 5.005