Moin,
I started a FAQ for this list a while back. Never finished it. But the following is
the entry for your problem.
In your specific case, you need to check on or after your line 2 to see if
$Wmi contains an actual object reference. It won't if the machine is
off-line. Assuming your sample lines are inside a loop of some sort,
changing line 2 to
my $Wmi = Win32::OLE->GetObject ($Class) or next;
will do the trick.
Tom Wyant
Why do I get "Can't call method on an undefined value?"
-------------------------------------------------------
Because you're trying to call a method on an undefined value. This
generally means that you did something like
$object = Class->new ();
$object->method ();
and Class->new failed. The message is an alert that you should do some
error checking:
$object = Class->new () or die "Failed to create object.";
$object->method ();
The next question is why it failed. Different packages return errors
differently. If $!, $@, and $^E don't contain anything interesting, read
the module documentation to see if it defines its own error reporting
mechanism (Win32 modules frequently do, and some other modules do also). If
it didn't come with any, read the module.
The situation might not be as clear-cut if you are "chaining" method calls
(i.e. - $object->method->method), but the principal is the same. If you
can't sort it out, the best way to proceed is probably to break apart such
calls, and check for errors at every available opportunity.
"Khan, Moin" <[EMAIL PROTECTED]>@listserv.ActiveState.com on 05/30/2002
06:01:28 PM
Sent by: [EMAIL PROTECTED]
To: "'[EMAIL PROTECTED]'"
<[EMAIL PROTECTED]>
cc:
Subject: Error Handling ?
Hello List,
I write some code to get process information from the remote machine, i am
reading the machine names from a text file and then by loop calling a
function and process each machine, somehow, if the machine is not exist, i
got the error and script stoped, is there anyway to bypass error, like in
vb
we have 'on error resume next', i kow its not a good way, but right now i
need something to bypass that, or i can chk the machine is up before
running
the function? the escript code where it give me error is follows:
1 my $Class="WinMgmts:{impersonationlevel=impersonate}!//$szServer";
2 my $Wmi = Win32::OLE->GetObject ($Class);
3 my @Processes = in ($Wmi->InstancesOf ("Win32_Process"));
the error i got is "Can't call method "InstancesOf" on an undefined value
at
Line 3.
Any help is Appreciated.
Thanks,
-Moin Khan
(See attached file: C.htm)
Title: Error Handling ?
Hello List,
I write some code to get process information from the remote machine, i am reading the machine names from a text file and then by loop calling a function and process each machine, somehow, if the machine is not exist, i got the error and script stoped, is there anyway to bypass error, like in vb we have 'on error resume next', i kow its not a good way, but right now i need something to bypass that, or i can chk the machine is up before running the function? the escript code where it give me error is follows:
1 my $Class="WinMgmts:{impersonationlevel=impersonate}!//$szServer";
2 my $Wmi = Win32::OLE->GetObject ($Class);
3 my @Processes = in ($Wmi->InstancesOf ("Win32_Process"));
the error i got is "Can't call method "InstancesOf" on an undefined value at Line 3.
Any help is Appreciated.
Thanks,
-Moin Khan
