I've used this in most CGIs (from a personal ~/lib/Tools.pm) as a way of human formatting missing fields or other list-ish items back to the user. It's a bit obfuscated but I've never needed to change it so I like it better this way.

Update (15 Aug 2004) Template Toolkit version added.

sub serial { join(', ', @_[0..$#_-1]) . (@_>2 ? ',':'' ) . (@_>1 ? (' and ' . $_[-1]) : $_[-1]); }
NB: removed prototype (@) from sub because as Aristotle pointed out it wasn't really doing anything.

sample usage

my @primate = qw( sifaka gibbon tarsier langur ); print "Endangered primates: ", serial(@primate), ".\n";
  Endangered primates: sifaka, gibbon, tarsier, and langur.

Update: Serial comma virtual method with Template Toolkit (same code, different use). Found I missed it when moving to TT2 so discovered how to transplant it.

# in some parent formatting, template processing module use Template; # not actually using it in the code here use Template::Stash; $Template::Stash::LIST_OPS->{ serial } = sub { my @list = @{ +shift || [] }; join(', ', @list[0..$#list-1]) . (@list>2 ? ',':'' ) . (@list>1 ? (' and ' . $list[-1]) : $list[-1]); };

Now in TT code you can do this with lists:

Hey, Joe. Where you going with that [% user.ListOfItems.serial %] in your hand?

In reply to serial comma function by Your Mother

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.