Text::SpellChecker(3)

πŸ“¦
version 0.14 Β· 2 versions
, in fedora 43

πŸ’– Text::SpellChecker β€” the perl manual page, but make it cute πŸ’–

okay bestie, so this is the User Contributed Perl Documentation page for Text::SpellChecker, and I am obsessed with it. it's a whole little module that spell-checks a block of text for you, and yes, the version is perl v5.42.0 and the date is 2025-07-25, we'll get to the lil sign-off at the bottom. sit down, grab a strawberry πŸ“, let's go.


🌸 NAME 🌸

Text::SpellChecker β€” it's an OO interface for spell-checking a block of text. periodt. πŸ’…


✨ SYNOPSIS ✨

okay so here's the classic vibe, the command-line one:

use Text::SpellChecker;
($Text::SpellChecker::pre_hl_word,
 $Text::SpellChecker::post_hl_word) = (qw([ ]));

my $checker = Text::SpellChecker->new(text => "Foor score and seven yeers ago");

while (my $word = $checker->next_word) {
    print $checker->highlighted_text,
        "\n",
        "$word : ",
        (join "\t", @{$checker->suggestions}),
        "\nChoose a new word : ";
    chomp (my $new_word = <STDIN>);
    $checker->replace(new_word => $new_word) if $new_word;
}

print "New text : ".$checker->text."\n";

--or-- πŸ¦‹

and here's the CGI era version, for when you wanna be a web main character:

use CGI;
use Text::SpellChecker;
my $q = new CGI;
print $q->header,
      $q->start_html,
      $q->start_form(-method=>'POST',-action=>$ENV{SCRIPT_NAME});

my $checker = Text::SpellChecker->new(
    text => "Foor score and seven yeers ago",
    from_frozen => $q->param('frozen') # will be false the first time.
);

$checker->replace(new_word => $q->param('replacement'))
    if $q->param('replace');

if (my $word = $checker->next_word) {
    print $q->p($checker->highlighted_text),
        $q->br,
        qq|Next word : "$word"|,
        $q->br,
        $q->submit(-name=>'replace',-value=>'replace with:'),
        $q->popup_menu(-name=>'replacement',-values=>$checker->suggestions),
        $q->submit(-name=>'skip');
} else {
    print "Done.  New text : ".$checker->text;
}

print $q->hidden(-name => 'frozen',
                 -value => $checker->serialize,
                 -override => 1),
      $q->end_form,
      $q->end_html;

πŸ’œ DESCRIPTION πŸ’œ

so like, this module is a thin layer sitting on top of either Text::Aspell or Text::Hunspell (and it prefers the latter if it's available, no cap), and it lets you spellcheck a whole body of text. 🫢

and here's the tea: Text::(Hu|A)spell deals with words, but Text::Spellchecker deals with blocks of text. so like, we give you methods for iterating through the text, serializing the object (so it literally remembers where you left off 😭 iconic), and highlighting the current misspelled word inside the text. it's giving thoughtful.


πŸŽ€ METHODS πŸŽ€

  • $checker = Text::SpellChecker->new(text => $text, from_frozen => $serialized_data, lang => $lang, options => $options) β€” send it either the text or a serialized object in the constructor. optionally you can also pass the language of the text. if you don't pass a language, $ENV{LANG} gets used, if it's set. and if that's not set, the default language is "en_US". $options are checker-specific options (peep below πŸ‘‡).
  • $checker = new_from_frozen($serialized_data) β€” this one's separate on purpose, so that it can be overridden for alternative serialization techniques. flexible queen. πŸ‘‘
  • $str=$checker->serialize β€” represent the object in its current state. like a lil screenshot of where you are. πŸ“Έ
  • $checker->reset β€” reset the checker back to the beginning of the text, and clear the list of ignored words. fresh start energy. ✨
  • $word = $checker->next_word β€” returns the next misspelled word. the main character of the loop.
  • $checker->current_word β€” returns the most recently returned word. the one we're currently looking at. πŸ‘€
  • $checker->replace(new_word => $word) β€” replace the current word with $word.
  • $checker->ignore_all β€” ignore all subsequent occurences of the current word. bye bestie, you're free.
  • $checker->replace_all(new_word => $new_word) β€” replace all subsequent occurences of the current word with a new word. one and done.
  • $checker->suggestions β€” returns a reference to a list of alternatives to the current word in scalar context, or the list directly in list context. iykyk, context matters. πŸ’…
  • $checker->text β€” returns the current text (with the corrections you've applied already, slay).
  • $checker->highlighted_text β€” returns the text, but with the current word surrounded by $Text::SpellChecker::pre_hl_word and $Text::SpellChecker::post_hl_word. ✨ look at her glow ✨
  • $checker->set_options β€” set checker-specific options. currently only aspell supports setting options, e.g.
$checker->set_options(aspell => { "extra-dicts" => "nl" } );

🌈 CONFIGURATION OPTIONS 🌈

  • $Text::SpellChecker::pre_hl_word β€” set this to control the highlighting of a misspelled word.
  • $Text::SpellChecker::post_hl_word β€” set this to control the highlighting of a misspelled word.
  • $Text::SpellCheckerDictionaryPath{Hunspell} β€” set this to the hunspell dictionary path. by default it's /usr/share/hunspell.

this directory should have $lang.dic and $lang.aff files. that's the whole vibe check. πŸ¦‹


πŸ“œ LICENSE πŸ“œ

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 🫢


🧚 TODO 🧚

  • Add word to custom dictionary

(not me waiting for this feature 😭 it's coming, trust)


⭐ SEE ALSO ⭐

Text::Aspell, Text::Hunspell β€” go read those too bestie, they're the whole foundation this queen stands on. πŸ›οΈ


πŸ’– AUTHOR πŸ’–

Brian Duggan <bduggan@matatu.org> β€” thank you for your service, king. 🫑


and that's the whole page!! perl v5.42.0, dated 2025-07-25 β€” so like, shoutout to that exact lil footer line, no cap, we love a version stamp. mwah πŸ’‹ go spellcheck something iconic. πŸ’…βœ¨

πŸ“¦ where this page comes from