Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: how do I use a hash of hashes reference to a function ?

by arturo (Vicar)
on Sep 22, 2000 at 18:33 UTC ( #33635=note: print w/replies, xml ) Need Help??


in reply to how do I use a hash of hashes reference to a function ?

Alternately, although this is less readable, you can dereference a hash in the subroutine with:
foo(\%hash); sub foo { my %hash = %{shift()}; # rest of function here, which manipulates hash directly }

Call me perverse, but I just love the way that line looks (from what I understant, the () is required when you put shift in the braces to force perl to recognize it as a function call and not a hash key ... but I've been wrong about these things in the past ...

Replies are listed 'Best First'.
RE: Answer: how do I use a hash of hashes reference to a function ?
by Fastolfe (Vicar) on Sep 22, 2000 at 19:55 UTC
    This does not affect the real %hash. By de-referencing it like that, you're essentially creating a copy. The item on the right-hand-side is now a real hash (by virtue of wrapping it in %{...}), not a reference. Thus, you are effectively doing this:
    %hash1 = %hash2;
    This does not create an alias, but a copy. Changes to %hash1 have no effect on %hash2.

    And no, you don't have to put parens around your split call. Perl does the correct thing here, but it may be more readable to others if you write it with parens anyway. Here is some test-case code you can try yourself:

    sub test { my $hash1 = shift; my %hash2 = %{shift}; $hash1->{three} = 3; $hash2{three} = 3; } my %a = (one => 1, two => 2); my %b = (one => 1, two => 2); &test(\%a, \%b); print join(", ", keys %a), "\n"; print join(", ", keys %b), "\n"; # three, two, one # two, one

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2020-10-31 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (287 votes). Check out past polls.

    Notices?