Algorithm::NGram is a Perl module for analyzing token sequences with n-grams. You can use it to parse a block of text, or feed in your own tokens. It can generate new sequences of tokens from what has been fed in.
my $ng = Algorithm::NGram->new(ngram_width => 3); # use trigrams
# feed in text
$ng->add_text($text1); # analyze $text1
$ng->add_text($text2); # analyze $text2
# feed in arbitrary sequence of tokens
$ng->add_tokens(qw/token1 token2 token3/);
my $output = $ng->generate_text;