man-pages/man3/File::MimeInfo::Cookbook.3pm.html
2021-03-31 01:06:50 +01:00

231 lines
4.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of File::MimeInfo::Cookbook</TITLE>
</HEAD><BODY>
<H1>File::MimeInfo::Cookbook</H1>
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2018-08-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
File::MimeInfo::Cookbook - various code snippets
<A NAME="lbAC">&nbsp;</A>
<H2>DESCRIPTION</H2>
Some code snippets for non-basic uses of the File::MimeInfo
module:
<DL COMPACT>
<DT id="1"><B>Matching an extension</B><DD>
A file does not have to actually exist in order to get a
mimetype for it. This means that the following will work:
<P>
<PRE>
my $extension = '*.txt';
my $mimetype = mimetype( $extension );
</PRE>
<DT id="2"><B>Mimetyping an scalar</B><DD>
If you want to find the mimetype of a scalar value you need magic
mimetyping; after all a scalar doesn't have a filename or inode.
What you need to do is to use IO::Scalar :
<P>
<PRE>
use File::MimeInfo::Magic;
use IO::Scalar;
my $io_scalar = new IO::Scalar \$data;
my $mimetype = mimetype( $io_scalar );
</PRE>
<P>
In fact most other <TT>&quot;IO::&quot;</TT> will work as long as they support the <TT>&quot;seek()&quot;</TT>
and <TT>&quot;read()&quot;</TT> methods. Of course if you want really obscure things to
happen you can always write your own <FONT SIZE="-1">IO</FONT> object and feed it in there.
<P>
Be aware that when using a filehandle like this you need to set the <TT>&quot;:utf8&quot;</TT>
binmode yourself if appropriate.
<DT id="3"><B>Mimetyping a filehandle</B><DD>
Regrettably for non-seekable filehandles like <FONT SIZE="-1">STDIN</FONT> simply using an <TT>&quot;IO::&quot;</TT>
object will not work. You will need to buffer enough of the data for a proper
mimetyping. For example you could mimetype data from <FONT SIZE="-1">STDIN</FONT> like this:
<P>
<PRE>
use File::MimeInfo::Magic;
use IO::Scalar;
my $data;
read(STDIN, $data, $File::MimeInfo::Magic::max_buffer);
my $io_scalar = new IO::Scalar \$data;
my $mimetype = mimetype( $io_scalar );
</PRE>
<P>
Be aware that when using a filehandle like this you need to set the <TT>&quot;:utf8&quot;</TT>
binmode yourself if appropriate.
<DT id="4"><B>Creating a new filename</B><DD>
Say you have a temporary file that you want to save with a more
proper filename.
<P>
<PRE>
use File::MimeInfo::Magic qw#mimetype extensions#;
use File::Copy;
my $tmpfile = '/tmp/foo';
my $mimetype = mimetype($tmpfile);
my $extension = extensions($mimetype);
my $newfile = 'untitled1';
$newfile .= '.'.$extension if length $extension;
move($tmpfile, $newfile);
</PRE>
<DT id="5"><B>Force the use of a certain database directory</B><DD>
Normally you just need to add the dir where your mime database lives
to either the <FONT SIZE="-1">XDG_DATA_HOME</FONT> or <FONT SIZE="-1">XDG_DATA_DIRS</FONT> environment variables
for it to be found. But in some rare cases you may want to by-pass
this system all together. Try one of the following:
<P>
<PRE>
@File::MimeInfo::DIRS = ('/home/me/share/mime');
eval 'use File::MimeInfo';
die if $@;
</PRE>
<P>
or:
<P>
<PRE>
use File::MimeInfo;
@File::MimeInfo::DIRS = ('/home/me/share/mime');
File::MimeInfo-&gt;rehash();
</PRE>
<P>
This can also be used for switching between databases at run time
while leaving other <FONT SIZE="-1">XDG</FONT> configuration stuff alone.
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>AUTHOR</H2>
Jaap Karssenberg &lt;<A HREF="mailto:pardus@cpan.org">pardus@cpan.org</A>&gt;
Maintained by Michiel Beijen &lt;<A HREF="mailto:michiel.beijen@gmail.com">michiel.beijen@gmail.com</A>&gt;
<A NAME="lbAE">&nbsp;</A>
<H2>COPYRIGHT</H2>
Copyright (c) 2005, 2012 Jaap G Karssenberg. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
<A NAME="lbAF">&nbsp;</A>
<H2>SEE ALSO</H2>
File::MimeInfo
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAD">AUTHOR</A><DD>
<DT id="9"><A HREF="#lbAE">COPYRIGHT</A><DD>
<DT id="10"><A HREF="#lbAF">SEE ALSO</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:43 GMT, March 31, 2021
</BODY>
</HTML>