Another way to do it... (but only works on newer perls with lookbehind.) I didn't realize it was a FAQ when I needed to do it. The theme is the same though. Repeatedly make substitutions until there are none left to make.
1 while ( s/(?<!\b)(\d{3})(?:,|$)/,$1/ );
# Explained
1 while ( # Repeat until no more matches are made.
s/(?<!\b)(\d{3}) # Match any three digits not preceded by a word
+break.
(?:,|$) # That come immediately before a comma or the en
+d.
/,$1/x # And insert a comma before them.
);
-sauoq
"My two cents aren't worth a dime.";