i am running a Vmware script where it goes half way through and then fails with below error and exits:
Can't use an undefined value as an ARRAY reference at test.pl line 49.
This is running on a vCetner inventory which has ~4000 VMs. So I want the script to ignore such erros and continue running until it finishes. At the moment the script exits withouht any ouput after above error.
here is my code which gives me the above error. I am pasting only few lines where the error arises from:
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
print $vm_mo_ref."\n";
}
This is what I did to ignore the error:
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
if (defined $vm_mo_ref)
{
print $vm_mo_ref."\n";
}
}
and
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
if (exists $vm_mo_ref)
{
print $vm_mo_ref."\n";
}
}
and
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
if ($vm_mo_ref)
{
print $vm_mo_ref."\n";
}
}
and
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
if (ref($vm_mo_ref) eq "ARRAY")
{
print $vm_mo_ref."\n";
}
}
and
foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
if (ref($vm_mo_ref) eq "ManagedObjectReference")
{
print $vm_mo_ref."\n";
}
}
However, none of above five approaches with “if” condition are working and my script still fails/exits with aforementioned error.
BTW, ManagedObjectReference is Vmware specific reference.
Could you help me get around this error and continue with the rest of the script.
Thanks.
Try
Nothing you change in the loop will have any effect because your code throws an exception before it even enters the loop.