Elegantly Determining System Architecture Within Perl

╄→尐↘猪︶ㄣ 提交于 2019-12-23 09:16:02

问题


I'm looking for a simple way to determine whether a system is 32- or 64-bit from within Perl 5. I have read the perlvar manual page backwards and forwards, and have not discovered a variable that contains the system's CPU architecture (the CPU architecture Perl was compiled for will come close enough). This is the closest I have come:

chomp (my $arch = `uname -m`);

I was wondering if there was a more elegant way of determining this; I hate relying on backtick expressions, as they are both a bottleneck, tend to be insecure, and often (this example especially) break cross-platform compatibility. There is no reason Perl shouldn't already have this information available.


回答1:


See the Config module.

Maybe checking whether $Config{'archname64'} is set would be sufficient.




回答2:


Sys::Info::OS->bitness method will determine "bitness" of your OS.




回答3:


Maybe try a CPAN module such as https://metacpan.org/pod/Devel::CheckOS .




回答4:


You could use the POSIX module which provides a uname function similar to the uname utility.

use POSIX ();

my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname;

Or, in your case :

my $arch = (POSIX::uname)[4];


来源:https://stackoverflow.com/questions/3224082/elegantly-determining-system-architecture-within-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!