<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><link rel="canonical" href="https://www.onperl.org/blog/onperl/page/stopwords_module"><title>On Perl :: Index</title><meta name="description" content="This is a site on Perl."><link rel="shortcut icon" href="/blog/onperl/page/favicon.ico"><script src="/scripts/common.js"></script><style type="text/css" media="all">@import "/styles/clean.css";</style><meta name="robots" content="follow,index,noarchive"><meta name="viewport" content="width=device-width,initial-scale=1"></head><body><div id="boundary"><a href="/"><img loading="lazy" id="mast" src="/images/onperl.png" alt="onperl"></a><div id="sidebar"><div class="navigation"><div class="link"><a href="/blog/onperl/latest/10">recent</a></div><div class="link"><a href="/blog/onperl/archive/10">archive</a></div><div class="link"><a href="/blog/onperl/page/about">about</a></div><div class="link"><a href="/feed/onperl">feed</a></div><div class="link last">etc...</div></div></div><div id="content"><h1 class="post-title">A Stopwords Module</h1><div class="post-content"><p>In a <a href="/blog/onperl/page/stopwords">recent post</a> I discussed the creation of a stopwords database. The next step was to create a more reusable perl module to access that database. This isn't terribly complex: essentially we just want to know if a particular word is in the database or not. We are, however, going to use an object oriented interface.</p><pre class="code">package Search::StopWords;

use Carp;
use strict;
use DBI;

sub new {
    my ($class, $database) = @_;
    my $self = {};
    
    unless (-e $database) { croak "$0: database file not found\n" }
    $self-&gt;{database} = $database;
    
    $self-&gt;{dbh} = DBI-&gt;connect(
        'dbi:SQLite:dbname='.$self-&gt;{database}, '', '',
        {RaiseError =&gt; 1}
    ) or croak "$0: database connection not made: $DBI::errstr\n";
    
    bless $self, $class;
}

sub has {
    my ($self, $word) = @_;
    
    my $sth = $self-&gt;{dbh}-&gt;prepare('
        SELECT count(*) FROM stopwords WHERE word = ?
    ');
    $sth-&gt;execute(lc $word);
    my @result = $sth-&gt;fetchrow_array;
    $sth-&gt;finish();
    
    return $result[0];
}

sub DESTROY {
    my ($self) = @_;
    $self-&gt;{dbh}-&gt;disconnect;
}

1;
</pre><p>Save this to a file called Search/StopWords.pm in your usual library location, then (assuming you have a stopwords database named /my/stopwords.db) you can use it like so...</p><pre class="code">
my $stopwords = new Search::StopWords('/my/stopwords.db');

my $word = 'flibbit';

unless ($stopwords-&gt;has($word)) {
    print "$word is searchable.";
}
</pre></div></div><div id="footer">© <a href="mailto:michael@mathews.net">michael mathews</a></div></div></body></html>