grondilu has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks,
I faced a weird failure while writing a solution to this rosalind problem. I won't paste the whole solution but here is the relevant part:
sub distance { ... do { return $1 + distance($_) if s/\(#:(\d+)\)/#/ } until ... ... }
This code returned a wrong result and a "use of uninitialized value $1 in addition" warning message. Such a warning is weird since I am in a section code where the substitution regex is supposed to have happened, thus affecting $1.
To fix the code, I had to write 0+$1 instead of just $1. (my $x = $1) would have worked too. The warning message disappeared and I ended up with the correct answer. As I understand it, it seems that I had to somehow "nail" the value before using it. But I don't quite understand why.
Any idea?
PS. Here is a simple example suggested below that reproduces the failure (not the warning, though):
sub butter { my $_ = shift; return $1 + butter($_) if s/(\d+)//; return 0; } print butter 'effect / 7ACV06';
It prints 12 with $1, and 13 with 0+$1.