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

Hi, I'm passing hash ref with data to subroutine. When I try to print inside subroutine, sometimes it prints values and sometime I see empty(verified hash has data before invoking subroutine). But if I dump(using Dumper) before invoking subroutine, subroutine always prints correctly. leaving Dumper in code doesn't see right. any suggestions please? thanks

Replies are listed 'Best First'.
Re: hash ref is empty inside subroutine
by LanX (Saint) on Sep 13, 2013 at 18:25 UTC
    I just hacked into your computer to check the code you're not showing us.

    But don't be afraid I won't reproduce an excerpt here ... would be too easy for the others to help you then... ;-)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: hash ref is empty inside subroutine
by tobyink (Canon) on Sep 13, 2013 at 21:41 UTC

    For what it's worth I seem to recall having experienced something similar. Try replacing your Dumper bit with something else that forces Perl to peek inside the hash. Something like this perhaps:

    do { my @tmp = keys(%$hashref) };
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: hash ref is empty inside subroutine
by NetWallah (Canon) on Sep 13, 2013 at 18:34 UTC
    Here are the results from debugging your invisible code:












    (That's right - the results are invisible!)

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: hash ref is empty inside subroutine
by AnomalousMonk (Archbishop) on Sep 14, 2013 at 09:44 UTC

    Further to tobyink's post: Here's one scenario (of many) in which hash data can 'vanish' in certain circumstances, only to magically reappear later:

    >perl -wMstrict -le "my $hr = { 'Node' => 'TestServer' }; my ($k) = each %$hr; print qq{'$k'}; ;; pk($hr); ;; sub pk { my ($hr) = @_; my ($k) = each %$hr; print qq{'$k'} } " 'Node' Use of uninitialized value $k in concatenation (.) or string at -e lin +e 1. ''

    Now try inserting an otherwise-useless keys statement after the first each statement (which exhausts hash iteration in this case):
        my ($k) = each %$hr;
        my $ignore_this = keys %$hr;

    >perl -wMstrict -le "my $hr = { 'Node' => 'TestServer' }; my ($k) = each %$hr; my $ignore_this = keys %$hr; print qq{'$k'}; ;; pk($hr); ;; sub pk { my ($hr) = @_; my ($k) = each %$hr; print qq{'$k'} } " 'Node' 'Node'

    See each and keys (and values) for discussions of hash iteration behavior.

    Update: neousr: Others have alluded to the annoying fact that you show no (useful) code. What would really be useful would be a small, standalone, executable code example demonstrating the problem. The effort to produce such code may not be trivial, but the very process of producing it may clarify the problem so far as to answer your own question!

Re: hash ref is empty inside subroutine
by CountZero (Bishop) on Sep 13, 2013 at 19:23 UTC
    It must be the (in)famous Heisenbug.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Thanks CountZero
        Ok w/o ironies ... why don't you show some code?

        Cheers Rolf

        ( addicted to the Perl Programming Language)