$ perl -E ' say for map { join q{-}, unpack q{(a3)*}, $_ } qw{ 12 123 1234 12345 123456 1234567 };' 12 123 123-4 123-45 123-456 123-456-7 $