File::LibMagic(3) User Contributed Perl Documentation File::LibMagic(3)

# [SYNOPSIS](#SYNOPSIS)

```
  use File::LibMagic;
  my $magic = File::LibMagic->new;
  my $info = $magic->info_from_filename('path/to/file');
  # 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('path/to/file');
  $info = $magic->info_from_string($file_content);
  open my $fh, '<', 'path/to/file' or die $!;
  $info = $magic->info_from_handle($fh);
```

# [DESCRIPTION](#DESCRIPTION)

The "File::LibMagic" module is a simple perl interface to libmagic from the file package (version 4.x or 5.x). You will need both the library (*libmagic.so*) and the header file (*magic.h*) to build this Perl module.

## [Installing libmagic](#Installing_libmagic)

On Debian/Ubuntu run:

```
    sudo apt-get install libmagic-dev
```

on Red Hat run:

```
    sudo yum install file-devel
```

On Mac you can use homebrew (https://brew.sh/):

```
    brew install libmagic
```

## [Specifying lib and/or include directories](#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:

```
    perl Makefile.PL --lib /usr/local/lib --include /usr/local/include
```

You can pass these parameters multiple times to specify more than one location.

# [API](#API)

This module provides an object-oriented API with the following methods:

## [File::LibMagic-&gt;new](#File::LibMagic)

Creates a new File::LibMagic object.

Using the object oriented interface only opens the magic database once, which is probably most efficient for repeated uses.

Each "File::LibMagic" object loads the magic database independently of other "File::LibMagic" objects, so you may want to share a single object across many modules.

This method takes the following named parameters:

- "magic\_file"
  
  This should be a string or an arrayref containing one or more magic files.
  
  If a file you provide doesn't exist the constructor will throw an exception, but only with libmagic 4.17+.
  
  If you don't set this parameter, the constructor will throw an exception if it can't find any magic files at all.
  
  Note that even if you're using a custom file, you probably *also* want to use the standard file (*/usr/share/misc/magic* on my system, yours may vary).
- "follow\_symlinks"
  
  If this is true, then calls to "$magic-&gt;info\_from\_filename" will follow symlinks to the real file.
- "uncompress"
  
  If this is true, then compressed files (such as gzip files) will be uncompressed, and the various "info\_from\_\*" methods will return info about the uncompressed file.
- Processing limits
  
  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.
  
  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.

<!--THE END-->

- "max\_indir"
  
  This limits recursion for indirection when processing entries in the magic file.
- "max\_name"
  
  This limits the maximum number of levels of name/use magic that will be processed in the magic file.
- "max\_elf\_notes"
  
  This limits the maximum number of ELF notes that will be processed when determining a file's mime type.
- "max\_elf\_phnum"
  
  This limits the maximum number of ELF program sections that will be processed when determining a file's mime type.
- "max\_elf\_shnum"
  
  This limits the maximum number of ELF sections that will be processed when determining a file's mime type.
- "max\_regex"
  
  This limits the maximum size of regexes when processing entries in the magic file.
- "max\_bytes"
  
  This limits the maximum number of bytes read from a file when determining a file's mime type.

The values of these parameters should be integer limits.

•

"max\_future\_compat"

For compatibility with future additions to the libmagic processing limit parameters, you can pass a "max\_future\_compat" 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.

## [$magic-&gt;info\_from\_filename('path/to/file')](#$magic)

This method returns info about the given file. The return value is a hash reference with four keys:

- "description"
  
  A textual description of the file content like "ASCII C program text".
- "mime\_type"
  
  The MIME type without a character encoding, like "text/x-c".
- "encoding"
  
  Just the character encoding, like "us-ascii".
- "mime\_with\_encoding"
  
  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 "mime\_type" key.

## [$magic-&gt;info\_from\_string($string)](#$magic~2)

This method returns info about the contents of the given string. The string can be passed as a reference to save memory.

The return value is the same as that of "$mime-&gt;info\_from\_filename".

## [$magic-&gt;info\_from\_handle($fh)](#$magic~3)

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.

## [File::LibMagic-&gt;max\_param\_constant](#File::LibMagic~2)

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 "max\_future\_compat" constructor parameter will work.

This may include constant values that do not have corresponding "max\_X" constructor keys if your version of libmagic is newer than the one used to build this distribution.

Conversely, if your version is older than it's possible that not all of the defined keys will be supported.

## [File::LibMagic-&gt;limit\_key\_is\_supported($key)](#File::LibMagic~3)

This method takes a processing limit key like "max\_indir" or "max\_name" and returns a boolean indicating whether the linked version of libmagic supports that processing limit.

# [DISCOURAGED APIS](#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.

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.

## [The Old OO API](#The_Old_OO_API)

This API uses the same constructor as the current API.

- $magic-&gt;checktype\_contents($data)
  
  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.
  
  This is the same value as would be returned by the "file" command with the "-i" switch.
- $magic-&gt;checktype\_filename($filename)
  
  Returns the MIME type of the given file.
  
  This is the same value as would be returned by the "file" command with the "-i" switch.
- $magic-&gt;describe\_contents($data)
  
  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.
  
  This is the same value as would be returned by the "file" command with no switches.
- $magic-&gt;describe\_filename($filename)
  
  Returns a description (as a string) of the given file.
  
  This is the same value as would be returned by the "file" command with no switches.

## [The "easy" interface](#The__easy__interface)

This interface is exported by:

```
  use File::LibMagic ':easy';
```

This interface exports two subroutines:

- MagicBuffer($data)
  
  Returns the description of a chunk of data, just like the "describe\_contents" method.
- MagicFile($filename)
  
  Returns the description of a file, just like the "describe\_filename" method.

## [The "complete" interface](#The__complete__interface)

This interface is exported by:

```
  use File::LibMagic ':complete';
```

This interface exports several subroutines:

- magic\_open($flags)
  
  This subroutine opens creates a magic handle. See the libmagic man page for a description of all the flags. These are exported by the ":complete" import.
  
  ```
    my $handle = magic_open(MAGIC_MIME);
      
  ```
- magic\_load($handle, $filename)
  
  This subroutine actually loads the magic file. The $filename argument is optional. There should be a sane default compiled into your "libmagic" library.
- magic\_buffer($handle, $data)
  
  This returns information about a chunk of data as a string. What it returns depends on the flags you passed to "magic\_open", a description, a MIME type, etc.
- magic\_file($handle, $filename)
  
  This returns information about a file as a string. What it returns depends on the flags you passed to "magic\_open", a description, a MIME type, etc.
- magic\_close($handle)
  
  Closes the magic handle.

# [EXCEPTIONS](#EXCEPTIONS)

This module can throw an exception if your system runs out of memory when trying to call "magic\_open" internally.

# [SUPPORT](#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.

# [BUGS](#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!

# [DEPENDENCIES/PREREQUISITES](#DEPENDENCIES/PREREQUISITES)

This module requires file 4.x or file 5x and the associated libmagic library and headers (https://darwinsys.com/file/).

# [RELATED MODULES](#RELATED_MODULES)

Andreas created File::LibMagic because he wanted to use libmagic (from file 4.x) File::MMagic only worked with file 3.x.

File::MimeInfo::Magic uses the magic file from freedesktop.org which is encoded in XML, and is thus not the fastest approach. See &lt;https://mail.gnome.org/archives/nautilus-list/2003-December/msg00260.html&gt; for a discussion of this issue.

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.

2026-10-26 perl v5.44.0

---

Document: File::LibMagic(3pm) from perl-file-libmagic 1.23-r7 (Chainguard OS)
Source: https://github.com/houseabsolute/File-LibMagic.git
