Hi Geoff, i like your explanation of the fundamentals of references, but near the end, you run the risk of spreading confusion:
Geoff wrote on Thu, Jun 26, 2008 at 10:12:00AM -0400: > ANOTHER way is to say: %myhash = %$data, and then %myhash > becomes the equivalent of the original %hash. Better say: a shallow copy, not "the equivalent". Why is this important? 1. A reference is not a copy. After you change the original, the reference points to the new value. $ perl -e ' > use warnings; > use strict; > my %orig = ( key => "oldvalue" ); > my $ref = \%orig; > print "at first, ref points to: $ref->{key}\n"; > $orig{key} = "newvalue"; > print "after changing orig, ref also points to: $ref->{key}\n";' at first, ref points to: oldvalue after changing orig, ref also points to: newvalue 2. Hash assignment does copy. When you change the original, the copy does not change. $ perl -e ' > use warnings; > use strict; > my %orig = ( key => "oldvalue" ); > my %copy = %orig; > $orig{key} = "newvalue"; > print "after changing orig, copy still contains: $copy{key}\n";' after changing orig, copy still contains: oldvalue 3. The copy is shallow. So, if references are contained, they are copied as refernces: $ perl -e ' > use warnings; > use strict; > my $scalar = "oldvalue"; > my $ref = \$scalar; > my %orig = ( key => $ref ); > print "${$orig{key}}\n"; # caution: not $orig->{key} > my %copy = %orig; > $scalar = "newvalue"; > print "${$copy{key}}\n";' oldvalue newvalue You need to be very careful what is refernced and what is copied; in fact, that's the whole point of references... Vague terms like "equivalent" are not helpful, and even "assign" may sometimes be confusing. [...l > %anotherhash = %$hashref; [...] > When you assign %anotherhash to %$hashref, Well, no. What you are doing here is assigning the contents of %$hashref to %anotherhash. Or better, copying the contents of %$hashref into %anotherhash. Yours, Ingo -- Ingo Schwarze <[EMAIL PROTECTED]> | Software Engineer | Framework Team Astaro AG | www.astaro.com | 76227 Karlsruhe | Germany _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs