http://www.perlmonks.org?node_id=435711

The well-known commifying snippet from the perlfaq5 is tricky enough to understand; the recent addition in that answer is much worse. And all of the other snippets I've seen use regex contortions to get the job done as well. Why?

The following is completely clear and straightforward, yet as I'm surprised to find, it seems noone else has posted something along these lines so far.

Update: of course it was supposed to be scalar reverse in both instances.

Update: this original version doesn't correctly handle anything but a string of digits:

sub commify { scalar reverse join ',', unpack '(A3)*', scalar reverse shift }

Update: gack, I need to sleep. Fixed three instances of reverse scalar that were supposed to be scalar reverse.

sub commify { my ( $sign, $int, $frac ) = ( $_[0] =~ /^([+-]?)(\d*)(.*)/ ); my $commified = ( scalar reverse join ',', unpack '(A3)*', scalar reverse $int ); return $sign . $commified . $frac; }