I need an efficient commify filter or routine for use with Template::Toolkit. It is to be used many times on the page. It should support decimals.
This one is found in The Perl Cookbook:
sub commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
Are there more efficient ways?
Before you try to optimize anything, be sure its actually a problem. Use a profiler to find the problem areas in your code and focus on those areas.
That commify solution is about as good as you can get, but there are other things you might do if you need to bypass it: