'Failing increment'
+= behaves differently after Data::Dump::pp.
Is this a bug in Data::Dump or possibly in Perl core?
I encountered some unexpected behavior in a simple snippet where regex captures are assigned to variables, formatted via Data::Dump::pp, concatenated, and then incremented using +=.
use v5.10;
use Data::Dump qw( pp );
"9." =~ /^ (\d+) \. (\d*) $/x;
my $n = { i => $1, f => $2 };
say "pp output: ", pp $n; # { f => "", i => 9 }
$n->{f} = "$n->{i}$n->{f}"; # "9"
$n->{f} += 2; # expected: 11
say "result: $n->{f} (expected: 11)";
Observed output:
pp output: { f => "", i => 9 }
result: 2 (expected: 11)
Instead of evaluating 9 + 2 = 11, the += 2 operation behaves as if the value of $n->{f} were numerically 0, resulting in 2.
I am not looking for a 'workaround' (I have already avoided it in the production code), but I would like to understand what causes this, and whether it warrants a bug report to Data::Dump or Perl core.
Observations
I tested several variations:
- using scalar variables instead of hash elements,
- using a direct assignment of constants instead of assigning the regex variables,
- removing the
ppoutput.
The behavior changes as follows:
- With
pp $nremoved, the result is always correct. - With scalar variables instead of hash elements, calling
ppalways causes the wrong value, regardless of whether the value originated from regex captures or constants. - With hash elements, calling
ppdoes not affect the result after constant assignment, but it does after assignment from regex captures.
My suspicion is that inspecting the variables inside Data::Dump::pp somehow alters their internal state, causing the numeric operation to behave differently.
Does this look like something that should be reported to Perl core, or to the Data::Dump maintainers?
Perl version breakdown
I ran a test script using several Perl versions that I have installed using perlbrew (all with Data::Dump version 1.25):
Perl v5.10.1 – v5.26.3: The bug does not occur.
Perl v5.28.3 – v5.44.0: The bug occurs.
Reproducing the bug
I have uploaded the full test matrix script here: failing_increment.pl, together with a Bash script test_with_available_perls that runs the test script for all Perls that are installed locally with perlbrew.
Thank you for your help!