Subject: | sum() returns undef, is this intended? |
I wrote the following to get a total number of characters of comments in a document...
sub metric_comment_chars {
my ($self, $Document) = @_;
List::Util::sum map { length $_ }
grep { s/^#//s; s/^\s+//s; s/\s+$//s; 1; }
map { $_->content }
grep { $_->isa('PPI::Token::Comment') }
$Document->tokens;
}
Much to my surprise, in early testing I caught this returning undef, which should never happen.
It would appear that when sum is passed a null list, it returns undef and not zero as I would have initially expected. However I can see the logic in both answers.
Given this, and the lack of mention of it in the docs, please do one of the following.
1) Change sum() to return zero and document that it returns 0
2) Document that sum() returns undef