http://www.perlmonks.org?node_id=1212736

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

Perl Monks,
I seek your knowledge and wisdom again.

This time pertaining to enumerating WIN32::OLE response.

The issue is finding a timeout/hung session when retrieving
the win32 information (wmic) from a remote machine.

The code below is step by step, the failure does not occur until near the
bottom. I included this code for FYI.
eval { local $SIG{ALRM} = sub { warn "ALARM FOR GET WMIC L: \"" . __LINE_ +_ . "\" F: \"" . __FILE__ . "\"\n" }; # NB: \n required alarm 10; $wmicon = Win32::OLE->GetObject('winmgmts:' . '{impersonationLevel +=impersonate, authenticationLevel=pkt}!\\\\' . $host . '\\root\\CIMV2 +\sms') or $ok = 0; alarm 0; }; if($@){ $err = 1; $ret = "timeout while accessing wmi on machine $h +ost"; return($err, $ret); } else { } # didn't

The code above works as desired when a machine is offline,
the alarm feature is not the issue
$col = undef; eval { local $SIG{ALRM} = sub { warn "ALARM FOR GET EXEC QUERY: \"$quer +y\" L: \"" . __LINE__ . "\" F: \"" . __FILE__ . "\"\n" }; # NB: \n re +quired alarm 5; $col = $wmicon->ExecQuery($query, "WQL", wbemFlagReturnImmediate +ly | wbemFlagForwardOnly | WbemAuthenticationLevelPkt) or $ok = 0; alarm 0; }; if($@){ $err = 1; $ret = "timeout while QUERY: \"$query\" wmi on +machine $host"; return($err, $ret); } else { } # didn't if($ok == 0) { $err = 1; $ret = clean(Win32::OLE->LastError); retu +rn($err, $ret); }
The code above works as expected
$objct = 0; $enabled = 0; $obj = undef; foreach $obj (in $col) { my $delete = 0; if($debug == 1) { print "\tOBJ: \"$obj\" L: \"" . __LINE__ . "\" +\n"; } }

This code is where the problem occurs.

I dont understand what the "foreach $obj (in $col)"
actually does/executes on the back end.
Why not use "foreach $obj (keys % { $col })".
This does not work.

I need to trap/eval the "foreach $obj (in $col)",
to test for failure then exit/return. currently
when this hangs, the script stops until i kill it manually.

"$col" is a hash, however when I move this
scalar to a hash with
my %colhash = % { $col }; if($debug == 1) { print "DUMP COLHASH: \"" . Dumper(%colhash) . "\" +L: \"" . __LINE__ . "\"\n"; }

The actual value is not the same as using the "foreach $obj (in $col)".
What is the difference in the "foreach" styles?



TIA,

Joe

P.S. I will sweeten the pot by honoring a single item
from a amazon wishlist (NOT GT $30USD).