[EMAIL PROTECTED] wrote:
> 
> Wizards,
> 
> I have several subs that have a common foreach loop in the code. What
> I'd like to do is pull the loop out into a separate subroutine to save
> typing. What's hanging me up is that I don't know how to pass a WMI
> thingy as a subroutine argument. Here's the code with some comments
> illustrating what I need to do...

My suggestion:

use strict;
use warnings;
use Win32::OLE qw(in);

my $WMI = Win32::OLE->GetObject("WinMgmts://$ENV{COMPUTERNAME}/root/cimv2") or
  die "GetObject: $!($^E)";

my @propnames = qw(Name Manufacturer Version Description FileType FileSize
  Group CreationDate InstallDate LastModified Compressed Encrypted Hidden);

my $properties = query_properties ();   # returns array of arrays

foreach my $set (@{$properties}) {
        for (my $ii = 0; $ii < @propnames; ++$ii) {
                my $val = defined $set->[$ii] ? $set->[$ii] : 'undef';
                print "$propnames[$ii] => $val\n";
        }
        print "\n";
}
exit;

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub query_properties {
#       my %args = (-this_stuff => undef, @_); # not used

my $colItems = $WMI->ExecQuery("Select * From Win32_CodecFile");
my $results = get_property_values ($colItems);
return $results;

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub get_property_values {
        my $colItems = shift;

my @props = ();
my $ii = 0;
foreach my $objItem (in $colItems) {

        # create date strings and Yes/No values for variables (omitted)

        for (my $jj = 0; $jj < @propnames; ++$jj) {
                my $prop = $propnames[$jj];
                # push property value in @propnames order
                push @{$props[$ii]}, $objItem->{$prop};
        }
        ++$ii;
}
return [EMAIL PROTECTED];

}

__END__


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to