Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Putting an array into a hash gives an unexpected reference to the hash itself.

by frozenwithjoy (Priest)
on Nov 07, 2012 at 23:26 UTC ( [id://1002767]=note: print w/replies, xml ) Need Help??


in reply to Putting an array into a hash gives an unexpected reference to the hash itself.

The problem is that you are creating two references to the same thing and want them to be distinct, but they aren't (because they reference the same thing...).

Instead of this:

my %services = ( service1 => { email => \@maintainers }, service2 => { email => \@maintainers }, );

You probably want this:

my %services = ( service1 => { email => [ @maintainers ] }, service2 => { email => [ @maintainers ] }, );

Below is an example showing what can happen when using/altering references. I make two references to a single scalar, alter one of the references and show that the contents of the original scalar and both references to that scalar change (but not a copy of the original).

#!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $a = "original"; my $a_ref1 = \$a; my $a_ref2 = \$a; my $a_copy = $a; say "\$a is $a"; say "\$a_ref1 is $$a_ref1"; say "\$a_ref2 is $$a_ref2"; say "\$a_copy is $a_copy"; say "OK, let's alter only the first reference (\$a_ref1):"; $$a_ref1 = "altered"; say "\$a is $a"; say "\$a_ref1 is $$a_ref1"; say "\$a_ref2 is $$a_ref2"; say "\$a_copy is $a_copy";

Output:

$a is original $a_ref1 is original $a_ref2 is original $a_copy is original OK, let's alter the first reference ($a_ref1): $a is altered $a_ref1 is altered $a_ref2 is altered $a_copy is original

Replies are listed 'Best First'.
Re^2: Putting an array into a hash gives an unexpected reference to the hash itself.
by polarbear (Novice) on Nov 08, 2012 at 01:00 UTC
    Thank you, that solved my problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1002767]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found