develooper Front page | perl.beginners | Postings from April 2010

How to make a hash in a subroutine accessible by all other subrountines?

Thread Next
From:
Remy Guo
Date:
April 29, 2010 01:30
Subject:
How to make a hash in a subroutine accessible by all other subrountines?
Message ID:
l2h34ed02d1004290130w6c62f110x94c19263bb07683f@mail.gmail.com
hi all,
I've got a problem in following script:
sub A  {
    our %a;
    $a{"fred"} = 1;
    $a{"bella"} = 2;
...
}
sub B {
    if ($fred != $a{"fred"})  {
        print "fred failed.\n";
    }
    if ($bella != $a{"bella"}  {
        print "bella failed.\n";
    }
}

The problem is, I made the hash %a in sub A but in sub B, the value in hash
%a is never read. The declaration "our" seems not effect.
Why?...

Thanks in advance...

-Remy

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About