|
NAMEFile::LibMagic - Determine MIME types of data or files using libmagicVERSIONversion 1.23SYNOPSISuse 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); DESCRIPTIONThe "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 libmagicOn 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 directoriesOn 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. APIThis module provides an object-oriented API with the following methods:File::LibMagic->newCreates 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:
The values of these parameters should be integer limits.
$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:
$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.The return value is the same as that of "$mime->info_from_filename". $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.File::LibMagic->max_param_constantThis 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->limit_key_is_supported($key)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 APISThis 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 APIThis API uses the same constructor as the current API.
The "easy" interfaceThis interface is exported by:use File::LibMagic ':easy'; This interface exports two subroutines:
The "complete" interfaceThis interface is exported by:use File::LibMagic ':complete'; This interface exports several subroutines:
EXCEPTIONSThis module can throw an exception if your system runs out of memory when trying to call "magic_open" internally.BUGSThis 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/PREREQUISITESThis module requires file 4.x or file 5x and the associated libmagic library and headers (https://darwinsys.com/file/).RELATED MODULESAndreas 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 <https://mail.gnome.org/archives/nautilus-list/2003-December/msg00260.html> 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. SUPPORTPlease 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 may be submitted at <https://github.com/houseabsolute/File-LibMagic/issues>. I am also usually active on IRC as 'autarch' on "irc://irc.perl.org". SOURCEThe source code repository for File-LibMagic can be found at <https://github.com/houseabsolute/File-LibMagic>.DONATIONSIf 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.Please note that I am not suggesting that you must do this 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. 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). To donate, log into PayPal and send money to autarch@urth.org, or use the button at <https://www.urth.org/fs-donation.html>. AUTHORS
CONTRIBUTORS
COPYRIGHT AND LICENSEThis software is copyright (c) 2020 by Andreas Fitzner, Michael Hendricks, Dave Rolsky, and Paul Wise.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. The full text of the license can be found in the LICENSE file included with this distribution.
Visit the GSP FreeBSD Man Page Interface. |