File::MMagic::XS(3pm)
π File::MMagic::XS π
okay bestie, gather round, because we're talking about File::MMagic::XS β the Perl manual page you're reading right now is the one for this module, and it's giving "guess that file type, but make it fast" β¨
πΈ NAME πΈ
File::MMagic::XS β guess the file type with XS, a la mod_mime_magic. no cap, that's literally the whole gig π
β¨ SYNOPSIS β¨
use File::MMagic::XS;
my $m = File::MMagic::XS->new();
$m = File::MMagic::XS->new('/etc/magic'); # use external magic file
my $mime = $m->get_mime($file);
# use File::MMagic compatible interface
use File::MMagic::XS qw(:compat);
my $m = File::MMagic::XS->new();
$m->checktype_filename($file);
π¦ DESCRIPTION π¦
So, this is a port of Apache2's mod_mime_magic.c into Perl, and it's written in XS because the whole goal is being efficient and fast β especially for apps that are gonna be running for a long, long time. longevity queen π
There's also a compatibility layer for File::MMagic. all you gotta do is specify :compat when you import the module:
use File::MMagic::XS qw(:compat);
and then boom, these methods are available from File::MMagic::XS:
checktype_filename
checktype_filehandle
checktype_contents
addMagicEntry
Real talk though: currently this software is in beta. π«£ So if you've got suggestions or recommendations about the interface, or about literally anything else, now is your chance to send them! speak now bestie π£
π METHODS π
π new(%args)
Makes you a brand new File::MMagic::XS object. fresh out the box, main character energy.
If you pass in the "file" argument, then File::MMagic::XS will load its magic definitions from that specified file. If you don't specify it, it'll use the magic file that gets installed under the File/MMagic/ directory. defaults, we love her π
π clone()
Clones an existing File::MMagic::XS object. yes, she has a twin now. iconic behavior.
π parse_magic_file($file)
Read and parse a magic file, as used by Apache2. reading the sacred texts fr fr π
π get_mime($file)
Inspects the file specified by $file and returns a MIME type if it possibly can. If no matching MIME type is found, then undef is returned. sad girl hours if that happens π
π fsmagic($file)
Inspects a file and returns a MIME type using inode information only. the contents of the file are not inspected β nope, not even peeked at π
π fhmagic($fh)
Inspects a file handle and returns a mime string by reading the contents of the file handle. okay so this one does read π
π ascmagic($file)
Inspects a piece of data (assuming it's not binary data) and attempts to determine the file type.
π bufmagic($scalar)
Inspects a scalar buffer and attempts to determine the file type.
π add_magic($magic_line)
Adds a new magic entry to the object. The format of $magic_line is the same as the magic(5) file. This lets you add custom magic entries at run time β build-your-own-detector, we stan a customizable queen π
π add_file_ext($ext, $mime)
Adds a new file extension to MIME mapping. This gets used as a fallback method for determining MIME types.
my $magic = File::MMagic::XS->new;
$magic->add_file_ext('t', 'text/perl-test');
my $mime = $magic->get_mime('t/01-sanity.t');
This will make get_mime() return 'text/perl-test'. and honestly? slay.
π error()
Returns the last error string. your little receipts of what went wrong π§Ύ
π₯ PERFORMANCE π₯
bestie. look at this. this is on the author's laptop (MacBook, Core 2 Duo / Mac OS X 10.4.3), tested against File::MMagic::XS 0.09003:
Rate perl xs
perl 513/s -- -96%
xs 12048/s 2249% --
the way that xs is doing 2249%... not me being obsessed. periodt. π
π SEE ALSO π
File::MMagic
π AUTHOR π
Copyright 2005-2007 Daisuke Maki <daisuke@endeworks.jp>.
Underlying software: Copyright 1999-2004 The Apache Software Foundation, Copyright (c) 1996-1997 Cisco Systems, Inc., Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
π LICENSE π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
and that's the whole page bestie π this version of the page shipped with perl v5.42.3, dated 2026-08-19, under the manual name File::MMagic::XS(3pm) β and the original's little footer signature reminds us the man page itself is File::MMagic::XS(3pm) from User Contributed Perl Documentation, so go forth and detect file types, you gorgeous genius π«Άβ¨
π¦ where this page comes from
- ubuntu stonking: libfile-mmagic-xs-perl 0.09008-5build1 Β· Artistic-1.0-Perl OR GPL-1+, AND Apache-2.0 AND other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms