in reply to serial comma function

Nice job, ++. But why the prototype? That one is the same as not specifying any, anyway. You can also get rid of a last smidgen of redundancy in the second ternary.
sub serial { join(', ', @_[0 .. $#_-1]) . (@_ > 2 ? ',' : '') . (@_ > 1 ? ' and ' : '') . $_[-1]; }
I think it'd look cleaner with a pop, too.
sub serial { my $last = pop; join(', ', @_) . (@_ > 1 ? ',' : '') . (@_ > 0 ? ' and ' : '') . $last; }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: serial comma function
by Enlil (Parson) on Apr 15, 2003 at 22:56 UTC
    I agree cleaner with pop, but to many ternary operators. :)
    sub serial { my $last = pop; @_ ? join(', ', @_) . " and $last" : $last; }
    update:forgot about that whole, if there are more than two in the list with the commas bit, so neglect the above as it does not do what the original code posted does (as Aristotle's does.)

    -enlil

      I was actually about to reply with something similar myself before I noticed there's three cases to distinguish. FWIW, here's my previous take - which adds a comma even when there's only two elements in the list:
      sub serial { my $last = ''; ($last, $_[-1]) = ($_[-1], 'and ') if @_ > 1; join(', ', @_) . $last; }

      Makeshifts last the longest.