1MimeInfo::Cookbook(3) User Contributed Perl DocumentationMimeInfo::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       Mimetyping a filehandle
35           Regrettably for non-seekable filehandles like STDIN simply using an
36           IO:: object will not work. You will need to buffer enough of the
37           data for a proper mimetyping. For example you could mimetype data
38           from STDIN like this:
39
40             use File::MimeInfo::Magic;
41             use IO::Scalar;
42
43             my $data;
44             read(STDIN, $data, $File::MimeInfo::Magic::max_buffer);
45             my $io_scalar = new IO::Scalar \$data;
46             my $mimetype = mimetype( $io_scalar );
47
48       Creating a new filename
49           Say you have a temporary file that you want to save with a more
50           proper filename.
51
52             use File::MimeInfo::Magic qw#mimetype extensions#;
53             use File::Copy;
54
55             my $tmpfile = '/tmp/foo';
56             my $mimetype = mimetype($tmpfile);
57             my $extension = extensions($mimetype);
58             my $newfile = 'untitled1';
59             $newfile .= '.'.$extension if length $extension;
60             move($tmpfile, $newfile);
61
62       Force the use of a certain database directory
63           Normally you just need to add the dir where your mime database
64           lives to either the XDG_DATA_HOME or XDG_DATA_DIRS environment
65           variables for it to be found. But in some rare cases you may want
66           to by-pass this system all together. Try one of the following:
67
68             @File::MimeInfo::DIRS = ('/home/me/share/mime');
69             eval 'use File::MimeInfo';
70             die if $@;
71
72           or:
73
74             use File::MimeInfo;
75             @File::MimeInfo::DIRS = ('/home/me/share/mime');
76             File::MimeInfo->rehash();
77
78           This can also be used for switching between databases at run time
79           while leaving other XDG configuration stuff alone.
80

AUTHOR

82       Jaap Karssenberg (Pardus) <pardus@cpan.org>
83
84       Copyright (c) 2005 Jaap G Karssenberg. All rights reserved.  This pro‐
85       gram is free software; you can redistribute it and/or modify it under
86       the same terms as Perl itself.
87

SEE ALSO

89       File::MimeInfo
90
91
92
93perl v5.8.8                       2005-10-03             MimeInfo::Cookbook(3)
Impressum