231 lines
4.4 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
File::MimeInfo::Cookbook - various code snippets
|
|
<A NAME="lbAC"> </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>"IO::"</TT> will work as long as they support the <TT>"seek()"</TT>
|
|
and <TT>"read()"</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>":utf8"</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>"IO::"</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>":utf8"</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->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"> </A>
|
|
<H2>AUTHOR</H2>
|
|
|
|
|
|
|
|
Jaap Karssenberg <<A HREF="mailto:pardus@cpan.org">pardus@cpan.org</A>>
|
|
Maintained by Michiel Beijen <<A HREF="mailto:michiel.beijen@gmail.com">michiel.beijen@gmail.com</A>>
|
|
<A NAME="lbAE"> </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"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
File::MimeInfo
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </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>
|