in reply to extracting search term context

A regular expression is all what you need, take a look at perlre. Just to give you an example see if the following code does what you want:

my $text = 'whatever you want in here'; my $n = 10; my $word = 'match'; if ( $text=~ /(([.\s]+\w+){$n}[.\s]+$word[.\s]+(\w+[.\s]+){$n})/ ) { print $1; }