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

Why are hash values missing?
#declare hash in main my %myhash; #require the autoloaded modules: require ${modulename}; require ${usermodule}; #build the hash in an autoloaded module using a reference: &{${modulename}.'::$modulename'}($param1,\%myhash,\%hash2); #call another autoloaded module that wants to use the hash: &{${usermodule}.'::$usermodule'}($param1,\%myhash,\$array); #in $usermodule, keys to the hash exist: @mykeys = keys %$hashref; # is non-empty array #but any attempt to get hash values produces null. e.g., my $myval = ${hashref}->{$mykeys[0]};

I can print the keys and values just before calling the user module. But inside that module the keys exist while the values are null. The main perl script has grown over time, but this used to work.
Can this have something to do with size of compiled code?

Replies are listed 'Best First'.
Re: hash values missing
by Joost (Canon) on Mar 26, 2008 at 16:41 UTC
    require ${modulename};
    What's wrong with require $modulename; ?

    #build the hash in an autoloaded module using a reference: &{${modulename}.'::$modulename'}($param1,\%myhash,\%hash2);

    Is that *REALLY* what's in your code? Are you *SURE* you're not getting errors doing that? Are you sure there is a subroutine literally named "$modulename" (note the $ at the start of the name) in the package named by $modulename ? Are you sure you understand what that code does? In any case, we'd probably need to see the code of that subroutine to help you out.

    #in $usermodule, keys to the hash exist: @mykeys = keys %$hashref; # is non-empty array
    That doesn't prove the keys you're looking for are there. Check that with exists.

Re: hash values missing
by apl (Monsignor) on Mar 26, 2008 at 16:57 UTC
    One problem is that your code doesn't compile. If you add
    use strict; use warnings;

    you'll get

    Global symbol "$modulename" requires explicit package name at /apps/ot +ctest/bin/report/Test0.perl line 10. Global symbol "$usermodule" requires explicit package name at /apps/ot +ctest/bin/report/Test0.perl line 11. Global symbol "$param1" requires explicit package name at /apps/otctes +t/bin/report/Test0.perl line 14. Global symbol "%hash2" requires explicit package name at /apps/otctest +/bin/report/Test0.perl line 14. Global symbol "$array" requires explicit package name at /apps/otctest +/bin/report/Test0.perl line 17. Global symbol "@mykeys" requires explicit package name at /apps/otctes +t/bin/report/Test0.perl line 20. Global symbol "$hashref" requires explicit package name at /apps/otcte +st/bin/report/Test0.perl line 20. Execution of /apps/otctest/bin/report/Test0.perl aborted due to compil +ation errors.

    If you define all that, then you'll finally see: When I run your sample code, I get the error:

    Null filename used at /apps/otctest/bin/report/Test0.perl line 19.

    (That's your require ${modulename}; statement.)

    Can you post the minimal runnable code that produces the problem you wish us to research?

    How do you define $modulename?

    Revised after I was chastened by chromatic for unintended rudeness.

      One problem is that your code doesn't compile.

      Waitaminute! I really do recommend the use of strict and warnings, but chastising someone for posting non-working code after you added code that makes the original code not compile is a little bit rude.