Short Markovian generator

Here you can find a golfed Markovian text generator that I have written in 2003 (back when Perl was a thing!).

It is written in Perl, using the worst voodoo I could hack together to make the code short. It is not particularly obfuscated, it is only designed to be as short as possible. Even if it is far from perfect (it does not take much advantage of the "predefined variable $_", for instance), I think it is nice. It is 92 bytes long, excluding the #!/usr/bin/perl header (which is not mandatory, it only makes invoking the program easier). I have not been able to shrink it to 100 bytes (including header) yet!

Usage: startpattern inputfile1 inputfile2 inputfile3...
or startpattern < inputfile
startpattern contains the first n letters of the text which will be created (seed). It must contain a sequence of letters which exists at least once in the input files. Its length n shall be the depth of the Markovian generator, which need not be specified in any other way. You had better enclose it into quotation marks "..." so that your shell does not mess with this first parameter.
inputfiles are the text files which will be used to generate the random text
Example: "To be or n" hamlet.txt romeo-and-juliet.txt macbeth.txt
(Markovian generator and poetry do not mix well, though. Once Francesco and I tried to write a generator expressly designed to preserve (Italian) metrics, but we were never able to finish it)

Here it is:

#shortest-ever Markovian generator: (c) 2003, Federico Poloni (
$/=$s;print $s=shift;for($_=<>;@o=/\Q$s\E(.)/gs;$s=~s/^.(.*)$/$1$c/s){print $c=$o[rand(@o)]}