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

How would I translate the "For Each" part to perl? I've can't find anything that works.
Set dom = GetObject("WinNT://ArcadiaBay") dom.Filter = Array("user") For each usr in dom Debug.Print usr.Name Next

Replies are listed 'Best First'.
Re: OLE VB to Perl
by btrott (Parson) on Jul 13, 2000 at 04:26 UTC
    Well, umm... how are you translating the *other* parts to Perl? :) Assuming that dom is an array of some sort of appropriate objects/references:
    foreach my $user (@dom) { print $user->{name}; }
RE: OLE VB to Perl
by barndoor (Pilgrim) on Jul 13, 2000 at 13:22 UTC
    I think I'm right to say your trying to read through what VB would call a collection. If this is the case you should use the Win32::OLE::Enum module. This allows you to extract a collection of objects and then iterate through them.
    use strict; use Win32::OLE; # Open excel. my $excel = Win32::OLE->new("Excel.Application", 'Quit'); # Create a workbook. my $excelBook = $excel->Workbooks->Add; # Enumerate the sheets in the workbook (sheet1,sheet2,sheet3 by defaul +t). my $sheets = Win32::OLE::Enum->new($excelBook->Worksheets()); #Iterate the sheets using the All() function. foreach my $sheet ($sheets->All()) { print $sheet->name() . "\n"; }
    No error handling I know but it gives you an idea and I've run this on NT4.0 with Office 98.
      Thank that was just what I was lookin' for.
      foreach $child(Win32::OLE::Enum->All($ADS)){ print $child->Name . "\n"; }
      I shall own the Win2k Active Directory!!!!

        I know that this is amazingly belated, but I've been wrestling with the same issue myself and came across this thread. There's actually an easier solution to iterating over OLE collections, and it goes a little something like this:

        use Win32::OLE 'in'; foreach $member (in $collection) { #do some stuff }

        Oddly enough, I gleaned this from the Win32::OLE docs :-). The 'in' function is not exported by default, so you need to specify it in the import list, as above.

        Again, sorry for the late reply, but hopefully this will help latecomers like myself who may stumble across this thread.

        -jehuni

Re: OLE VB to Perl
by Rydor (Scribe) on Jul 13, 2000 at 06:11 UTC
    Perl has a term called foreach. it works like this:
    foreach $element (@array){dostuff.....;}
    foreach takes the first element of array, makes $element a reference to that value, and does the loop. it does the next location next, and so on until the array is done. example
    @numbers=(1,2,3,4,5);
    foreach $digit (@numbers){print "$digit\n";}
    This prints:
    1
    2
    3
    4
    5

    @:::::::((==========Rydor====>