Perl resources, modules, and sample code

Titlecase with Perl

Monday, 13 August 2007—subject to ongoing, unnanounced updates

Modern Perl Modern Perl at Powell’s, Modern Perl at Barnes and Noble, Modern Perl at Amazon.com, or download Modern Perl for free and then recommend it to your friends, write a great review, or otherwise spread the word.

Update: After looking around I found there was no module which solved for this so I went ahead and took a stab at it: Lingua::EN::Titlecase. If you really want to do this, use that instead. It should age much better—getting updates and new features now and then—than the following snippet.

use warnings;
use strict;

my %lc = map { $_ => 1 }
    qw( the a an and or but aboard about above across after against
        along amid among around as at before behind below beneath
        beside besides between beyond but byfor from in inside into
        like minus near of off on onto opposite outside over past per
        plus regarding since than through to toward towards under
        underneath unlike until up upon versus via with within without
        v vs
        );

for my $line ( <DATA> )
{
    my $ucs = () = $line =~ /([A-Z])/g;
    my $ratio = $ucs / length($line);
    $line = lc $line if $ratio > 0.60; # too much uppercase to be real
    $line =~ s/(\b(?<!\w[[:punct:]])\w+)/$lc{lc$1} ? lc($1) : ucfirst($1)/eg;
    $line =~ s/(\A\w)/\U$1/g;
    print $line;
}

exit 0;

=pod

A module would require a user controlled list of words, usually proper
names, that are to be used canonically and perhaps some way to
tie into a dictionary file...? User should also be able to set
threshold for when to lc before processing.

=cut

__DATA__
Library of perl tools
things that are properly titled already
a ring around a rosey
nice one, McSnarky
Can't get no satisfaction
STOP SHOUTING, JACKASS
USA vs USSR
'twas the night before christmas
cold-cocked the guy with my black-jack

It’s a little naïve but it works pretty well as is.

Library of Perl Tools
Things That Are Properly Titled Already
A Ring around a Rosey
Nice One, McSnarky
Can't Get No Satisfaction
Stop Shouting, Jackass
USA vs USSR
'Twas the Night before Christmas
Cold-Cocked the Guy with My Black-Jack

Here is one, slightly different behavior, in JavaScript—JavaScript converting string to TitleCase.