package WordMatch; sub new { my $class = shift; $class = ref($class) || $class; my $word = shift; # word to match my $self = qr/\[$word\]/; bless($self, $class); return $self; } sub match { my $self = shift; my $string = shift; return $string =~ $self ? "Matches!" : "Doesn't match!"; } package main; my $wm = WordMatch->new("hi"); print $wm->match("[hi] how are you?"), "\n"; print $wm->match("hi how are you?"), "\n";