| Mac-OSVersion documentation | Contained in the Mac-OSVersion distribution. |
Mac::OSVersion - Get the Mac OS X system version
use Mac::OSVersion; my $version = Mac::OSXVersion->version; # 10.4.11 my @version = Mac::OSXVersion->version; # (10, 4, 11, 'Tiger', '8.10.1' ) my $name = Mac::OSXVersion->name; # Tiger, etc. my $name = Mac::OSXVersion->minor_to_name( 3 ); 'Panther'; my $major = Mac::OSXVersion->major; # 10 of 10.4.11 my $minor = Mac::OSXVersion->minor; # 4 or 10.4.11 my $point = Mac::OSXVersion->point; # 11 of 10.4.11 my $build = Mac::OSXVersion->build; # 8R2218 my $kernel = Mac::OSXVersion->kernel; # 8.10.1
Extract the values for the various OS numbers ( Mac OS X version, build, kernel ) using various methods. Methods may use other modules or external programs. So far this only works on the current machine.
In scalar context, returns the numeric version of OS X, e.g. 10.4.11.
In list context, returns the list of the major, minor, point, name, and kernel versions. Some methods may not return all values, but the values will always be in the same positions.
# 0 1 2 3 4 5 ( $major, $minor, $point, $name, $build, $kernel );
The available methods are list in "Ways of collecting info". Use the subroutine
name as METHOD. For instance:
my @list = Mac::OSVersion->version( 'gestalt' );
Returns the list of methods that version can use to do its work.
Returns the name of major version number, e.g. 'Tiger' 10.4.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values.
Returns the name ( e.g. 'Tiger' ) for the given minor version number.
0 Cheetah 1 Puma 2 Jaguar 3 Panther 4 Tiger 5 Leopard 6 Snow Leopard
Returns a list of the minor version numbers
Returns a list of the names of the minor versions ( e.g. qw(Cheetah Puma ... )
Returns the major version number, e.g. 10 or 10.4.11.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values. Not all methods can return an answer.
Returns the major version number, e.g. 4 or 10.4.11.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values. Not all methods can return an answer.
Returns the point release version number, e.g. 11 or 10.4.11.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values. Not all methods can return an answer.
Returns the point release version number, e.g. 11 or 10.4.11.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values. Not all methods can return an answer.
Returns the kernel version number, e.g. 8.10.1.
METHOD optionally specifies the method to use to get the answer. See
version for the possible values. Not all methods can return an answer.
There isn't a single way to get all of the info that version wants to
provide, and some of the ways might give different answers for the same
installation. Not all methods can return an answer. Here's a table of
which methods return what values:
default system_profiler sw_vers gestalt uname major x x x x minor x x x x point x x x x name x x x x build x x x kernel x x x
Uses several methods to collect information.
Only uses gestaltSystemVersion from Mac::Gestalt to get the
major, minor, point, and name fields. This has the curious bug that
the point release number will not be greater than 9.
In scalar context, returns the version as "10.m.p". In list context, returns
the same list as version, although some fields may be missing.
Only uses /usr/bin/sw_vers to get the major, minor, point, build, and name
fields.
In scalar context, returns the version as "10.m.p". In list context, returns
the same list as version, although some fields may be missing.
Only uses /usr/sbin/system_profiler to get the major, minor, point,
build, and name fields.
In scalar context, returns the version as "10.m.p". In list context,
returns the same list as version, although some fields may be
missing.
Software:
System Software Overview:
System Version: Mac OS X 10.4.10 (8R2218)
Kernel Version: Darwin 8.10.1
Boot Volume: Tiger
Computer Name: macbookpro
User Name: brian d foy (brian)
Only uses uname -a to get the kernel field.
In scalar context, returns the kernel version. In list context, returns
the same list as version, although some fields may be missing.
* How does the API look if there is a Mac OS 11?
* Specify a remote machine
This module is in Github
git://github.com/briandfoy/mac-osversion.git
brian d foy, <bdfoy@cpan.org>
Copyright (c) 2007-2008, brian d foy, All Rights Reserved.
You may redistribute this under the same terms as Perl itself.
| Mac-OSVersion documentation | Contained in the Mac-OSVersion distribution. |
# $Id$ package Mac::OSVersion; use strict; use warnings; no warnings; use Carp; use subs qw(); use vars qw($VERSION); $VERSION = '0.14';
BEGIN { no strict 'refs'; my @positions = qw(MAJOR MINOR POINT NAME BUILD KERNEL); use vars ( map { "_$_" } @positions ); foreach my $index ( 0 .. $#positions ) { my $name = $positions[$index]; *{"_$name" } = sub () { $index } } } { my %methods = map { $_, 1 } qw(uname gestalt sw_vers system_profiler default); sub version { my( $class, $method ) = @_; $method ||= 'default'; croak( "$class doesn't know about method [$method]" ) unless eval { $class->can( $method ) }; my @list = $class->$method; unless( wantarray ) { return join ".", @list[0,1,2] } return @list; } sub methods { () = keys %methods } }
BEGIN { my @names = qw( Cheetah Puma Jaguar Panther Tiger Leopard ) ; push @names, 'Snow Leopard'; sub minor_to_name { $names[ $_[1] ] } sub minor_version_numbers { ( 0 .. $#names ) } sub minor_version_numbers { @names } } sub name { $_[0]->minor_to_name( ${ [ $_[0]->version( $_[1] ) ] }[_MINOR] ) }
sub major { ${ [ $_[0]->version( $_[1] ) ] }[_MAJOR] }
sub minor { ${ [ $_[0]->version( $_[1] ) ] }[_MINOR] }
sub point { ${ [ $_[0]->version( $_[1] ) ] }[_POINT] }
sub build { ${ [ $_[0]->version( $_[1] ) ] }[_BUILD] }
sub kernel { ${ [ $_[0]->version( $_[1] ) ] }[_KERNEL] }
sub default { my $class = shift; my @list; if( wantarray ) { @list = $class->system_profiler } else { scalar $class->system_profiler } }
sub gestalt { my $class = shift; eval { require Mac::Gestalt }; croak "Need to install Mac:Gestalt to use 'gestalt' method" if $@; my @list; my $key = Mac::Gestalt::gestaltSystemVersion(); #print STDERR "key is $key\n"; my $version = sprintf "%x", $Mac::Gestalt::Gestalt{$key}; my @version = $version =~ m/^(\d+)(\d)(\d)$/g; #print STDERR "Got version [@version]\n"; @list[ _MAJOR, _MINOR, _POINT ] = @version; return join ".", @list[ _MAJOR, _MINOR, _POINT ] unless wantarray; $list[_NAME] = $class->minor_to_name( $list[_MINOR] ); @list; }
BEGIN { my $command = '/usr/bin/sw_vers'; sub sw_vers { croak "Missing $command!" unless -x $command; my $class = shift; my @list = (); chomp( my $product = `$command -productVersion` ); return $product unless wantarray; chomp( my $build = `$command -buildVersion` ); ( $list[_MAJOR], $list[_MINOR], $list[_POINT] ) = split /\./, $product; $list[_BUILD] = $build; $list[_NAME] = $class->minor_to_name( $list[_MINOR] ); @list; } }
BEGIN { my $command = '/usr/sbin/system_profiler'; sub system_profiler { croak "Missing $command!" unless -x $command; my $class = shift; chomp( my $output = `$command SPSoftwareDataType` ); my @list = (); if( $output =~ m/ \s+System\ Version:\ Mac\ OS\ X\ (\d+\.\d+\.\d+)\ \((.*?)\) \s+Kernel\ Version:\ Darwin\ (\d+\.\d+\.\d+) /xm ) { return $1 unless wantarray; my( $version, $build, $kernel ) = ($1, $2, $3 ); ( $list[_MAJOR], $list[_MINOR], $list[_POINT] ) = split /\./, $version; $list[_BUILD] = $build; $list[_KERNEL] = $kernel; $list[_NAME] = $class->minor_to_name( $list[_MINOR] ); } @list; } }
BEGIN { my $command = '/usr/bin/uname'; sub uname { croak "Missing $command!" unless -x $command; my $class = shift; chomp( my $output = `$command -a` ); my @list = (); if( $output =~ /Darwin Kernel Version (\d+\.\d+\.\d+)/ ) { return $1 unless wantarray; $list[_KERNEL] = $1; } @list; } }
1;