use strict; # https://perlmonks.org/?node_id=11120564
my $file1 = <<END;
my $file2 = <<END;
my $marker = '***MARKER***'; # something not in either string
my $combine = "$file1$marker$file2" =~ s/(.*)\K\Q$marker\E\1//sr;
you don't need complete lines to make it work, but anchoring to line start might prove to be faster
I'd include characters below ASCII 8 to the "marker" to play safe, see also discussion surrounding the similar $;
you might be interested to check with re "debug" , how the backtracking of the .* submatch performs. I'd guess you prefer it to grow from right to left instead of shrinking from left to right. I know the regex engine can do this depending on the anchors.
I haven't checked the last point since performance might not be your biggest issue.
my $file1 = join "\n", qw( a b c d c );
my $file2 = join "\n", qw( c d c x );
my $content = "$file2\n$file1";
$content =~ /^(.*)\n.*\1$/s;
(substr $file2,0,length $1)=$file1;