1File::MimeInfo::CookbooUks(e3r)Contributed Perl DocumentFaitlieo:n:MimeInfo::Cookbook(3)
2
3
4

NAME

6       File::MimeInfo::Cookbook - various code snippets
7

DESCRIPTION

9       Some code snippets for non-basic uses of the File::MimeInfo module:
10
11       Matching an extension
12           A file does not have to actually exist in order to get a mimetype
13           for it. This means that the following will work:
14
15             my $extension = '*.txt';
16             my $mimetype = mimetype( $extension );
17
18       Mimetyping an scalar
19           If you want to find the mimetype of a scalar value you need magic
20           mimetyping; after all a scalar doesn't have a filename or inode.
21           What you need to do is to use IO::Scalar :
22
23             use File::MimeInfo::Magic;
24             use IO::Scalar;
25
26             my $io_scalar = new IO::Scalar \$data;
27             my $mimetype = mimetype( $io_scalar );
28
29           In fact most other "IO::" will work as long as they support the
30           "seek()" and "read()" methods. Of course if you want really obscure
31           things to happen you can always write your own IO object and feed
32           it in there.
33
34           Be aware that when using a filehandle like this you need to set the
35           ":utf8" binmode yourself if appropriate.
36
37       Mimetyping a filehandle
38           Regrettably for non-seekable filehandles like STDIN simply using an
39           "IO::" object will not work. You will need to buffer enough of the
40           data for a proper mimetyping. For example you could mimetype data
41           from STDIN like this:
42
43             use File::MimeInfo::Magic;
44             use IO::Scalar;
45
46             my $data;
47             read(STDIN, $data, $File::MimeInfo::Magic::max_buffer);
48             my $io_scalar = new IO::Scalar \$data;
49             my $mimetype = mimetype( $io_scalar );
50
51           Be aware that when using a filehandle like this you need to set the
52           ":utf8" binmode yourself if appropriate.
53
54       Creating a new filename
55           Say you have a temporary file that you want to save with a more
56           proper filename.
57
58             use File::MimeInfo::Magic qw#mimetype extensions#;
59             use File::Copy;
60
61             my $tmpfile = '/tmp/foo';
62             my $mimetype = mimetype($tmpfile);
63             my $extension = extensions($mimetype);
64             my $newfile = 'untitled1';
65             $newfile .= '.'.$extension if length $extension;
66             move($tmpfile, $newfile);
67
68       Force the use of a certain database directory
69           Normally you just need to add the dir where your mime database
70           lives to either the XDG_DATA_HOME or XDG_DATA_DIRS environment
71           variables for it to be found. But in some rare cases you may want
72           to by-pass this system all together. Try one of the following:
73
74             @File::MimeInfo::DIRS = ('/home/me/share/mime');
75             eval 'use File::MimeInfo';
76             die if $@;
77
78           or:
79
80             use File::MimeInfo;
81             @File::MimeInfo::DIRS = ('/home/me/share/mime');
82             File::MimeInfo->rehash();
83
84           This can also be used for switching between databases at run time
85           while leaving other XDG configuration stuff alone.
86

AUTHOR

88       Jaap Karssenberg <pardus@cpan.org> Maintained by Michiel Beijen
89       <michiel.beijen@gmail.com>
90
92       Copyright (c) 2005, 2012 Jaap G Karssenberg. All rights reserved.  This
93       program is free software; you can redistribute it and/or modify it
94       under the same terms as Perl itself.
95

SEE ALSO

97       File::MimeInfo
98
99
100
101perl v5.32.0                      2020-07-28       File::MimeInfo::Cookbook(3)
Impressum