sum#
Add up every value in the list and return the numerical total.
Synopsis#
my $total = sum @values;
my $total = sum 1..10; # 55
What you get back#
A single number — an integer if every argument summed as an
integer, a float otherwise. Integer arithmetic wraps on overflow
rather than promoting, matching upstream. For an empty list, sum
returns undef for backwards compatibility; if that is awkward
use sum0 instead.
Examples#
my $n = sum 3, 9, 12; # 24
my $n = sum @bar, @baz; # concatenated lists
my $n = sum(); # undef
my $n = sum map { length } @s; # total length of all strings
Edge cases#
Empty list returns
undef. Usesum0for a0default.Mixing ints and floats promotes the running total to a float.
Non-numeric strings are coerced via the usual numification.
Differences from upstream#
Fully compatible with upstream.
See also#
sum0— same assum, but returns0for an empty list.product— multiplies rather than adds.reduce— the generic reduction whensumis not specific enough.