This is driving me crazy, and I'm hoping someone can offer up a good alternative.

My problem: I have a perl script that is a test harness. It starts up some clients using fork/exec and then does stuff. This much all works great. Now there is a new requirement. I need to start a matlab RMI server, which is essentially just calling java with the matlab class. The problem is there isn't a graceful way to shutdown the matlab server, you have to kill it. Perl under windows doesn't return a true pid when it forks, and it changes upon exec anyway. My solution was to use Win32::OLE and access the process info. Since it's possible there will be other java processes running I tried to find the pid's of all the java.exe processes and save them. I then fork/exec the rmi server, and look again to find the new java process. However if you have Win32::OLE in your script and fork it throws an unhandled exception. There was a bug filed against Active perl about this back in 2005, but it's clearly not fixed yet.

I tried various methods of load win32::ole, do my thing, unload it, and load it again for the second pid scrape, but its just not working.

Here's the most recent iteration of the code:

sub scrape_java_pids { use Win32::OLE; my $CLASS = "winmgmts:{impersonationLevel=impersonate}\\\\.\\Root\\c +imv2"; my $WMI = Win32::OLE->GetObject( $CLASS ) || die "Error - failed to +instantiate Win32::OLE object: $!\n"; my %jpid; foreach my $Proc ( $WMI->ExecQuery("SELECT * FROM Win32_Process WHER +E Name = 'java.exe'") ) { $jpid{ $Proc->{ProcessID} } = 1; } Symbol::delete_package("Win32::OLE"); # fork & win32::ole don't pla +y nicely. delete $INC{'Win32/OLE.pm'}; $WMI=""; return %jpid; }
There has got to be a better way to do this.

In reply to Windows + Win32::OLE + fork = Hell by UnstoppableDrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.