in reply to Hash as Hash Value

Thank you for the responses. I'm not sure why I'm having such a struggle with hash referencing and de-referencing, but this helped.

Given my previous example, suppose I have something like this within my subroutine:

my $hashOfUsers = $args->{users}

Why does this:

$hashOfUsers{$someKey}{UID}

not return the value of UID for the user identified by key $someKey?

Replies are listed 'Best First'.
Re^2: Hash as Hash Value
by kennethk (Abbot) on Apr 21, 2010 at 14:08 UTC
    When you write $hashOfUsers{$someKey}{UID}, perl automatically rewrites it internally as $hashOfUsers{$someKey}->{UID}, using The Arrow Operator to dereference. This is then interpreted as:

    1. Get the value from the hash %hashOfUsers corresponding to $someKey
    2. deference it as a hash (->{}) and
    3. get the key associated with UID."

    If $hashOfUsers is a hashref and you want to access that, you want the syntax $hashOfUsers->{$someKey}{UID}. Perl can't automatically know to deference $hashOfUsers because then $hashOfUsers{$someKey} would be ambiguous in the case where you had both a hash and a scalar named hashOfUsers. It can automatically deference the second bracket since the syntax is unambiguous.

Re^2: Hash as Hash Value
by choroba (Cardinal) on Apr 21, 2010 at 14:04 UTC
    Because $hashOfUsers is a scalar, not a hash. It is a hash reference, though, so you can use
    $hashOfUsers->{$someKey}{UID}
      Drat!