Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

cross platform - How can I detect the operating system in Perl?

I have Perl on Mac, Windows and Ubuntu. How can I tell from within the script which one is which? Thanks in advance.

Edit: I was asked what I am doing. It is a script, part of our cross-platform build system. The script recurses directories and figures out what files to build. Some files are platform-specific, and thus, on Linux I don't want to build the files ending with _win.cpp, etc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Examine the $^O variable which will contain the name of the operating system:

print "$^O
";

Which prints linux on Linux and MSWin32 on Windows.

You can also refer to this variable by the name $OSNAME if you use the English module:

use English qw' -no_match_vars ';
print "$OSNAME
";

According to perlport, $^O will be darwin on Mac OS X.


You can also use the Config core module, which can provide the same information (and a lot more):

use Config;

print "$Config{osname}
";
print "$Config{archname}
";

Which on my Ubuntu machine prints:

linux
i486-linux-gnu-thread-multi

Note that this information is based on the system that Perl was built, which is not necessarily the system Perl is currently running on (the same is true for $^O and $OSNAME); the OS won't likely be different but some information, like the architecture name, may very well be.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...