As everybody surely knows, the "file" command in bash prints info about the type of a file. (i.e file flower.jpg will show that this is a jpeg image)

I'm trying to achieve the same in a perl script. Thus I wrote something like this:

use strict; use File::Type; my $infile = "flower.jpg"; my $ft = File::Type->new(); my $type1 = $ft->checktype_filename($infile); my $type2 = $ft->mime_type($infile); print "$type1\n"; print "$type2\n"; print `file $infile`;

and my proto-script returns

image/jpeg image/jpeg flower.jpg: JPEG image data, JFIF standard 1.01

Thus we have a clear winner, the File::Type module, although not bad, is clearly less accurate that file. And that's the question: żDo you know any way/module/function integrated with perl to obtain the same amount of info about a file? or I need to use system here to call directly the file command?. The big nuisance here is that if you search for file + perl in google you obtain millions of unrelated results


In reply to file command replacement (showing the type of a file with perl) by pvaldes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.