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