Text::SpellChecker(3)
π 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".$optionsare 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_wordand$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
- fedora 43: perl-Text-SpellChecker 0.14-34.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms