{"id":7262,"date":"2022-12-20T19:36:03","date_gmt":"2022-12-20T22:36:03","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/filemimeinfocookbook-man3\/"},"modified":"2022-12-20T19:36:03","modified_gmt":"2022-12-20T22:36:03","slug":"filemimeinfocookbook-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/filemimeinfocookbook-man3\/","title":{"rendered":"File::MimeInfo::Cookbook (man3)"},"content":{"rendered":"<h1 align=\"center\">File::MimeInfo::Cookbook<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">File::MimeInfo::Cookbook \u2212 various code snippets<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Some code snippets for non-basic uses of the File::MimeInfo module: <b><br \/> Matching an extension<\/b><\/p>\n<p style=\"margin-left:17%;\">A file does not have to actually exist in order to get a mimetype for it. This means that the following will work:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">my $extension = &#8216;*.txt&#8217;; <br \/> my $mimetype = mimetype( $extension );<\/p>\n<p style=\"margin-left:11%;\"><b>Mimetyping an scalar<\/b><\/p>\n<p style=\"margin-left:17%;\">If you want to find the mimetype of a scalar value you need magic mimetyping; after all a scalar doesn\u2019t have a filename or inode. What you need to do is to use IO::Scalar :<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use File::MimeInfo::Magic; <br \/> use IO::Scalar; <br \/> my $io_scalar = new IO::Scalar $data; <br \/> my $mimetype = mimetype( $io_scalar );<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">In fact most other &#8220;IO::&#8221; will work as long as they support the &#8220;seek()&#8221; and &#8220;read()&#8221; methods. Of course if you want really obscure things to happen you can always write your own <small>IO<\/small> object and feed it in there.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Be aware that when using a filehandle like this you need to set the &#8220;:utf8&#8221; binmode yourself if appropriate.<\/p>\n<p style=\"margin-left:11%;\"><b>Mimetyping a filehandle<\/b><\/p>\n<p style=\"margin-left:17%;\">Regrettably for non-seekable filehandles like <small>STDIN<\/small> simply using an &#8220;IO::&#8221; object will not work. You will need to buffer enough of the data for a proper mimetyping. For example you could mimetype data from <small>STDIN<\/small> like this:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use File::MimeInfo::Magic; <br \/> use IO::Scalar; <br \/> my $data; <br \/> read(STDIN, $data, $File::MimeInfo::Magic::max_buffer); <br \/> my $io_scalar = new IO::Scalar $data; <br \/> my $mimetype = mimetype( $io_scalar );<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Be aware that when using a filehandle like this you need to set the &#8220;:utf8&#8221; binmode yourself if appropriate.<\/p>\n<p style=\"margin-left:11%;\"><b>Creating a new filename<\/b><\/p>\n<p style=\"margin-left:17%;\">Say you have a temporary file that you want to save with a more proper filename.<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use File::MimeInfo::Magic qw#mimetype extensions#; <br \/> use File::Copy; <br \/> my $tmpfile = &#8216;\/tmp\/foo&#8217;; <br \/> my $mimetype = mimetype($tmpfile); <br \/> my $extension = extensions($mimetype); <br \/> my $newfile = &#8216;untitled1&#8217;; <br \/> $newfile .= &#8216;.&#8217;.$extension if length $extension; <br \/> move($tmpfile, $newfile);<\/p>\n<p style=\"margin-left:11%;\"><b>Force the use of a certain database directory<\/b><\/p>\n<p style=\"margin-left:17%;\">Normally you just need to add the dir where your mime database lives to either the <small>XDG_DATA_HOME<\/small> or <small>XDG_DATA_DIRS<\/small> 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>\n<p style=\"margin-left:17%; margin-top: 1em\">@File::MimeInfo::DIRS = (&#8216;\/home\/me\/share\/mime&#8217;); <br \/> eval &#8216;use File::MimeInfo&#8217;; <br \/> die if $@;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">or:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use File::MimeInfo; <br \/> @File::MimeInfo::DIRS = (&#8216;\/home\/me\/share\/mime&#8217;); <br \/> File::MimeInfo\u2212>rehash();<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">This can also be used for switching between databases at run time while leaving other <small>XDG<\/small> configuration stuff alone.<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Jaap Karssenberg <pardus@cpan.org> Maintained by Michiel Beijen <michiel.beijen@gmail.com><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">File::MimeInfo<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  File::MimeInfo::Cookbook \u2212 various code snippets <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3587,3007],"class_list":["post-7262","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-filemimeinfocookbook","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7262"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7262\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}