View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Dave Rolsky > File-LibMagic-1.15 > File::LibMagic



Annotate this POD



New  1
Open  5
View/Report Bugs
Module Version: 1.15   Source   Latest Release: File-LibMagic-1.16


File::LibMagic - Determine MIME types of data or files using libmagic


version 1.15


  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);


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

Installing libmagic

On Debian/Ubuntu run:

    sudo apt-get install libmagic-dev

On Mac you can use homebrew (

    brew install libmagic

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.


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


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:


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


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

The return value is the same as that of $mime->info_from_filename().


This method returns info about the given filehandle. It will read data starting from the handle's current position, and leave the handle at that same position after reading.


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

This API uses the same constructor as the current API.

The "easy" interface

This interface is exported by:

  use File::LibMagic ':easy';

This interface exports two subroutines:

The "complete" interface

This interface is exported by:

  use File::LibMagic ':complete';

This interface exports several subroutines:


This module can throw an exception if your system runs out of memory when trying to call magic_open internally.


Please submit bugs to the CPAN RT system at or via email at


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!


This module requires file 4.x or file 5x and the associated libmagic library and headers (


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 which is encoded in XML, and is thus not the fastest approach. See 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.




This software is copyright (c) 2015 by Andreas Fitzner, Michael Hendricks, and Dave Rolsky.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: