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

I am converting some VB scripts to perl and I am having a problem referencing instances of an object that are returned in a method. The first code below is the VB code and the second is the perl code.

The problem is the value in $NGList is the number of instances returned not a list of the object instances like I thought it would be. The in the foreach loop the value of 4 (the number of instances returned) is assigned to $fNode. The assignment of $objNodes in the ExecQuery works fine and the line  print "$fNode->{PrimaryNodeName}\n"; works fine. Any suggestions?

VB Script

Set WshNetwork = WScript.CreateObject("WScript.Network") Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") Set objWMIService = objSWbemLocator.ConnectServer(strMLM,"\root\hewlet +tpackard\openview\data") Set objNodes = objWMIService.ExecQuery("select * from OV_ManagedNode w +here PrimaryNodeName like '" & strND & "%'") for each nm in objNodes Wscript.Echo "Primary Node name is " & nm.PrimaryNodeName '& vbNew +Line Name = nm.name nm.GetParents(NGarray) For Each gp in NGarray Wscript.Echo gp.Caption Next next Wscript.Quit 0

Perl Script

$objSWbemLocator = Win32::OLE->new("WbemScripting.SWbemLocator"); $objWMIService = $objSWbemLocator->ConnectServer($strMLM,"\\root\\hewl +ettpackard\\openview\\data"); $objNodes = $objWMIService->ExecQuery("Select * from OV_ManagedNode wh +ere PrimaryNodeName like \"%$argNode%\""); foreach $fNode (in $objNodes) { print "$fNode->{PrimaryNodeName}\n"; $NGList = $fNode->GetParents() or die; foreach $fNode ($NGList) { print "***************\n"; my $fName = $fNode->{Name}; my $fCaption = $fNode->{Caption}; print "$fName:$fCaption \n"; #GUID of the group @polGrps = $fNode->GetAutoDeployPolicyGroups; } }

Replies are listed 'Best First'.
Re: Problem with win32 objects
by runrig (Abbot) on Feb 25, 2011 at 01:46 UTC
    If $NGList is a collection, then in($NGList) should return the list of them.

    I really suggest Use strict and warnings though.

Re: Problem with win32 objects
by toolic (Bishop) on Feb 25, 2011 at 01:50 UTC
    $NGList = $fNode->GetParents() or die;
    If GetParents returns a list of things, you could try assigning it to an array:
    my @NGList = $fNode->GetParents() or die; foreach my $fNode (@NGList)
    I strongly recommend that you use strict and warnings (update: runrig beat me to it).
Re: Problem with win32 objects
by ScooterTrash (Novice) on Feb 28, 2011 at 15:44 UTC

    I found the problem and it has to do with the data type used. The referenced list of object instances has to be declared as a type of MS object. If I declare the variable and populate it using the following snippet it works.

    $objSWbemLocator = Win32::OLE->new("WbemScripting.SWbemLocator"); $objWMIService = $objSWbemLocator->ConnectServer($strMLM,"\\root\\hewl +ettpackard\\openview\\data"); $objNodes = $objWMIService->ExecQuery("Select * from OV_ManagedNode wh +ere PrimaryNodeName like \"%$argNode%\""); my $NGList = Win32::OLE::Variant->new(VT_VARIANT|VT_BYREF); foreach $fNode (in $objNodes) { print "$fNode->{PrimaryNodeName}\n"; $fNode->GetParents($NGArray, "TRUE") or die; foreach $fGrp (@{$NGArray->Value()}) { push(@nGrp, $fGrp->{Caption}); } }