gilthoniel, you should be able to figure out how to use tybalt89's code as a starting point, but stopping as soon as you have a match. If you test the length of @answers at the end of the while loop, you can stop right away.
Or you could add a similar check to GrandFather's answer — though, not having run his, I'm not positive his will run in longest-first order.
Or, if the one to four lines of code (depending on how Perl-ly you think) that would be required to exit out of the loop when there's at least one element in @answers — if that's too difficult, you could look at my answer, which already tells you the one character you need to edit in order to get it to stop at the first match rather than listing them all. Given that my answer starts with the longest possible combination, and works its way down (inspired by your own structure, btw, to try to make it as easy for you to understand as possible), the first match will be the longest (or the first of the group that are all the same longest length, if there are multiple answers of the same length). If that won't work for you, please describe what feature of the answer does not meet your requirements.