Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for an equivalent to Unix's file(1) utility.

I was happy to see that something was available on CPAN:

http://search.cpan.org/~cwest/ppt-0.14/bin/file

But I'm confused. What module is to be loaded, & does file need to be imported? CPAN's normally pretty good when it comes to clear information, but I'm lost on this one.

Any clarification anyone can provide would be appreciated.

Replies are listed 'Best First'.
Re: file(1) equivalent?
by Eliya (Vicar) on Jan 11, 2012 at 20:40 UTC

    Most importantly, you need a "magic" file — typically /etc/magic (which is also used by the regular Unix file program).  It contains the patterns that are used for recognizing file types.   So if the idea is to use the Perl file utility on a non-Unix platform, don't forget to copy that magic file. (You can specify where it lives by setting the environment variable MAGIC.)

    Other than that, the utility only uses core Perl modules (FindBin, FileHandle, Getopt::Long).

    In other words, the installation steps would be

    • download the tarball
    • extract ppt-0.14/bin/file
    • get hold of a magic file (unless on Unix)

    Note that this is an executable file, not a module, but you can of course also include the code (with minor modifications) into a program of your own...

Re: file(1) equivalent?
by Corion (Patriarch) on Jan 11, 2012 at 21:14 UTC
Re: file(1) equivalent?
by toolic (Bishop) on Jan 11, 2012 at 20:18 UTC
Re: file(1) equivalent?
by mr.nick (Chaplain) on Jan 11, 2012 at 21:12 UTC
    I think the OP is looking for a callable function from inside his/her own scripts. The linked CPAN link isn't a module, but an external program.

    Which really confuses me ... why write a command-line utility that mimics another (other than for fun-n-profit)?

    However, OP, you can always wrap the functionality of that script up into a function of your own to use. Though I'm a bit surprised one doesn't already exist (I didn't search).

    Edit: How about File::Type?

    mr.nick ...

      why write a command-line utility that mimics another

      It is part of the "Perl Power Tools" / Unix Reconstruction Project — for which the README says

      "Our goal is quite simply to reimplement the classic Unix command set in pure Perl, and to have as much fun as we can doing so."

      (discontinued by now, AFAIK)

        Ha! I had totally forgotten about that project. Looks like it went dead between 2004 & 2006.

        Oh, duh the original link even had "ppt" in it and I didn't notice. Funny thing is if you go to the top level page for the project, the module I linked (File::Type) is listed there.

        mr.nick ...

      Which really confuses me ... why write a command-line utility that mimics another (other than for fun-n-profit)?

      Portability is a common reason. e.g. ExtUtils::MakeMaker reimplements some unix tools in Perl for this reason.

Re: file(1) equivalent?
by Anonymous Monk on Jan 12, 2012 at 02:22 UTC