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

I am learning Perl and would like some help with a script. I would like to be able to run this script against any pc by entering c:\perl> perl "script" "machinename" Here is what I have so far.
----------------------------------------------------- #!perl use Win32::OLE qw(in); if ( $ARGV[0] ) { $strComputer = $ARGV[0]; } else { $strComputer = "."; } $strComputer = '.'; $objWMIService = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer +. '\\root\\CIMV2'); $colItems = $objWMIService->ExecQuery('SELECT * FROM Win32_Process WHE +RE Name = \'explorer.exe\'', undef, 48); foreach my $objItem (in $colItems) { print 'PageFileUsage: ' . $objItem->PageFileUsage, "\n";
---------------------------------------------------

Replies are listed 'Best First'.
Re: Help for a beginner
by agianni (Hermit) on Mar 02, 2007 at 19:04 UTC
    It would probably help if you deleted line 12, which sets $strComputer to "." even after you potentially assigned $ARGV[0] to it.
      Or he could instead just copy the if-else block above that assignment and paste it below the assignment.

      It would be like that old saying, "If you love someone, set them free -- if they come back, assign a typeglob to them."

      :D

      Thank you so much. That worked. What was the $strComputer doing? I thought that you had to have that in there so that if nothing was called out it would default to the local host.
        You're simply overwriting it with the assignment operator on line 12. A common way to deal with ensuring a default value is set is through the use of a C -style OR:
        $strComputer = $ARGV[0] || '.';