100 likes | 203 Views
Devel::AssertOS. What platform is my code running on?. Devel::AssertOS. my($name, undef, $uid, $gid) = getpwent();. my($mode, $file_uid, $file_gid) = (stat($file))[2,4,5];. The problem. my $is_writeable = $mode & ( S_IWOTH | (($userid == $file_uid) ? S_IWUSR : 0) |
E N D
Devel::AssertOS What platform is my code running on?
Devel::AssertOS my($name, undef, $uid, $gid) = getpwent(); my($mode, $file_uid, $file_gid) = (stat($file))[2,4,5]; The problem my $is_writeable = $mode & ( S_IWOTH | (($userid == $file_uid) ? S_IWUSR : 0) | ($UIDinGID{$file_gid}{$userid} ? S_IWGRP : 0) ); my $error = `$^X $file 2>&1`;
Devel::AssertOS At the top of Makefile.PL: use lib ‘inc’; use Devel::AssertOS qw(Unix); The Solution! Your module now won’t even try to build anywhere else And the CPAN testers will shut the hell up about it
Devel::AssertOS Or maybe you need to run on something BSDish: use lib ‘inc’; use Devel::AssertOS qw(FreeBSD NetBSD OpenBSD); More Solution! Dissolves twice the code of other modules! Or you need a particular OS vendor: use lib ‘inc’; use Devel::AssertOS qw(Sun);
Devel::AssertOS Or you need a particular feature: use lib ‘inc’; use Devel::AssertOS qw(OSFeatures::POSIXShellRedirection); And some other useful tentacles! The use-devel-assertos script will do all this for you
Devel::CheckOS Take different paths through the code depending on platform use Devel::CheckOS qw(os_is); … sub get_users { if(os_is(‘MicrosoftWindows’)) { eval ‘use Win32’; … } elsif(os_is(‘Unix’)) { … … getpwent(); … } } Detecting the platform from within your code
48 modules for such a simple task!?!? But Dave, why not just use $^O?
48 modules for such a simple task!?!? Because this is how to detect Unix: sub os_is { $^O =~ /^( aix | bsdos | dgux | dragonfly | dynixptx | freebsd | linux | hpux | irix | darwin | machten | openbsd | netbsd | dec_osf | svr4 | svr5 | sco_sv | unicos | unicosmk | solaris | sunos | interix )$/x ? 1 : 0; } You don’t want to maintain this in lots of different places. You will get it wrong. But Dave, why not just use $^O?
48 modules for such a simple task!?!? Because this is how to detect Unix: sub os_is { $^O =~ /^( aix | bsdos | dgux | dragonfly | dynixptx | freebsd | linux | hpux | irix | darwin | machten | openbsd | netbsd | dec_osf | svr4 | svr5 | sco_sv | unicos | unicosmk | solaris | sunos | interix )$/x ? 1 : 0; } You don’t want to maintain this in lots of different places. You will get it wrong. But Dave, why not just use $^O? I probably have too, but at least there’s just one place to report and fix the bugs now.
See also • Devel::CheckLib (not yet finished) • Probe::Perl • The perlport manpage