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

the dowhile goes into a continual loop when Data::Dumper or Data::Printer are called

originally took this to be a Data::Dumper issue, found it also in Data::Printer, so must be this code...

use strict; use warnings; use Data::Dumper; use Data::Printer; # perl v5.22.1 # Linux 4.4.0-137-generic #163-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Lin +ux # print $Data::Dumper::VERSION ."\n"; # v2.172 # print $Data::Printer::VERSION ."\n"; # 0.40 my $hash = {}; my $self = {}; $self->{a} = { b => $hash }; $self->{b} = $hash; dowhile($self); # comment this line to prevent continual output dofor($self); sub dowhile { my $self = shift; while (my ($k, $v) = each %{$self->{a}}) { # print Dumper $self->{a}; # uncomment line for continual output p $self->{a}; # uncomment line for continual output } } sub dofor { my $self = shift; for my $k (keys %{$self->{a}}) { p $self->{a}; } }

Replies are listed 'Best First'.
Re: Data::Dumper and Data::Printer continual output (each side effects)
by LanX (Saint) on Oct 22, 2018 at 13:23 UTC
    each has a global effect because the counter is changed on the data structure, when it's used at several different code places.

    From each docs:

    > Each hash or array has its own internal iterator, accessed by each, keys, and values. The iterator is implicitly reset when each has reached the end as just described; it can be explicitly reset by calling keys or values on the hash or array.

    IIRC is Data::Dumper resetting the counter internally.

    see also do-not-use-each by Reini Urban.

    Honestly I don't understand your intention, because Dumper is already recursively descending into the nested data structure. And those modules can't achieve it without using keys or values.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    rephrased and added references.

      Hi,

      thanks for the link do-not-use-each, interesting read.

      Intention of the code is only to display the behaviour described in a compact form (an ill-placed debug statement the original source)