cutudi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everybody

How it's possible that you create a hash table in one function in your script like  Utils::make_hash_table() and you want to use it in another function like  main::use_hash_table() and you don't see any data in your hash table like  dbmopen(%h_yest,"data_yesterday",0666); my @cle = keys (%h_yest); $#cle ==1?

Thank's for your help

Replies are listed 'Best First'.
Re: dbmopen mistake?
by jethro (Monsignor) on Dec 23, 2011 at 12:53 UTC
    It would be helpful to also post where you create the hash. Generally speaking, when you have code like this:
    my %uberhash=(1,2,3,4); sub a { my %hash=(1,2,3,4); } sub b { #can see %uberhash #can't see %hash }

    %hash is not seen anywhere but in sub a because 'my' creates variables local to a block (and a subroutine aka function creates a block. Remedies: You could create the variable with 'our' instead of 'my' making the variable globally accessible, or add something like 'my %hash;' outside of the subroutine.

    Another possibility would be to return the hash contents as function result and assign that to a globally accessible variable

Re: dbmopen mistake?
by mbethke (Hermit) on Dec 23, 2011 at 12:50 UTC

    I suppose that's because there is actually no data in your hash. Perhaps something went wrong in the dbmopen call and you didn't see it because you're not using strict/warnings? Or you declared the hash using "my" in one function but aren't passing it to the other one as a reference? Without a real code snippet it's hard to tell.

    By the way, are you sure you want to use dbmopen? That's basically a legacy function, have a look at BerkeleyDB.pm and tie.

Re: dbmopen mistake?
by roboticus (Chancellor) on Dec 23, 2011 at 13:19 UTC

    cutudl:

    You've got two good possibilities above, and here's a third: Perhaps you're working with different hashes, something like this:

    $ cat floog.pl #!/usr/bin/perl package Utils; sub make_hash_table { %hash = qw(a,b,c,d); } package main; use Data::Dumper; Utils::make_hash_table(); print Dumper(\%hash); print Dumper(\%Utils::hash); $ perl floog.pl $VAR1 = {}; $VAR1 = { 'a,b,c,d' => undef };

    Why do you have so many radically different possibilities? Simple--you didn't show a simple program that illustrates the problem, or otherwise give enough detail to adequately describe the problems you're having. So we get to play guessing games if we feel motivated enough to try to help. If you spend a little more effort asking good questions, you can greatly increase the possibility of getting a your questions solved quickly.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: dbmopen mistake?
by NetWallah (Canon) on Dec 23, 2011 at 16:06 UTC
    If I understand your post correctly, you are attempting to populate, then use a hash using code like:
    my %hash; populate_hash(%hash); sub populate_hash{ my %h = @_; # Makes a COPY of the paramter %h= (k1=>'v1', k2=>'v2); # Populates the LOCAL %h, not '%hash' }
    Here are 2 options to correct this:
    my %hash = populate_hash( ); # Option 1: Use the Return value # Option 2: pass a REFERENCE to '%hash', like this: populate_hash(\%hash); # This requires "populate_hash" to be aware of, and use the referenc +e, like this: sub populate_hash{ my ($href) = @_; # Makes a COPY of the reference $href= {k1=>'v1', k2=>'v2}; # Notice - using CURLY brackets, not par +ens. This creates a Hash ref. }

                "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
            -Friedrich Nietzsche: A Dynamic Translation