in reply to use Win2000; and alike

use Windows;

BEGIN { die "This program needs Windows" unless $^O eq 'Win32' }

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
$^O eq 'Win32' is a bad idea.
by demerphq (Chancellor) on Jun 06, 2002 at 09:53 UTC
    Argh! No! Please please dont do this!

    $^O eq 'Win32'

    Code like this is pervasive and wrong. And not only that but holds back implementing proper Win32 OS identification in future versions of perl. Currently the only real way to determine if you are on a W2k box is code like Determine Windows Type or Version. The fact is that this should _not_ be necessary. Instead Perl should know and return the correct value in $^O. But so many people have _hardcoded_ tests like you (and tadman) do in this example and in the perl distribution that loads of tests will fail if it is ever changed and we (Win32 users) all get stuck with b0rked OS reporting. (Yes I started working on getting $^O to return the correct value and decided it wasnt worth the effort changing all those tests. Shudder. They are everywhere.)

    At very minimum it makes sense to change the above to the much more flexible and forward compatible

    $^O =~/Win32/
    to determine that you are on a Win32 box. But even still this test will _not_ tell you if you are on an XP box or a W98 box or....

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      You are probably right. I have no way of testing this, but I'll start changing die if $^O eq 'Win32' to die if $^O =~ /win32|dos/i in my scripts ;) (Come to think of it, Windows.pm would allow for a nice no Windows :)

      On $^O knowing the type of Windows: if I recall correctly, $^O is hardcoded into perl, and Windows 95 programs can run under Windows 2000. I may be wrong, though.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        If you need to be specific, then it might be worth looking at some special Win32 functions.

        Here's a quote from the relevant perldoc:

        Win32::GetOSVersion()
        [CORE] Returns the array (STRING, MAJOR, MINOR, BUILD, ID), where the elements are, respectively: An arbitrary descriptive string, the major version number of the operating system, the minor version number, the build number, and a digit indicating the actual operating system. For ID, the values are 0 for Win32s, 1 for Windows 9X and 2 for Windows NT. In scalar context it returns just the ID.

        You might also be interested in this recent snippet which basically formats the output of Win32::GetOSVersion().

        - wil
        Well, most times it is sufficient to know that you are on a Win32 box irrespective of type. So yes Win9x stuff usually works ok on W2k. But not necessarily the opposite. And when using external tools, the system directories are in different locations so on occassion it can be useful to know the difference.

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

      Your "forward compatible" test may wind up more convoluted than you think. No matter what you try.

      Give people string identifiers to work with and they will screw up any notion of compatibility. Take a look at what various versions of IE report themselves as sometime to get around bad browser detect code...

        Your "forward compatible" test may wind up more convoluted than you think. No matter what you try.

        Well, I'm not so sure I follow you. For instance if $^O was changed to return "Win32 Win2000" or the like then the test should work fine. So long as the "Win32" was not excised completely what I suggested seems to be a reasonable path forward. The issue here is not with the test, but with the logic applied to deciding what should be returned. If the later is terrible then of course the former doesnt have a lot of hope. Also dont forget that deciding what $^O is at the mercy of the perl5-porters, a group that IMO has more credibility than MS with regard to this kind of thing.

        Oh I'm basing the above comments on the idea that $^O shouldnt really be hard coded. Or least shouldnt be hard coded on an MS box. Other OS's may require the hard coding, MS doesn't.

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

        Like demerphq said, IE and the code that produces its User-Agent string was written by a team that's part of a company I believe has time and again proven they care about standards more in the sense of how to trample them best. Somehow I do not think of the people behind Perl as a group with similar mindset.

        Makeshifts last the longest.

Re: Re: use Win2000; and alike
by BrotherAde (Pilgrim) on Jun 06, 2002 at 08:52 UTC

    Well, as the original author mentions, it would be nicely consistent with use 5.6.0 for Perl-versions... and anyway, TIMTOWTDI!

    BrotherAde
      Come on, you can figure it out.
      package Windows; use Carp; sub import { croak "This program needs Windows" unless ($^O eq 'Win32'); } sub unimport { croak "This program refuses to use Windows" if ($^O eq 'Win32'); } 1;