Text::SpellChecker - OO interface for spell-checking a block of text


    use Text::SpellChecker;
     $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, 
            "$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";


    use CGI;
    use Text::SpellChecker;
    my $q = new CGI;
    print $q->header,

    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),
            qq|Next word : "$word"|, 
            $q->submit(-name=>'replace',-value=>'replace with:'),
    } else {
        print "Done.  New text : ".$checker->text;

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


This module is a thin layer above either Text::Aspell or Text::Hunspell (preferring the latter if available), and allows one to spellcheck a body of text.

Whereas Text::(Hu|A)spell deals with words, Text::Spellchecker deals with blocks of text. For instance, we provide methods for iterating through the text, serializing the object (thus remembering where we left off), and highlighting the current misspelled word within the text.


$checker = Text::SpellChecker->new(text => $text, from_frozen => $serialized_data, lang => $lang, options => $options)

Send either the text or a serialized object to the constructor. Optionally, the language of the text can also be passed. If no language is passed, $ENV{LANG} will be used, if it is set. If it is not set, the default language will be "en_US".

$options are checker-specific options (see below).

$checker = new_from_frozen($serialized_data)

This is provided separately, so that it may be overridden for alternative serialization techniques.


Represent the object in its current state.


Reset the checker to the beginning of the text, and clear the list of ignored words.

$word = $checker->next_word

Returns the next misspelled word.


Returns the most recently returned word.

$checker->replace(new_word => $word)

Replace the current word with $word.


Ignore all subsequent occurences of the current word.

$checker->replace_all(new_word => $new_word)

Replace all subsequent occurences of the current word with a new word.


Returns a reference to a list of alternatives to the current word in a scalar context, or the list directly in a list context.


Returns the current text (with corrections that have been applied).


Returns the text, but with the current word surrounded by $Text::SpellChecker::pre_hl_word and $Text::SpellChecker::post_hl_word.


Set checker-specific options. Currently only aspell supports setting options, e.g.

    $checker->set_options(aspell => { "extra-dicts" => "nl" } );



Set this to control the highlighting of a misspelled word.


Set this to control the highlighting of a misspelled word.


Set this to the hunspell dictionary path. By default /usr/share/hunspell.

This directory should have $lang.dic and $lang.aff files.


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


Add word to custom dictionary


Text::Aspell, Text::Hunspell


Brian Duggan <>