Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How to know the Windows OS that perl is running on

by alain_desilets (Beadle)
on Apr 28, 2011 at 17:19 UTC ( #901838=perlquestion: print w/replies, xml ) Need Help??
alain_desilets has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl app that can be run on any flavour of Windows, including Windows 7 64 bits.

This app needs to occasionally make external calls to an EXE file. Unfortunately, that EXE needs to be compiled differently for 32 vs 64 bits versions of Windows.

So, my script needs to know which version of Windows it's running on, in order to invoke either the 32 bit or the 64 bit version of the script.

I tried using the $^O variable, but it tells me MSWin32, eventhough the script is running on Win64. My guess is that the value of $^O is the name of the OS under which the perl interpreter was COMPILED, but I need the version of the OS that the perl interpreter is RUNNING on. For all sorts of reasons, we don't want to migrate the perl interpreter to 64 bit version yet.

Is there a way to tell what OS the perl interpreter is RUNNING on?


  • Comment on How to know the Windows OS that perl is running on

Replies are listed 'Best First'.
Re: How to know the Windows OS that perl is running on
by davido (Archbishop) on Apr 28, 2011 at 17:29 UTC

    perlvar does give some additional clues:



    The name of the operating system under which this copy of Perl was built, as determined during the configuration process. For examples see PLATFORMS in perlport.

    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 always MSWin32 , it doesn't tell the difference between 95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or Win32::GetOSVersion() (see Win32 and perlport) to distinguish between the variants.

    In other words...... use Win32 and specifically either GetOSName() or GetOSVersion() (depending on what you're looking for).


      THx, that does the trick.
Re: How to know the Windows OS that perl is running on
by dasgar (Priest) on Apr 28, 2011 at 17:32 UTC

    Using $^O will help you figure out if you're on Windows or Linux. If you look perlvar for the information on $^O, you'll see a note referring the reader to Win32.

    Using Win32::GetOSName will get you the version of Windows (XP, Vista, etc.). Win32::GetOSVersion will give you the version numbers. That information might not help you figure out 32-bit vs. 64-bit.

    Here's one way to help determine 32-bit vs. 64-bit. Have your code look at the top level directories of the c: drive. A 64-bit Windows will have "Program Files" and "Program Files (x86)" directories and a 32-bit Window will have only a "Program Files" directory. Might not be very elegant, but should get the job done.

Re: How to know the Windows OS that perl is running on
by Khen1950fx (Canon) on Apr 28, 2011 at 22:22 UTC
    Sys::Info::OS will detect bitness. In conjunction with Sys::Info::Base:
    package Foo; use strict; use warnings; use base qw(Sys::Info::Base); my $class = __PACKAGE__->load_subclass('Sys::Info::Driver::%s::OS'); print $class, "\n"; print $class->bitness, "\n";
Re: How to know the Windows OS that perl is running on
by mr_mischief (Monsignor) on Apr 28, 2011 at 19:34 UTC

    There are a few answers here. They tend to be much more Windows-specific than Perl-specific, but then that means they work from any language. has a batch script which uses the ver command interpreter built-in command which will tell you the version and the processor architecture. I know a command line seems archaic to some Windows users, but it can still be useful. I'm sure you could find a way to do this without the batch file based on the information provided.

    @Echo off Setlocal :: Get windows Version numbers For /f "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G) For /f "tokens=2,3,4 delims=. " %%G in ('echo %_version%') Do (set _ma +jor=%%G& set _minor=%%H& set _build=%%I) Echo Major version: %_major% Minor Version: %_minor%.%_build% if "%_major%"=="5" goto sub5 if "%_major%"=="6" goto sub6 Echo unsupported version goto:eof :sub5 ::Winxp or 2003 if "%_minor%"=="2" goto sub_2003 Echo Windows XP [%PROCESSOR_ARCHITECTURE%] goto:eof :sub_2003 Echo Windows 2003 or XP 64 bit [%PROCESSOR_ARCHITECTURE%] goto:eof :sub6 if "%_minor%"=="1" goto sub7 Echo Windows Vista or Windows 2008 [%PROCESSOR_ARCHITECTURE%] goto:eof :sub7 Echo Windows 7 or Windows 2008 R2 [%PROCESSOR_ARCHITECTURE%] goto:eof has command lists or statement dictionaries for:

    They also have other information like links to the relevant MS KB entries when those apply, a Unicode character reference, an all-JavaScript password generator, and a forum. I'm not affiliated, not even as a forum member or regular user. I do enjoy their low-bandwidth and easily read pages when I just need to look up help for a command I don't have on the system I'm using at the moment. All the stuff here that is pulled from SS64 is of course copyright SS64. The site is even Creative Commons ShareAlike (Attribution-Non-Commercial-Share Alike 2.0 UK: England & Wales), so I have a local mirror or three of the whole site for my own use.It's only 14 MB -- see what I mean about low-bandwidth? Still, of course, if you're taking more than one complete mirror mirror your first mirror to do that.

    If you can figure out all you need by parsing the environment variable %PROCESSOR_ARCHITECTURE% (which says x86 or IA64) then that'll be handy, but I think that returns x86 for 32-bit applications on 64-bit OSes. But then there's the Windows-on-Windows shim, and it has its own variable... has a howto for determining if a 32-bit program is running on 64-bit Windows by using both PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 but admits it's a bit of tickling the wizard.

    There's also the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE registry key. You can even read this from a command line of:

    reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Envir +onment" /v PROCESSOR_ARCHITECTURE

    Then there's the Microsoft Knowledge Base article on bit width of the OS: complete with its own batch file which checks the registry a different way.

    You Windows folks might want to give some love to configure and to Config so that $Config{'archname'} and $Config{'archname64'} end up being useful (if they're not now, which I assume is the case since nobody mentioned those). Having separate Win32 modules is all well and good for many things in areas in which you're trying to do complex things in ways Thompson, Kernighan, Ritchie, Pike, et al never intended. Yet I think it's not too much to ask that Perl users on Windows can tell easily what "Windows" means at the moment. Just expect the porters to not think it's too much to ask for Windows folks who care to do the work. Heck, even $^O might be made to return the information you want if you can provide the patch.

    Hey, I'm a Linux guy. Who says it's hard to find relevant information using search engine? ;-) (Okay, I admit, I was an old DOS hand part of last century, and part of knowing what to search for was based on that.)

Re: How to know the Windows OS that perl is running on
by sundialsvc4 (Abbot) on Apr 29, 2011 at 01:45 UTC

    Do you happen to have the luxury of using an installer to set up your application at each customer site?   The reason why I make that suggestion is that, if you do, then perhaps your problem has just become very easy.   Even though the situation may vary from one customer to the next, the appropriate choice will be made for you at installation time (and stored, say, in a registry-key that you require must exist).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://901838]
Approved by toolic
Front-paged by mr_mischief
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-02-21 23:40 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (288 votes). Check out past polls.