CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: "exists" if you just have hash reference?: Edit Log



perlFun
Novice

Jun 19, 2013, 10:29 AM


Views: 633
"exists" if you just have hash reference?

say I have a key $key and a hash reference $hashRef (I got $hashRef from a subroutine which returns a reference to a hash). Now say I want to check that $key is indeed a key for the hash referenced by $hashRef. if I do the following:

my $hashRef = stupid_sub();#hashRef is a reference to a hash

if(exists $hashRef{$key}){
...do stuff
}

I get the error:

Global symbol "%hashRef" requires explicit package name at ... (etc)


Now if I do

my %hashRef = stupid_sub();

this compiles fine. But stupid_sub is a subroutine which only returns a reference to a hash so I think this shouldn't work in reality. Is there a way to, in that same line, actually get the hash that's being referenced? so I don't have to do:

my $hashRef = stupid_sub();
my %hash = %$hashRef;
if(exists $hash{$key}} {
... stuff
}


thanks again for all the help


(This post was edited by perlFun on Jun 19, 2013, 10:30 AM)


Edit Log:
Post edited by perlFun (Novice) on Jun 19, 2013, 10:30 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives