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

Hello Monks, long time no see...!

Here's my situation:

I have a file that contains $hash{key}{key}{key} = "value".
$hash is defined as our
From the main code, it is read in a sub inside one of these modules.
When eval'd in that sub, its content updates the value for $hash{key}{key}{key}

I already figured out what was happening by reading the code, setting a fair number of breakpoints and using X var in the debugger.
I believe I could have done it smarter and watching $hash{key}{key}{key} and voilą.

I tried the following:

DB<341> w $hash->{key}->{key}->{key}

But it gives me this:

Use of each() on hash after insertion without resetting hash iterator +results in undefined behavior, Perl interpreter: 0x677010 at /somepat +h/cpan/5.18.4/1/somebuild/lib/perl5/Devel/Symdump.pm line 108. at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/Symdump.pm line +108. Devel::Symdump::_symdump(Devel::Symdump=HASH(0x9d20270), "main") c +alled at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/Symdump.pm + line 43 Devel::Symdump::_doit(Devel::Symdump=HASH(0x9d20270), "main") call +ed at /somepath/cpan/5.18.4/1/somebuild/lib/perl5/Devel/Symdump.pm li +ne 37 Devel::Symdump::new("Devel::Symdump", "main") called at [REDACTED] + line 361 [REDACTED]

Any idea?
Thanks!

Replies are listed 'Best First'.
Re: Watch the value of hash key in perldebug
by LanX (Cardinal) on Nov 26, 2020 at 14:55 UTC
    > Any idea?

    well

    >  $hash{key}{key}{key} = "value".

    and

    >  $hash->{key}->{key}->{key}

    are two different things.

    How can I reproduce your problem?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      > How can I reproduce your problem?

      well I can't

      use strict; use warnings; our $h={}; warn "init"; $h->{k1}{k2}{k3}=42; warn "call sub"; change(); warn "set"; $h->{k1}{k2}{k3}=666; warn "end"; sub change { eval '$h->{k1}{k2}{k3}=12'; }

      Current directory is d:/tmp/pm/ Loading DB routines from perl5db.pl version 1.49_05 Editor support enabled. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. DB<3> L DB<3> w $h->{k1}{k2}{k3} DB<4> c init at d:/tmp/pm/t_watch.pl line 8. at d:/tmp/pm/t_watch.pl line 8. Watchpoint 0: $h->{k1}{k2}{k3} changed: old value: '' new value: '42' DB<4> c call sub at d:/tmp/pm/t_watch.pl line 15. at d:/tmp/pm/t_watch.pl line 15. Watchpoint 0: $h->{k1}{k2}{k3} changed: old value: '42' new value: '12' DB<4> c set at d:/tmp/pm/t_watch.pl line 20. at d:/tmp/pm/t_watch.pl line 20. Watchpoint 0: $h->{k1}{k2}{k3} changed: old value: '12' new value: '666' DB<4> c end at d:/tmp/pm/t_watch.pl line 26. at d:/tmp/pm/t_watch.pl line 26. Watchpoint 0: $h->{k1}{k2}{k3} changed: old value: '666' new value: '' DB<4>

      UPDATE

      expanded code

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Watch the value of hash key in perldebug
by LanX (Cardinal) on Nov 26, 2020 at 15:21 UTC
    > I have a (very large) piece of code with many modules ...

    > ... $hash is defined as our

    But probably in different packages?

    update

    Maybe try w $PKG::hash->{key}{key}{key} but with the PKG from the module were the change happens.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I suspect that the OP is unmarshalling Marshalling a hash from a file. Appending a package name would seem to defeat the idea. A typeglob assignment allows both packages to share the variable name.
      use strict; use warnings; BEGIN{ push @ARGV, \'$hash{key}{key}{key} = "value"' if (!@ARGV); } our %hash; my $file_name = $ARGV[0]; Some::Module::some_sub($file_name); print $hash{key}{key}{key}, "\n"; package Some::Module; sub some_sub{ our %hash; *hash = *main::hash; my $fname = shift; open my $fh, '<', $fname or die "cannot open input"; my $string = <$fh>; eval $string; my $debug = 'foo'; # Just a place for debug to stop. }
      Bill
        Don't you think that's an overkill for the purpose of a watch expression for debugging?

        And a simple package Target::Package; directive inside the sub should have a similar effect - if marshalling was the intention - without the need of aliasing.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery