http://qs1969.pair.com?node_id=508516

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

The following returns the longest substring that is common to two given strings and starts at the beginning of each, and the remaining characters in each of the arguments.

sub common { if ( $_[0] eq $_[1] ) { return( $_[0], "", "" ); } else { use bytes; my ( $pos, $len0, $len1 ) = ( 0, length($_[0]), length($_[1]) ); # find the offset of the first byte that differs while ( vec($_[0], $pos, 8) == vec($_[1], $pos, 8) ) { $pos++; last unless ( $pos < $len0 && $pos < $len1 ); } return( unpack("a${pos}", $_[0]), unpack("x${pos}a*", $_[0]), unpack("x${pos}a*", $_[1]), ); } }

Obviously, this looks more like C than Perl. Is there any other way to achieve these (and similar) results without using bytes that is efficient?

Thanks!