NAME
File::LibMagic - Perlwrapper for libmagic
SYNOPSIS
The easy way:
use File::LibMagic ':easy';
print MagicBuffer("Hello World\n"),"\n";
# returns "ASCII text"
print MagicFile("/bin/ls"),"\n";
# returns "ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)"
# on my system
To use all capabilities of libmagic use
use File::LibMagic ':complete';
my $handle=magic_open(0);
my $ret =magic_load($handle,""); # use default magic file
# OR $ret =magic_load($handle, '/home/someone/.magic');
print magic_buffer($handle,"Hello World\n"),"\n";
print magic_file($handle,"/bin/ls"),"\n";
magic_close($handle);
To use the object-oriented interface:
use File::LibMagic;
my $flm = File::LibMagic->new();
# determine a content description
print $flm->describe_filename('path/to/file');
print $flm->describe_contents('this is some data');
# determine the MIME type
print $flm->checktype_filename('path/to/file');
print $flm->checktype_contents('this is some data');
Please have a look at the files in the example-directory.
ABSTRACT
The File::LibMagic
is a simple perl interface to libmagic from the file-4.x package from Christos Zoulas (ftp://ftp.astron.com/pub/file/)
DESCRIPTION
The File::LibMagic
is a simple perlinterface to libmagic from the file-4.x package from Christos Zoulas (ftp://ftp.astron.com/pub/file/).
new
Create a new File::LibMagic object to use for determining the type or MIME type of content.
Using the object oriented interface provides an efficient way to repeatedly determine the magic of a file. Using the object oriented interface provides significant performance improvements over using the :easy
interface when testing many files. This performance improvement is because the loading of the magic database happens only once, during object creation.
Each File::LibMagic object loads the magic database independently of other File::LibMagic objects.
checktype_contents
Returns the MIME type of the data given as the first argument.
checktype_filename
Returns the MIME type of the given file. This will be the same as returned by the file -i
command.
describe_contents
Returns a description of the data given as the first argument.
describe_filename
Returns the MIME type of the given file. This will be the same as returned by the file
command.
EXPORT
None by default.
DIAGNOSTICS
MagicBuffer requires defined content
This exception is thrown if MagicBuffer
is called with an undefined argument.
libmagic cannot open %s
If libmagic is unable to open the file for which you want to determine the type, this exception is thrown. The exception can be thrown by MagicFile
or magic_file
. '%s' contains details about why libmagic was unable to open the file.
This exception is only thrown when using libmagic version 4.17 or later.
libmagic could not find any magic files
If libmagic is unable to find a suitable database of magic definitions, this exception is thrown. The exception can be thrown by MagicBuffer
, MagicFile
or magic_load
.
With magic_load
, you can specify the location of the magic database with the second argument. Depending on your libmagic implementation, you can often set the MAGIC environment variable to tell libmagic where to find the correct magic database.
libmagic out of memory
If libmagic is unable to allocate enough memory for its internal data structures, this exception is thrown. The exception can be thrown by MagicBuffer
, MagicFile
or magic_open
.
magic_file requires a filename
If magic_file
is called with an undefined second argument, this exception is thrown.
BUGS
I'm still learning perlxs ...
HISTORY
April 2004 initial Release
April 2005 version 0.81
Thanks to James Olin Oden (joden@lee.k12.nc.us) for his help. Thanks to Nathan Hawkins <utsl@quic.net> for his port to 64-bit systems.
June 2006 version 0.8x (x>1) Michael Hendricks started to put a lot of work into File::LibMagic.
AUTHOR
Andreas Fitzner <fitzner@informatik.hu-berlin.de>, Michael Hendricks <michael@ndrix.org>
COPYRIGHT AND LICENSE
Copyright 2005 by Andreas Fitzner
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.