My perl code makes a call to an API, passing a reference to an array of string IDs, and expects to get an array of objects back.
my @customers = $api->get_customer_details(\@customer_ids);
for my $customer (@customers) {
# debug
print $customer . "/n";
print ref($customer) . "/n";
print Dumper ($customer);
if (!$customer) {
print "No customer object\n";
# do stuff
last;
}
print "Got the customer object\n";
$info->{customer_objects}{$customer->customer_id} = $customer;
}
99% of the time, when I run this I get customer objects back. However, I'll occasionally get the output:
HASH(0x12345)
HASH
{}
Got the customer object
Can't call method "customer_id" on unblessed reference at ..
I've tried to edit my IF statement to check for an empty hashref, but it will always ignore the IF:
if (!$customer || !%$customer)
When I test this out via command line, the empty hashref works as expected:
$ perl -E 'my $hr = {}; if (!$hr || !%$hr) { say "empty" } else { "nonempty" }'
empty
I'm not understanding what the issue is. It appears that my debug output isn't accurate, and I am actually not getting an empty hashref. Can someone please explain what is going on, and how I might figure out exactly what $customer is and how to ignore this case in my IF statement?