Three small suggestions, lightly tested:

#$name =~ s/^(The|A|An) //i; $name =~ s/^(?:The|An?) //i; # no capturing #for my $word (split(/[ _-]/,$name)) { # push @abbr, substr($word,0,1); #} my @abbr = $name =~ /(?:_|\b)(\w)/g; #my $raw_abbr = $opt{periods} && $opt{periods} =~ /^[yt1]/i ? join(' +',map { $_ =~ s/$/./; $_; } @abbr) : join('',@abbr); my $raw_abbr = $opt{periods} && $opt{periods} =~ /^[yt1]/i ? join('.',@abbr) . '.' : join('', @abbr);

Update:Thinking about it a bit more, I'd do all the [yt1] testing up front, and eliminate the intermediate variables:

use strict; use warnings; my %opt = ( periods => 0, ALLCAPS => 1, HTML => 1, name => "Compact Disc read-only memory", ); print abbr(%opt); sub abbr { my %opt = @_; $opt{name} =~ s/^(?:The|An?) //i; die("Sorry...") unless $opt{name} =~ /\S/; my %ON = map { $_ => 1 } grep { $opt{$_} =~ /^[yt1]$/i } keys %opt; return ($ON{HTML} ? qq{<abbr title="$opt{name}">} : "") . ( join '', map { $_ = $ON{ALLCAPS} ? uc : $_; $_ = $ON{periods} ? "$_." : $_; } $opt{name} =~ /(?:_|\b)(\w)/g ) . ($ON{HTML} ? qq{</abbr>} : "") }

And confession! Somehow, I did not know this worked:

/(The|A|An)/;

Sadly, I've always wrapped pipes in non-capturing parens, and wrapped it all in capturing parens:

/((?:The|A|An))/;

In reply to Re: Getting abbreviations or initials by hbm
in thread Getting abbreviations or initials by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.