Sometimes, for the sake of readability1, assigning '' to a variable is better that leaving it undefined, or assigning undef.
That said, it is possible for assigning '' to a variable hides errors that might otherwise be detected by the warning.
As long as the coder is aware of the trade-offs, the coder can choose the alternative that best fits a given situation.
---
1 Just adding a comment explaining what you are doing and why can actually confuse the reader:
my $buf; # leave value as undefined so use before a real value is avai
+lable will result in a warning message.
...;
while (<$input>) {
...;
$buf .= $newletter; # not an error because appending to an undef v
+alue is treated the same as appending to an empty string
}
While the above is certainly valid Perl code (and doesn't produce a warning), there are a lot of code reviewers who will reject this, citing it as being confusing.
And, often, the same reviewers will reject:
if (defined $buf) { $buf .= $newletter; }
else { $buf = $newletter; }
as being too complex.
But:
my $buf = '';
...;
while (<$input>) { ...; $buf .= $newletter; }
is clear about what is being done and comfortable to the reviewers.
Sometimes debugging a weird bug is easier than arguing with the reviewers. |