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

I need to access an executables version information, specifically all the fields that appear if you click on executable-properties-version. How would I go about doing that, I tried

my $uid = (stat $filename)[4]; my $user = (getpwuid $uid)[0];

but that rightly gives me user information. Any help is highly appreciated.

Replies are listed 'Best First'.
Re: Get file version information in Perl
by ikegami (Patriarch) on May 18, 2011 at 07:12 UTC

    After two minutes of searching CPAN, I found Win32::Exe.

    use Data::Dumper qw( Dumper ); use Win32::Exe qw( ); my $exe = Win32::Exe->new('c:/windows/notepad.exe'); my $version_info = $exe->get_version_info(); { local $Data::Dumper::Useqq = 1; local $Data::Dumper::Indent = 1; print(Dumper($version_info)); }
    $VAR1 = { "FileDescription" => "Notepad", "LegalCopyright" => "\251 Microsoft Corporation. All rights reserved +.", "FileVersion" => "6.1.7600.16385", "CompanyName" => "Microsoft Corporation", "ProductVersion" => "6.1.7600.16385", "ProductName" => "Microsoft\256 Windows\256 Operating System", "OriginalFilename" => "NOTEPAD.EXE", "InternalName" => "Notepad" };
Re: Get file version information in Perl
by GrandFather (Saint) on May 18, 2011 at 08:05 UTC

    If you mean version information for a Windows Portable Executable format file (.exe, .dll, ...) then you may be interested in Win32::PEFile.

    use strict; use warnings; use Win32::PEFile; my $filename = 'c:/windows/notepad.exe'; my $pe = Win32::PEFile->new (-file => $filename); my $strings = $pe->getVersionStrings (); printf "%-20s %s\n", $_, $strings->{$_} for sort keys %$strings; my $values = $pe->getFixedVersionValues(); printf "%-20s 0x%08x\n", $_, $values->{$_} for sort keys %$values;

    Prints:

    CompanyName Microsoft Corporation FileDescription Notepad FileVersion 5.1.2600.5512 (xpsp.080413-2105) InternalName Notepad LegalCopyright © Microsoft Corporation. All rights reserved. OriginalFilename NOTEPAD.EXE ProductName Microsoft® Windows® Operating System ProductVersion 5.1.2600.5512 dwFileDateLS 0x00000000 dwFileDateMS 0x00000000 dwFileFlags 0x00000000 dwFileFlagsMask 0x0000003f dwFileOS 0x00040004 dwFileSubtype 0x00000000 dwFileType 0x00000001 dwFileVersionLS 0x0a281588 dwFileVersionMS 0x00050001 dwProductVersionLS 0x0a281588 dwProductVersionMS 0x00050001 dwSignature 0xfeef04bd dwStrucVersion 0x00010000

    This module release is very much a case of "Publish early, publish often" (although the "often" bit has been neglected). In particular, obvious error handling (like for a bad file name or path!) is absent at present.

    True laziness is hard work
Re: Get file version information in Perl
by davido (Cardinal) on May 18, 2011 at 01:13 UTC

    Does the executable have a -v (version) option available when run from the command line?

    If so, the backticks operator could capture the output. For example:

    use strict; use warnings; my $output = `perl -v`; print "Here's the version info:\n$output\n";

    Update:The question you posted to StackOverflow yielded a similar response among the meta-conversation.


    Dave

Re: Get file version information in Perl
by ikegami (Patriarch) on May 18, 2011 at 01:34 UTC
Re: Get file version information in Perl
by planetscape (Chancellor) on May 18, 2011 at 08:14 UTC
Re: Get file version information in Perl
by Khen1950fx (Canon) on May 18, 2011 at 05:57 UTC
    This is experimental. I took davido's suggestion and expanded it a little. I tested it against gnuplot and perl. It won't work on all executables, so you'll need to play with it.
    #!/usr/bin/perl use strict; use warnings; use SVN::Notify; my $exe = SVN::Notify->find_exe('gnuplot'); system("$exe -V");

      Using -v, -V or --version doesn't work at all for .dll files, and I didn't find any .exe for which that worked.

      Even for programs that do recognise one of these, the option causes far more than the version to be returned.

        It doesn't even work for perl, not that all versions of perl include such information
        $ sigcheck -a -h -e -i -q C:\perl\5.12.2\bin\MSWin32-x86-multi-thread\ +perl.exe c:\perl\5.12.2\bin\mswin32-x86-multi-thread\perl.exe: Verified: Unsigned File date: 5:11 AM 10/16/2010 Publisher: n/a Description: n/a Product: n/a Version: n/a File version: n/a Strong Name: Unsigned Original Name: n/a Internal Name: n/a Copyright: n/a Comments: n/a MD5: 8d75c8a4ca154a3c5a55d0db5f45ad83 SHA1: fda7cb1ffa77e1497a6e6b372cec065240aabfb7 SHA256: 062b62f5981aee65d63d16c140f558f6aa8d06d192b7449b21b3e0 +7eb5196a0b $ perl -V |grep 2010 Compiled at Oct 16 2010 05:10:19
      s/experimental/wishful/