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.
NB: removed prototype (@) from sub because as Aristotle pointed out it wasn't really doing anything.sub serial { join(', ', @_[0..$#_-1]) . (@_>2 ? ',':'' ) . (@_>1 ? (' and ' . $_[-1]) : $_[-1]); }
sample usage
Endangered primates: sifaka, gibbon, tarsier, and langur.my @primate = qw( sifaka gibbon tarsier langur ); print "Endangered primates: ", serial(@primate), ".\n";
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: serial comma function
by rob_au (Abbot) on Apr 15, 2003 at 02:48 UTC | |
by Your Mother (Archbishop) on Apr 15, 2003 at 03:07 UTC | |
by Anonymous Monk on Apr 15, 2003 at 05:57 UTC | |
|
Re: serial comma function
by Aristotle (Chancellor) on Apr 15, 2003 at 21:05 UTC | |
by Enlil (Parson) on Apr 15, 2003 at 22:56 UTC | |
by Aristotle (Chancellor) on Apr 16, 2003 at 00:04 UTC | |
|
Re: serial comma function
by Aristotle (Chancellor) on Apr 16, 2003 at 00:18 UTC | |
by Your Mother (Archbishop) on Apr 16, 2003 at 01:45 UTC | |
by Aristotle (Chancellor) on Apr 16, 2003 at 11:47 UTC |