|Just another Perl shrine|
$1 not "freezing" in an additionby grondilu (Pilgrim)
|on Dec 14, 2012 at 12:17 UTC||Need Help??|
grondilu has asked for the
wisdom of the Perl Monks concerning the following question:
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:
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.
PS. Here is a simple example suggested below that reproduces the failure (not the warning, though):
It prints 12 with $1, and 13 with 0+$1.