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

I have a dilemma. I need to use threads, and I also need to use Excel Constants. If I put this at the start, perl dies because it is not thread safe.
use threads; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel';
Up to this point, I was using require Win32::OLE in the sub and all worked fine.
Now, I have to make my excel file backward compatible, so I need to save it as a xlExcel8 file format. If you try to use require Win32::OLE::Const 'Microsoft Excel', you get an error. I have tried eval'ing this statement, but i get a bareword error for the xlExcel8 statement. Does anyone know of a workaround for this problem?
UPDATE:
I replaced the xlExcel8 with '56' after dumping the Microsoft Excel Constants, and it works great. Thanks everyone.

Replies are listed 'Best First'.
Re: Threads and Excel Constants
by BrowserUk (Patriarch) on Nov 13, 2008 at 00:59 UTC

    Hm Which versions of threads and Win32::OLE do you have? I get this, but no trap:

    c:\test>perl use threads; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; No type library matching "Microsoft Excel" found at - line 3 Win32::OLE(0.1702): GetOleTypeLibObject() Not a Win32::OLE::TypeLib ob +ject at c:/Perl/site/lib/Win32/OLE/Const.pm line 45. ^Z

    Theoretically, use Win32::OLE::Const 'Microsoft Excel'; is roughly equivalent to

    require Win32::OLE::Const; Win32::OLE::Const->import( 'Microsoft Excel' ); ^Z No type library matching "Microsoft Excel" found at - line 2 Win32::OLE(0.1702): GetOleTypeLibObject() Not a Win32::OLE::TypeLib ob +ject at c:/Perl/site/lib/Win32/OLE/Const.pm line 45.

    And it certainly elicits the same response. Maybe that'll work for you with your combination of versions?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Threads and Excel Constants
by runrig (Abbot) on Nov 13, 2008 at 00:18 UTC
    Huh, this works for me, but if it doesn't for you, maybe you can write the constants out as below, and read them into your main program manually:
    use threads; use Win32::OLE::Const; my $h = Win32::OLE::Const->Load('Microsoft Excel'); use Data::Dumper qw(Dumper); print Dumper $h;