.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "File::LibMagic 3"
.TH File::LibMagic 3 2025-01-11 "perl v5.40.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
File::LibMagic \- Determine MIME types of data or files using libmagic
.SH VERSION
.IX Header "VERSION"
version 1.23
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\&  use File::LibMagic;
\&
\&  my $magic = File::LibMagic\->new;
\&
\&  my $info = $magic\->info_from_filename(\*(Aqpath/to/file\*(Aq);
\&  # Prints a description like "ASCII text"
\&  print $info\->{description};
\&  # Prints a MIME type like "text/plain"
\&  print $info\->{mime_type};
\&  # Prints a character encoding like "us\-ascii"
\&  print $info\->{encoding};
\&  # Prints a MIME type with encoding like "text/plain; charset=us\-ascii"
\&  print $info\->{mime_with_encoding};
\&
\&  my $file_content = read_file(\*(Aqpath/to/file\*(Aq);
\&  $info = $magic\->info_from_string($file_content);
\&
\&  open my $fh, \*(Aq<\*(Aq, \*(Aqpath/to/file\*(Aq or die $!;
\&  $info = $magic\->info_from_handle($fh);
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
The \f(CW\*(C`File::LibMagic\*(C'\fR module is a simple perl interface to libmagic from the
file package (version 4.x or 5.x). You will need both the library
(\fIlibmagic.so\fR) and the header file (\fImagic.h\fR) to build this Perl module.
.SS "Installing libmagic"
.IX Subsection "Installing libmagic"
On Debian/Ubuntu run:
.PP
.Vb 1
\&    sudo apt\-get install libmagic\-dev
.Ve
.PP
on Red Hat run:
.PP
.Vb 1
\&    sudo yum install file\-devel
.Ve
.PP
On Mac you can use homebrew (https://brew.sh/):
.PP
.Vb 1
\&    brew install libmagic
.Ve
.SS "Specifying lib and/or include directories"
.IX Subsection "Specifying lib and/or include directories"
On some systems, you may need to pass additional lib and include directories
to the Makefile.PL. You can do this with the `\-\-lib` and `\-\-include`
parameters:
.PP
.Vb 1
\&    perl Makefile.PL \-\-lib /usr/local/lib \-\-include /usr/local/include
.Ve
.PP
You can pass these parameters multiple times to specify more than one
location.
.SH API
.IX Header "API"
This module provides an object-oriented API with the following methods:
.SS File::LibMagic\->new
.IX Subsection "File::LibMagic->new"
Creates a new File::LibMagic object.
.PP
Using the object oriented interface only opens the magic database once, which
is probably most efficient for repeated uses.
.PP
Each \f(CW\*(C`File::LibMagic\*(C'\fR object loads the magic database independently of other
\&\f(CW\*(C`File::LibMagic\*(C'\fR objects, so you may want to share a single object across
many modules.
.PP
This method takes the following named parameters:
.IP \(bu 4
\&\f(CW\*(C`magic_file\*(C'\fR
.Sp
This should be a string or an arrayref containing one or more magic files.
.Sp
If a file you provide doesn't exist the constructor will throw an exception,
but only with libmagic 4.17+.
.Sp
If you don't set this parameter, the constructor will throw an exception if it
can't find any magic files at all.
.Sp
Note that even if you're using a custom file, you probably \fIalso\fR want to use
the standard file (\fI/usr/share/misc/magic\fR on my system, yours may vary).
.IP \(bu 4
\&\f(CW\*(C`follow_symlinks\*(C'\fR
.Sp
If this is true, then calls to \f(CW\*(C`$magic\->info_from_filename\*(C'\fR will follow
symlinks to the real file.
.IP \(bu 4
\&\f(CW\*(C`uncompress\*(C'\fR
.Sp
If this is true, then compressed files (such as gzip files) will be
uncompressed, and the various \f(CW\*(C`info_from_*\*(C'\fR methods will return info
about the uncompressed file.
.IP \(bu 4
Processing limits
.Sp
Newer versions of the libmagic library have a number of limits order to
prevent malformed or malicious files from causing resource exhaustion or other
errors.
.Sp
If your libmagic support it, you can set the following limits through
constructor parameters. If your version does not support setting these limits,
passing these options will cause the constructor to croak. In addition, the
specific limits were introduced over a number of libmagic releases, and your
version of libmagic may not support every parameter. Using a parameter that is
not supported by your libmagic will also cause the constructor to cloak.
.RS 4
.IP \(bu 8
\&\f(CW\*(C`max_indir\*(C'\fR
.Sp
This limits recursion for indirection when processing entries in the
magic file.
.IP \(bu 8
\&\f(CW\*(C`max_name\*(C'\fR
.Sp
This limits the maximum number of levels of name/use magic that will be
processed in the magic file.
.IP \(bu 8
\&\f(CW\*(C`max_elf_notes\*(C'\fR
.Sp
This limits the maximum number of ELF notes that will be processed when
determining a file's mime type.
.IP \(bu 8
\&\f(CW\*(C`max_elf_phnum\*(C'\fR
.Sp
This limits the maximum number of ELF program sections that will be processed
when determining a file's mime type.
.IP \(bu 8
\&\f(CW\*(C`max_elf_shnum\*(C'\fR
.Sp
This limits the maximum number of ELF sections that will be processed when
determining a file's mime type.
.IP \(bu 8
\&\f(CW\*(C`max_regex\*(C'\fR
.Sp
This limits the maximum size of regexes when processing entries in the magic
file.
.IP \(bu 8
\&\f(CW\*(C`max_bytes\*(C'\fR
.Sp
This limits the maximum number of bytes read from a file when determining a
file's mime type.
.RE
.RS 4
.Sp
The values of these parameters should be integer limits.
.RE
.IP \(bu 4
\&\f(CW\*(C`max_future_compat\*(C'\fR
.Sp
For compatibility with future additions to the libmagic processing limit
parameters, you can pass a \f(CW\*(C`max_future_compat\*(C'\fR parameter. This is a hash
reference where the keys are constant values (integers defined by libmagic,
not names) and the values are the limit you want to set.
.ie n .SS $magic\->info_from_filename('path/to/file')
.el .SS \f(CW$magic\fP\->info_from_filename('path/to/file')
.IX Subsection "$magic->info_from_filename('path/to/file')"
This method returns info about the given file. The return value is a hash
reference with four keys:
.IP \(bu 4
\&\f(CW\*(C`description\*(C'\fR
.Sp
A textual description of the file content like "ASCII C program text".
.IP \(bu 4
\&\f(CW\*(C`mime_type\*(C'\fR
.Sp
The MIME type without a character encoding, like "text/x\-c".
.IP \(bu 4
\&\f(CW\*(C`encoding\*(C'\fR
.Sp
Just the character encoding, like "us-ascii".
.IP \(bu 4
\&\f(CW\*(C`mime_with_encoding\*(C'\fR
.Sp
The MIME type with a character encoding, like "text/x\-c;
charset=us\-ascii". Note that if no encoding was found, this will be the same
as the \f(CW\*(C`mime_type\*(C'\fR key.
.ie n .SS $magic\->info_from_string($string)
.el .SS \f(CW$magic\fP\->info_from_string($string)
.IX Subsection "$magic->info_from_string($string)"
This method returns info about the contents of the given string. The string
can be passed as a reference to save memory.
.PP
The return value is the same as that of \f(CW\*(C`$mime\->info_from_filename\*(C'\fR.
.ie n .SS $magic\->info_from_handle($fh)
.el .SS \f(CW$magic\fP\->info_from_handle($fh)
.IX Subsection "$magic->info_from_handle($fh)"
This method returns info about the contents read from the given filehandle. It
will read data starting from the handle's current position, and leave the
handle at that same position after reading.
.SS File::LibMagic\->max_param_constant
.IX Subsection "File::LibMagic->max_param_constant"
This method returns the maximum value that can be passed as a processing limit
parameter to the constructor. You can use this to determine if passing a
particular value in the \f(CW\*(C`max_future_compat\*(C'\fR constructor parameter will work.
.PP
This may include constant values that do not have corresponding \f(CW\*(C`max_X\*(C'\fR
constructor keys if your version of libmagic is newer than the one used to
build this distribution.
.PP
Conversely, if your version is older than it's possible that not all of the
defined keys will be supported.
.SS File::LibMagic\->limit_key_is_supported($key)
.IX Subsection "File::LibMagic->limit_key_is_supported($key)"
This method takes a processing limit key like \f(CW\*(C`max_indir\*(C'\fR or \f(CW\*(C`max_name\*(C'\fR and
returns a boolean indicating whether the linked version of libmagic supports
that processing limit.
.SH "DISCOURAGED APIS"
.IX Header "DISCOURAGED APIS"
This module offers two different procedural APIs based on optional exports,
the "easy" and "complete" interfaces. There is also an older OO API still
available. All of these APIs are discouraged, but will not be removed in the
near future, nor will using them cause any warnings.
.PP
I strongly recommend you use the new OO API. It's simpler than the complete
interface, more efficient than the easy interface, and more featureful than
the old OO API.
.SS "The Old OO API"
.IX Subsection "The Old OO API"
This API uses the same constructor as the current API.
.IP \(bu 4
\&\f(CW$magic\fR\->checktype_contents($data)
.Sp
Returns the MIME type of the data given as the first argument. The data can be
passed as a plain scalar or as a reference to a scalar.
.Sp
This is the same value as would be returned by the \f(CW\*(C`file\*(C'\fR command with the
\&\f(CW\*(C`\-i\*(C'\fR switch.
.IP \(bu 4
\&\f(CW$magic\fR\->checktype_filename($filename)
.Sp
Returns the MIME type of the given file.
.Sp
This is the same value as would be returned by the \f(CW\*(C`file\*(C'\fR command with the
\&\f(CW\*(C`\-i\*(C'\fR switch.
.IP \(bu 4
\&\f(CW$magic\fR\->describe_contents($data)
.Sp
Returns a description (as a string) of the data given as the first argument.
The data can be passed as a plain scalar or as a reference to a scalar.
.Sp
This is the same value as would be returned by the \f(CW\*(C`file\*(C'\fR command with no
switches.
.IP \(bu 4
\&\f(CW$magic\fR\->describe_filename($filename)
.Sp
Returns a description (as a string) of the given file.
.Sp
This is the same value as would be returned by the \f(CW\*(C`file\*(C'\fR command with no
switches.
.SS "The ""easy"" interface"
.IX Subsection "The ""easy"" interface"
This interface is exported by:
.PP
.Vb 1
\&  use File::LibMagic \*(Aq:easy\*(Aq;
.Ve
.PP
This interface exports two subroutines:
.IP \(bu 4
MagicBuffer($data)
.Sp
Returns the description of a chunk of data, just like the \f(CW\*(C`describe_contents\*(C'\fR
method.
.IP \(bu 4
MagicFile($filename)
.Sp
Returns the description of a file, just like the \f(CW\*(C`describe_filename\*(C'\fR method.
.SS "The ""complete"" interface"
.IX Subsection "The ""complete"" interface"
This interface is exported by:
.PP
.Vb 1
\&  use File::LibMagic \*(Aq:complete\*(Aq;
.Ve
.PP
This interface exports several subroutines:
.IP \(bu 4
magic_open($flags)
.Sp
This subroutine opens creates a magic handle. See the libmagic man page for a
description of all the flags. These are exported by the \f(CW\*(C`:complete\*(C'\fR import.
.Sp
.Vb 1
\&  my $handle = magic_open(MAGIC_MIME);
.Ve
.IP \(bu 4
magic_load($handle, \f(CW$filename\fR)
.Sp
This subroutine actually loads the magic file. The \f(CW$filename\fR argument is
optional. There should be a sane default compiled into your \f(CW\*(C`libmagic\*(C'\fR
library.
.IP \(bu 4
magic_buffer($handle, \f(CW$data\fR)
.Sp
This returns information about a chunk of data as a string. What it returns
depends on the flags you passed to \f(CW\*(C`magic_open\*(C'\fR, a description, a MIME type,
etc.
.IP \(bu 4
magic_file($handle, \f(CW$filename\fR)
.Sp
This returns information about a file as a string. What it returns depends on
the flags you passed to \f(CW\*(C`magic_open\*(C'\fR, a description, a MIME type, etc.
.IP \(bu 4
magic_close($handle)
.Sp
Closes the magic handle.
.SH EXCEPTIONS
.IX Header "EXCEPTIONS"
This module can throw an exception if your system runs out of memory when
trying to call \f(CW\*(C`magic_open\*(C'\fR internally.
.SH BUGS
.IX Header "BUGS"
This module is totally dependent on the version of file on your system. It's
possible that the tests will fail because of this. Please report these
failures so I can make the tests smarter. Please make sure to report the
version of file on your system as well!
.SH DEPENDENCIES/PREREQUISITES
.IX Header "DEPENDENCIES/PREREQUISITES"
This module requires file 4.x or file 5x and the associated libmagic library
and headers (https://darwinsys.com/file/).
.SH "RELATED MODULES"
.IX Header "RELATED MODULES"
Andreas created File::LibMagic because he wanted to use libmagic (from
file 4.x) File::MMagic only worked with file 3.x.
.PP
File::MimeInfo::Magic uses the magic file from freedesktop.org which is
encoded in XML, and is thus not the fastest approach. See
<https://mail.gnome.org/archives/nautilus\-list/2003\-December/msg00260.html>
for a discussion of this issue.
.PP
File::Type uses a relatively small magic file, which is directly hacked
into the module code. It is quite fast but the database is quite small
relative to the file package.
.SH SUPPORT
.IX Header "SUPPORT"
Please submit bugs to the CPAN RT system at
https://rt.cpan.org/Public/Dist/Display.html?Name=File\-LibMagic or via email at
bug\-file\-libmagic@rt.cpan.org.
.PP
Bugs may be submitted at <https://github.com/houseabsolute/File\-LibMagic/issues>.
.PP
I am also usually active on IRC as 'autarch' on \f(CW\*(C`irc://irc.perl.org\*(C'\fR.
.SH SOURCE
.IX Header "SOURCE"
The source code repository for File-LibMagic can be found at <https://github.com/houseabsolute/File\-LibMagic>.
.SH DONATIONS
.IX Header "DONATIONS"
If you'd like to thank me for the work I've done on this module, please
consider making a "donation" to me via PayPal. I spend a lot of free time
creating free software, and would appreciate any support you'd care to offer.
.PP
Please note that \fBI am not suggesting that you must do this\fR in order for me
to continue working on this particular software. I will continue to do so,
inasmuch as I have in the past, for as long as it interests me.
.PP
Similarly, a donation made in this way will probably not make me work on this
software much more, unless I get so many donations that I can consider working
on free software full time (let's all have a chuckle at that together).
.PP
To donate, log into PayPal and send money to autarch@urth.org, or use the
button at <https://www.urth.org/fs\-donation.html>.
.SH AUTHORS
.IX Header "AUTHORS"
.IP \(bu 4
Andreas Fitzner
.IP \(bu 4
Michael Hendricks <michael@ndrix.org>
.IP \(bu 4
Dave Rolsky <autarch@urth.org>
.SH CONTRIBUTORS
.IX Header "CONTRIBUTORS"
.IP \(bu 4
E. Choroba <choroba@matfyz.cz>
.IP \(bu 4
Mithun Ayachit <mayachit@amfam.com>
.IP \(bu 4
Olaf Alders <olaf@wundersolutions.com>
.IP \(bu 4
Paul Wise <pabs3@bonedaddy.net>
.IP \(bu 4
Tom Wyant <wyant@cpan.org>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 2020 by Andreas Fitzner, Michael Hendricks, Dave Rolsky, and Paul Wise.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
.PP
The full text of the license can be found in the
\&\fILICENSE\fR file included with this distribution.