What is the best way to programatically determine if a Perl script is executing on a Windows based system (Win9x, WinXP, Vista, Win7, etc.)?
Fill in the blanks here:
my $running_under_windows = ... ? 1 : 0;
From perldoc perlvar
:
$OSNAME
$^O
The name of the operating system under which this copy of Perl was built, as determined during the configuration process. The value is identical to
$Config{'osname'}
. See also Config and the -V command-line switch documented in perlrun.In Windows platforms,
$^O
is not very helpful: since it is alwaysMSWin32
, it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. UseWin32::GetOSName()
orWin32::GetOSVersion()
(see Win32 and perlport ) to distinguish between the variants.
$^O eq 'MSWin32'
(来源: perlvar
页)
Use Devel::CheckOS . It handles all of the logic and special cases for you. I usually do something like:
use Devel::CheckOS qw(die_unsupported os_is);
die "You need Windows to run this program!" unless os_is('MicrosoftWindows');
The 'MicrosoftWindows' families knows about things such as Cygwin, so if you are on Windows but not at the cmd prompt, os_is()
will still give you the right answer.
This is very quick and dirty, and wouldn't bet it's 100% portable, but still useful in a pinch. Check for presence of back slashes in the PATH Env variable, since PATH is common to both Windows and Unix. So - in Perl:
if ( $ENV{PATH}=~m{\\} ) {
#Quick and dirty: It's windows!
print "It's Windows!";
} else {
print "It's Unix!";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.