.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 3pm" .TH File::LibMagic 3pm "2018-11-01" "perl v5.28.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.16 .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 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 (http://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 \s-1API\s0 with the following methods: .SS "File::LibMagic\->\fBnew()\fP" .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 magic_file .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 follow_symlinks .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 uncompress .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. .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 description .Sp A textual description of the file content like \*(L"\s-1ASCII C\s0 program text\*(R". .IP "\(bu" 4 mime_type .Sp The \s-1MIME\s0 type without a character encoding, like \*(L"text/x\-c\*(R". .IP "\(bu" 4 encoding .Sp Just the character encoding, like \*(L"us-ascii\*(R". .IP "\(bu" 4 mime_with_encoding .Sp The \s-1MIME\s0 type with a character encoding, like \*(L"text/x\-c; charset=us\-ascii\*(R". 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 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 given filehandle. It will read data starting from the handle's current position, and leave the handle at that same position after reading. .SH "DISCOURAGED APIS" .IX Header "DISCOURAGED APIS" This module offers two different procedural APIs based on optional exports, the \*(L"easy\*(R" and \*(L"complete\*(R" interfaces. There is also an older \s-1OO API\s0 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 \s-1OO API.\s0 It's simpler than the complete interface, more efficient than the easy interface, and more featureful than the old \s-1OO API.\s0 .SS "The Old \s-1OO API\s0" .IX Subsection "The Old OO API" This \s-1API\s0 uses the same constructor as the current \s-1API.\s0 .IP "\(bu" 4 \&\f(CW$magic\fR\->checktype_contents($data) .Sp Returns the \s-1MIME\s0 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 \s-1MIME\s0 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. .ie n .SS "The ""easy"" interface" .el .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. .ie n .SS "The ""complete"" interface" .el .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 \s-1MIME\s0 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 \s-1MIME\s0 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 (http://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 \s-1XML,\s0 and is thus not the fastest approach. See 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 \s-1CPAN RT\s0 system at http://rt.cpan.org/NoAuth/Bugs.html?Dist=File\-LibMagic or via email at bug\-file\-libmagic@rt.cpan.org. .PP Bugs may be submitted at or via email to bug\-file::libmagic@rt.cpan.org . .PP I am also usually active on \s-1IRC\s0 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 . .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 \*(L"donation\*(R" 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 . .SH "AUTHORS" .IX Header "AUTHORS" .IP "\(bu" 4 Andreas Fitzner .IP "\(bu" 4 Michael Hendricks .IP "\(bu" 4 Dave Rolsky .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 E. Choroba .IP "\(bu" 4 Mithun Ayachit .IP "\(bu" 4 Olaf Alders .IP "\(bu" 4 Tom Wyant .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2017 by Andreas Fitzner, Michael Hendricks, and Dave Rolsky. .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 \&\fI\s-1LICENSE\s0\fR file included with this distribution.