Subject: | Always returns application/octet-stream when $/ (RS) is undefined |
perl v5.8.4, File::MMagic v1.22
I was having trouble with $mmagic->checktype_contents($image) returning 'application/octet-stream' in seemingly random situations.
In the end, I discovered it was because I was doing the following:
sub someSub {
local $/ = undef;
open F, $some_filename;
my $image = <F>;
close F;
my $mimetype = $mmagic->checktype_contents($image);
# ... etc
}
whereas the following code will set $mimetype correctly:
sub someSub {
my $image;
{
local $/ = undef;
open F, $some_filename;
$image = <F>;
close F;
}
my $mimetype = $mmagic->checktype_contents($image);
# ... etc
}
The problem stems back to the function readMagicEntry() in MMagic.pm, which contains two diamond (<FILEHANDLE>) operators, but doesn't localise $/ (input record separator) to its default value of "\n". I suspect (although I haven't tested it) adding the following line to the start of readMagicEntry() will fix the problem:
local $/ = "\n";
I notice there is also a diamond in the checktype_filename() function, which will problem exhibit similar strangeness if used in a program with a non-default $/.
I hope I've been helpful, and I'm sorry I don't have more time to test this thoroughly.
~ Rich Daley