use strict; use warnings; print mungeAddress($_) . "\n" for '123 Circle Way', '123 Way Circle'; sub mungeAddress { my %street_suf_lkup = (circle => 'Cir', boulevard => 'Blvd'); my @parts = map { # Apply the global corrections # proper case words, simple $_ = ucfirst lc $_; # if apartments or units, such as #A or #215-C, capitalize unit # numbers. Don't strip off pound signs. s/^(#.+)/\U$1/; # correct numbering, like 0000048th -> 48th s/^(#?)0+([1-9]\S+)/$1$2/; # correct casing of O'Brien, McDonald, etc. s/^(mc|o')(.+$)/\u\L$1\E\u\L$2/i; # Remove literal dots at the end of elements. s/\.+$//g; $_; } split ' ', $_[0]; # Normalize street prefixes to standard postal abbreviations, for # example Circle -> Cir, Boulevard -> Blvd $parts[-1] = $street_suf_lkup{lc $parts[-1]} if exists $street_suf_lkup{lc $parts[-1]}; return "@parts"; }