You don't show any code, but in general it's impossible to tell what endianness a file is unless you know what values you should be reading from the file. Many file formats, for instance, reserve a few bytes at the beginning to indicate what the format is, and if this applies to the data you are dealing with then you can just read
those bytes, and change the open mode if you don't get what you're expecting
Alternatively, since your program dies if the wrong format is chosen, then you can use that to test whether the chosen format is correct. Something like this should suit
my $file = $ARGV[0];
open my $fh, '<:raw:encoding(UTF-16LE):crlf', $file or die $!;
eval { do_stuff_that_may_crash() };
if ( $@ ) {
if ( $@ =~ /Malformed HI surrogate/ ) {
open my $fh, '<:raw:encoding(UTF-16BE):crlf', $file or die $!;
do_stuff_that_may_crash();
}
else {
die $@;
}
}
but since it sounds like do_stuff_that_may_crash()
is pretty much all of your program, you should probably find a better criterion