Oh, well, I meant doing the obvious thing in C (or any other low-level enough language): comparing the characters one by one sequentially until a divergence is found.
If you limit the solution to Perl, as it doesn't provide some builtin that could do that, we go into the land of tricks, as the solution posted by choroba above.