Perl: Return hash from subroutine

后端 未结 3 884
北荒
北荒 2020-12-10 18:19

I have been trying examples for hours but I can\'t seem to grasp how to do what I want to do.

I want to return a hash from a subroutine, and I figured a reference wa

3条回答
  •  一个人的身影
    2020-12-10 19:12

    You probably don't want to be doing this:

    return \$hashTable{ $login };
    

    You are returning a reference (pointer) to your hash. Try just

    return $hashTable{$login} 
    

    Which will return the account number.

    Or if you really want a hash with a bunch of people in there then

    return \$hashTable

    is fine (don't add the {$login} part), but on the other side you need to be dereferencing.

    eg:

       my $p = authUser()
       if ($p->{'Dr. Brule'})
       ...
    

    Notice the -> in there. It de-references the pointer you passed back.

提交回复
热议问题