1LibMagic(3) User Contributed Perl Documentation LibMagic(3)
2
3
4
6 File::LibMagic - Perlwrapper for libmagic (file-4.x or file-5.x)
7
9 The easy way:
10
11 use File::LibMagic ':easy';
12
13 print MagicBuffer("Hello World\n"),"\n";
14 # returns "ASCII text"
15
16 print MagicFile("/bin/ls"),"\n";
17 # returns "ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV)"
18 # on my system
19
20 To use all capabilities of libmagic use
21
22 use File::LibMagic ':complete';
23
24 my $handle=magic_open(0);
25 my $ret =magic_load($handle,""); # use default magic file
26 # OR $ret =magic_load($handle, '/home/someone/.magic');
27
28 print magic_buffer($handle,"Hello World\n"),"\n";
29 print magic_file($handle,"/bin/ls"),"\n";
30
31 magic_close($handle);
32
33 Using the object-oriented interface:
34
35 use File::LibMagic;
36
37 my $flm = File::LibMagic->new();
38
39 # determine a content description
40 print $flm->describe_filename('path/to/file');
41 print $flm->describe_contents('this is some data');
42
43 # determine the MIME type
44 print $flm->checktype_filename('path/to/file');
45 print $flm->checktype_contents('this is some data');
46
47 Please have a look at the files in the example-directory.
48
50 The "File::LibMagic" is a simple perl interface to libmagic from the
51 file-4.x or file-5.x package from Christos Zoulas
52 (ftp://ftp.astron.com/pub/file/).
53
55 The "File::LibMagic" is a simple perlinterface to libmagic from the
56 file-4.x or file-5.x package from Christos Zoulas
57 (ftp://ftp.astron.com/pub/file/).
58
59 You can use the simple Interface like MagicBuffer() or MagicFile(), use
60 the functions of libmagic(3) or use the OO-Interface.
61
62 Simple Interface
63 MagicBuffer()
64
65 fixme
66
67 MagicFile()
68
69 fixme
70
71 libmagic[1m(3)
72 magic_open, magic_close, magic_error, magic_file, magic_buffer,
73 magic_setflags, magic_check, magic_compile, magic_load -- Magic number
74 recognition library
75
76 OO-Interface
77 new
78
79 Create a new File::LibMagic object to use for determining the type or
80 MIME type of content.
81
82 Using the object oriented interface provides an efficient way to
83 repeatedly determine the magic of a file. Using the object oriented
84 interface provides significant performance improvements over using the
85 ":easy" interface when testing many files. This performance
86 improvement is because the loading of the magic database happens only
87 once, during object creation.
88
89 Each File::LibMagic object loads the magic database independently of
90 other File::LibMagic objects.
91
92 checktype_contents
93
94 Returns the MIME type of the data given as the first argument.
95
96 checktype_filename
97
98 Returns the MIME type of the given file. This will be the same as
99 returned by the "file -i" command.
100
101 describe_contents
102
103 Returns a description of the data given as the first argument.
104
105 describe_filename
106
107 Returns the MIME type of the given file. This will be the same as
108 returned by the "file" command.
109
110 EXPORT
111 None by default.
112
114 MagicBuffer requires defined content
115 This exception is thrown if "MagicBuffer" is called with an undefined
116 argument.
117
118 libmagic cannot open %s
119 If libmagic is unable to open the file for which you want to determine
120 the type, this exception is thrown. The exception can be thrown by
121 "MagicFile" or "magic_file". '%s' contains details about why libmagic
122 was unable to open the file.
123
124 This exception is only thrown when using libmagic version 4.17 or
125 later.
126
127 libmagic could not find any magic files
128 If libmagic is unable to find a suitable database of magic definitions,
129 this exception is thrown. The exception can be thrown by
130 "MagicBuffer", "MagicFile" or "magic_load".
131
132 With "magic_load", you can specify the location of the magic database
133 with the second argument. Depending on your libmagic implementation,
134 you can often set the MAGIC environment variable to tell libmagic where
135 to find the correct magic database.
136
137 libmagic out of memory
138 If libmagic is unable to allocate enough memory for its internal data
139 structures, this exception is thrown. The exception can be thrown by
140 "MagicBuffer", "MagicFile" or "magic_open".
141
142 magic_file requires a filename
143 If "magic_file" is called with an undefined second argument, this
144 exception is thrown.
145
147 "normalisation"-problem:
148 The results from libmagic are dependend on the (linux) distribution
149 being used. A Gentoo-Linux might return "text/plain;
150 charset=us-asci", an OpenSUSE "text/plain charset=us-asci" (no
151 semicolon!). Please check this if you run your project on a different
152 platform (and send me an mail if you see different
153 outputs/return-values).
154
155 I'm still learning perlxs ...
156 still no real error handling (printf is not enough)
157
159 This module requires these other modules and libraries:
160
161 o) file-4.x or file-5x and the associated libmagic
162 (ftp://ftp.astron.com/pub/file/)
163 o) on some systems zlib is required.
164
166 I created File::LibMagic because I wanted to use libmagic (from
167 file-4.x) and the otherwise great Module File::MMagic only works with
168 file-3.x. In file-3.x exists no libmagic but an ascii file (/etc/magic)
169 in which all data (magic numbers, etc.) is included. File::MMagic
170 parsed this ascii file at each request and is thus releativly slow.
171 Also it can not use the new data from file-4.x or file-5.x.
172
173 File::MimeInfo::Magic uses the magic file from freedesktop which is
174 encoded completely in XML, and thus not the fastest approach (
175 http://mail.gnome.org/archives/nautilus-list/2003-December/msg00260.html
176 ).
177
178 File::Type uses a relativly small magic file, which is directly hacked
179 into the module code. Thus it is quite fast. It is also mod_perl save.
180 It may be the right choice for you, but the databasis is quite small
181 relative to the file-package.
182
184 April 2004 initial Release
185
186 April 2005 version 0.81
187
188 Thanks to James Olin Oden (joden@lee.k12.nc.us) for his help. Thanks
189 to Nathan Hawkins <utsl@quic.net> for his port to 64-bit systems.
190
191 June 2006 version 0.8x (x>1) Michael Hendricks started to put a lot of
192 work into File::LibMagic.
193
194 May 2009 latest relase.
195
197 Andreas Fitzner <fitzner@informatik.hu-berlin.de>, Michael Hendricks
198 <michael@ndrix.org>
199
201 Copyright 200[5-9] by Andreas Fitzner, Michael Hendricks
202
203 This library is free software; you can redistribute it and/or modify it
204 under the same terms as Perl itself.
205
206
207
208perl v5.12.2 2009-05-23 LibMagic(3)