in reply to Get macOS Version

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.

Replies are listed 'Best First'.
Re^2: Get macOS Version
by Anonymous Monk on Oct 25, 2018 at 14:22 UTC

    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.

        It’s been almost 20 years but IIRC you are quite right. The OS X beta had the dev tools more “above the fold” than OS X currently does, again, if I’m remembering correctly. I do remember how goddamn painful getting modperl installed was. 80% my systardiness and 20% dearth of documentation on a bleeding edge OS.

        Also, thanks for the Config reminder. Just yesterday I wrote a test that should have used it but I went all qx{} on it like a dummy. :P

        Ah, so simple. Didn't thought about it! I'll give it a try, but it seems a good solution. Cheers.

      "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