Skip Menu |

This queue is for tickets about the Scalar-List-Utils CPAN distribution.

Report information
The Basics
Id: 14945
Status: resolved
Priority: 0/
Queue: Scalar-List-Utils

People
Owner: Nobody in particular
Requestors: adamk [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.17
Fixed in: (no value)



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
Date: Wed, 5 Oct 2005 16:36:57 -0500 (CDT)
Subject: Re: [cpan #14945] sum() returns undef, is this intended?
From: "Graham Barr" <gbarr [...] pobox.com>
To: bug-Scalar-List-Utils [...] rt.cpan.org
RT-Send-Cc:
On Wed, October 5, 2005 1:21 pm, via RT wrote: Show quoted text
> It would appear that when sum is passed a null list, it returns undef
Correct. Show quoted text
> and not zero as I would have initially expected. However I can see the
logic Show quoted text
> 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
I think 2) is the correct approach. If you want zero to be returned you can always ecplicitly pass 0 as the first argument. Passing an empty list should be considered different to a list which sums to zero IMO, so returning undef seems right to me. Graham.