Boldra has asked for the wisdom of the Perl Monks concerning the following question:

When is 7.5 < 7.5?
use strict; use warnings; use Win32::OLE; my $foo = 7.5; print "foo is '$foo'\n"; my $winmgmts = Win32::OLE->GetObject("winmgmts:"); my $bar = 7.5; print "bar is '$bar'\n";
Outputs the following on one of my customers machines (perl 5.6.1/win2k):

foo is '7.5'
bar is '7,5'
There's something spooky happening to my perl environment when I call that GetObject on winmgmts. It doesn't happen for all GetObject calls, only those with winmgmts.

Can anyone tell me what is going on here? Better yet, does anyone know how to contain the strange influence of GetObject call? It's messing things up all over the place, when I change locale in the middle of a run!
Thanks for your help


- Boldra

Replies are listed 'Best First'.
Re: Win32::OLE->GetObject("winmgmts:") changes my locale
by Anonymous Monk on Apr 20, 2009 at 12:22 UTC
    can't you re-set locale after loading?
    Win32::OLE->Option(LCID => 0x0000);#LOCALE_NEUTRAL
      This doesn't seem to have any effect here. I guess this sets the locale for the OLE lookups, but my problem is that the global environment has changed

      I've discovered that LC_NUMERIC is being changed, which means that
      print "LOCALE " . setlocale(LC_NUMERIC) . "\n"; my $winmgmts = Win32::OLE->GetObject("winmgmts:"); print "LOCALE " . setlocale(LC_NUMERIC) . "\n"; setlocale(LC_NUMERIC, 'C'); print "LOCALE " . setlocale(LC_NUMERIC) . "\n";
      prints
      LOCALE C
      LOCALE German_Germany.1252
      LOCALE C
      
      Which answers most of my questions. The issues still bothering me are:
      1. What should I do to avoid this sort of thing in the future (it's already cost myself and my colleagues 3 days)
      2. What other strange global side-effects might Win32::OLE be having?


      - Boldra
        grep locale Win32-OLE-0.1709 doesn't show locale being set by default, maybe its an OLE issue (not a Win32::OLE specific)? You can test with vbs
Re: Win32::OLE->GetObject("winmgmts:") changes my locale
by ww (Archbishop) on Apr 20, 2009 at 12:27 UTC
    May be no value (or "all wet"), but output of $bar appears to be European formatting for a decimal number: is the customer's locale somewhere where that printout would be "what's expected?"
      Hi ww,

      while the machine in question is in Germany, the problem is that perls behaviour is changing at runtime as a side-effect of doing an OLE-lookup.

      This isn't what the customer wants, and certainly isn't what I want. It doesn't even match the documentation for locale (Category LC_NUMERIC: Numeric Formatting), which should only change print, and not the parsing of strings. In my case I'm also having problems because of code like the following:
      my $foo = '7.5'; # a string my $winmgmts = Win32::OLE->GetObject("winmgmts:"); #spooky action-at-a +-distance $foo < 7.5 and die("version too old"); #dies here!



      - Boldra

        Did you set your locale at the start of your program? perllocale says you should/have to declare the locale at program startup. Maybe that would prevent the locale getting set from elsewhere. Otherwise, you'll have to save/restore the locale around all calls to Win32::OLE :-/