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

Hello

Do you know a way to get the exact OS version of macOS (HighSierra, Sierra, and so on) from Perl?

Replies are listed 'Best First'.
Re: Get macOS Version
by marto (Cardinal) on Oct 25, 2018 at 13:17 UTC

      Oh, I missed this module!!! I will give it a try. Seems what I need.

Re: Get macOS Version
by Tux (Canon) on Oct 26, 2018 at 08:11 UTC

    That information should be easily accessible using System::Info, which should work on most (if not all) supported OS's:

    $ perl -MData::Dumper -MSystem::Info=sysinfo_hash -wE'print Dumper (sy +sinfo_hash)' $VAR1 = { 'cpu_cores' => '2', 'cpu' => 'Mac mini (2 GHz)', 'hostname' => 'myhostname.local', 'osname' => 'Darwin', 'distro' => 'Darwin 17.0.0', 'cpu_type' => 'x86_64', 'osvers' => '17.0.0', 'os' => 'darwin - 17.0.0 (Mac OS X - macOS 10.13 (17A365))', 'cpu_count' => '1 [2 cores]' };

    Enjoy, Have FUN! H.Merijn

      Hi Tux, it looks like there 'osvers' also means 'build version', rather than 'OS version,' and the string that the OP needs ("10.13") is buried in the 'os' value. Of course one could fish it out of there, but in this case I think I prefer the simplicity of a shell call: qx/sw_vers -productVersion/. If the function had to be portable, I might use the module you show, but with the parsing code needed for each OS, it seems like one might as well have conditionally executed shell calls.


      The way forward always starts with a minimal test.

        Mea culpa, mea maxima culpa for waiting so long to implement something so simple. I needed access to a Mac to test it, but it is just released as 0.059:

        mac01 $ perl -Mblib examples/sysinfo.pl -- from methods Distname : mac01.local darwin - 17.0.0 (Mac OS X - macOS 1 +0.13 (17A365)) Mac mini (2 GHz) 1 [2 cores] x86_64 Hostname : mac01.local Number of CPU's : 1 [2 cores] Processor type : x86_64 Processor description: Mac mini (2 GHz) OS and version : darwin - 17.0.0 (Mac OS X - macOS 10.13 (17A365 +)) -- from hash cpu : Mac mini (2 GHz) cpu_cores : 2 cpu_count : 1 [2 cores] cpu_type : x86_64 distro : Darwin 10.13 hostname : mac01.local os : darwin - 17.0.0 (Mac OS X - macOS 10.13 (17A365 +)) osname : Darwin osvers : 10.13

        Enjoy, Have FUN! H.Merijn
Re: Get macOS Version
by 1nickt (Canon) on Oct 25, 2018 at 13:58 UTC

    Hi, those names are just marketing brands. The version of your OS is numeric. Perl has knows what it is via the configuration with which your perl was built. The build configuration can be accessed with the Config module.

    On macOS Sierra:

    $ perl -MConfig -E 'say for @Config{qw/osname osvers/}' darwin 16.7.0

    Update: The perl Config module returns the macOS build version, not the product version, at the time Perl was built. See my follow-up node for more info.

    Hope this helps!


    The way forward always starts with a minimal test.

      True about the commercial names. What is important is 16.7.0 and so on. However, I need a way to get it independently of the OS Perl has been build on. Am am giving my users application and Perl bundled together for easier deployment. From this application with its own Perl I need to get the osvers of the macOS.

        That should be simple enough. The host OS will have Perl installed (as far as I know it has been installed by default since the first OS X). Have your app's perl find out where the system perl is (maybe a system call to which perl?) and then use that perl to examine its Config.

        Hope this helps!


        The way forward always starts with a minimal test.
        "What is important is 16.7.0 and so on."

        Are you sure about that? 16.7.0 (and darwin) refer to the Kernel Version:

        [Note: I'm using Sierra 10.12.5.]

        $ system_profiler SPSoftwareDataType Software: System Software Overview: System Version: macOS 10.12.5 (16F73) Kernel Version: Darwin 16.6.0 ...

        It sounds like you really want the System Version. If so, and you only want the version number, this might be easier:

        $ sw_vers -productVersion 10.12.5

        To get at this information, you can use backticks, qx{}, open with '-|' mode, and other methods (the open doco has links to some of these). And, of course, add appropriate error handling.

        Mac::OSVersion (already suggested by ++marto) uses both of those commands with backticks. It also provides a name() method; however, the names are hard-coded:

        my @names = qw( Cheetah Puma Jaguar Panther Tiger Leopard ) ; push @names, 'Snow Leopard', 'Lion', 'Mountain Lion', 'Mavericks', 'Yosemite', 'El Capitan', 'Sierra', 'High Sierra', 'Mojave';

        There's a potential problem with this. When Apple releases a new (named) version, that module will need to be updated, you'll need to upgrade to that updated version, and your users may need a similar upgrade.

        If you wanted to handle the names yourself, https://support.apple.com/en-us/HT201260 has a list of names and version numbers. It looks like this information is kept up-to-date: it was last updated a month ago.

        — Ken