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

I have been given the task of automating the transfer of files from one machine to another using perl. The only files I am to transfer are .eps files, though there may be other types of files in the directory. The problem is that many (almost all) of the files do not have extensions. How can I determine if a file is an eps if it does not have an extension?

Replies are listed 'Best First'.
Re: Eps Files and perl
by gellyfish (Monsignor) on Jun 17, 2003 at 16:56 UTC

    The advice to use a module that does this probably the best but if you should for some reason find yourself unable or unwilling to use or install a module you can determine whether a file is EPS because the start of the file will match the regular expression:

    /^\%!PS-Adobe-\d+.\d EPSF-\d+.\d/
    /J\
    

Re: Eps Files and perl
by benn (Vicar) on Jun 17, 2003 at 16:34 UTC
    You could try File::MMagic, which will give you a MIME type of "image/eps" if the file is such.

    Cheers, Ben.

Re: Eps Files and perl
by TVSET (Chaplain) on Jun 17, 2003 at 16:34 UTC
Re: Eps Files and perl
by Anonymous Monk on Jun 17, 2003 at 18:53 UTC
    the start of the file will match the regular expression: /^\%!PS-Adobe-\d+.\d EPSF-\d+.\d/
    This does not seem to be the case in all eps files. for instance, when saving an eps with the option to 'Include Screen Preview', the regex below will be far from the start of the file.
Re: Eps Files and perl
by hardburn (Abbot) on Jun 17, 2003 at 16:36 UTC

    You want to check the file's magic numbers. I thought there was a module that checked it, but I can't find it ATM (Video::Info::Magic uses magic numbers, but it's for video file types).

    If a module can't be found, you can always execute the magic program itself and grab its output. I usually prefer to avoid relying on an external program, but in this case you may have no choice.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Eps Files and perl
by Anonymous Monk on Jun 17, 2003 at 19:16 UTC
    File::MMagic will fail to determine an eps file type if the file is saved as mentioned above (with 'Include Screen Preview', etc..)