1 # $Id: CheckOS.pm,v 1.13 2007/10/04 20:15:05 drhyde Exp $
3 package Devel::CheckOS;
8 use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
12 # localising prevents the warningness leaking out of this module
13 local $^W = 1; # use warnings is a 5.6-ism
16 @EXPORT_OK = qw(os_is os_isnt die_if_os_is die_if_os_isnt die_unsupported list_platforms);
19 booleans => [qw(os_is os_isnt die_unsupported)],
20 fatal => [qw(die_if_os_is die_if_os_isnt)]
25 Devel::CheckOS - check what OS we're running on
29 Devel::CheckOS provides a more friendly interface to $^O, and also lets
30 you check for various OS "families" such as "Unix", which includes things
31 like Linux, Solaris, AIX etc.
36 print "Hey, I know this, it's a Unix system\n" if(os_is('Unix'));
40 Devel::CheckOS implements the following functions, which load subsidiary
41 OS-specific modules on demand to do the real work. They can be exported
42 by listing their names after C<use Devel::CheckOS>. You can also export
43 groups of functions thus:
45 use Devel::CheckOS qw(:booleans); # export the boolean functions
46 # and 'die_unsupported'
48 use Devel::CheckOS qw(:fatal); # export those that die on no match
50 use Devel::CheckOS qw(:all); # export everything
52 =head2 Boolean functions
56 Takes a list of OS names. If the current platform matches any of them,
57 it returns true, otherwise it returns false. The names can be a mixture
58 of OSes and OS families, eg ...
60 os_is(qw(Unix VMS)); # Unix is a family, VMS is an OS
66 foreach my $target (@targets) {
67 die("Devel::CheckOS: $target isn't a legal OS name\n")
68 unless($target =~ /^\w+$/);
69 eval "use Devel::AssertOS::$target";
72 return 1 if(&{"Devel::AssertOS::${target}::os_is"}());
80 If the current platform matches any of the parameters it returns false,
81 otherwise it returns true.
87 foreach my $target (@targets) {
88 return 0 if(os_is($target));
93 =head2 Fatal functions
97 As C<os_is()>, except that it dies instead of returning false. The die()
98 message matches what the CPAN-testers look for to determine if a module
99 doesn't support a particular platform.
104 os_is(@_) ? 1 : die_unsupported();
109 As C<os_isnt()>, except that it dies instead of returning false.
114 os_isnt(@_) ? 1 : die_unsupported();
117 =head2 And some utility functions ...
119 =head3 die_unsupported
121 This function simply dies with the message "OS unsupported", which is what
122 the CPAN testers look for to figure out whether a platform is supported or
127 sub die_unsupported { die("OS unsupported\n"); }
129 =head3 list_platforms
131 Return a list of all the platforms for which the corresponding
132 Devel::AssertOS::* module is available. This includes both OSes and OS
133 families, and both those bundled with this module and any third-party
134 add-ons you have installed.
136 Unfortunately, on some platforms this list may have file case
137 broken. eg, some platforms might return 'freebsd' instead of 'FreeBSD'.
138 This is because they have case-insensitive filesystems so things
139 should Just Work anyway.
144 eval " # only load these if needed
145 use File::Find::Rule;
150 return sort { $a cmp $b } map {
154 } File::Find::Rule->file()->name('*.pm')->in(
156 map { File::Spec->catdir($_, qw(Devel AssertOS)) }
161 =head1 PLATFORMS SUPPORTED
163 To see the list of platforms for which information is available, run this:
165 perl -MDevel::CheckOS -e 'print join(", ", Devel::CheckOS::list_platforms())'
167 Note that capitalisation is important. These are the names of the
168 underlying Devel::AssertOS::* modules
169 which do the actual platform detection, so they have to
170 be 'legal' filenames and module names, which unfortunately precludes
171 funny characters, so platforms like OS/2 are mis-spelt deliberately.
174 Also be aware that not all of them have been properly tested. I don't
175 have access to most of them and have had to work from information
176 gleaned from L<perlport> and a few other places.
178 The following OS 'families' are supported 'out of the box':
180 Apple (Mac OS, both classic and OS X)
182 MicrosoftWindows (this matches either MSWin32 or Cygwin)
186 If you want to add your own OSes or families, see L<Devel::AssertOS::Extending>
187 and please feel free to upload the results to the CPAN.
189 =head1 BUGS and FEEDBACK
191 I welcome feedback about my code, including constructive criticism.
192 Bug reports should be made using L<http://rt.cpan.org/> or by email.
194 You will need to include in your bug report the exact value of $^O, what
195 the OS is called (eg Windows Vista 64 bit Ultimate Home Edition), and,
196 if relevant, what "OS family" it should be in and who wrote it.
198 If you are feeling particularly generous you can encourage me in my
199 open source endeavours by buying me something from my wishlist:
200 L<http://www.cantrell.org.uk/david/wishlist/>
210 L<Devel::AssertOS::Extending>
214 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
216 Thanks to David Golden for the name and ideas about the interface, and
217 to the cpan-testers-discuss mailing list for prompting me to write it
220 Thanks to Ken Williams, from whose L<Module::Build> I lifted some of the
221 information about what should be in the Unix family.
223 Thanks to Billy Abbott for finding some bugs for me on VMS.
225 =head1 COPYRIGHT and LICENCE
227 Copyright 2007 David Cantrell
229 This module is free-as-in-speech software, and may be used, distributed,
230 and modified under the same conditions as perl itself.
234 This module is also free-as-in-mason software.