in reply to Re: Data::Dumper / write output into Mysql
in thread Data::Dumper / write output into Mysql

Hi hippo, The code you've helped me with works pretty well but if I understand the error correctly it seems like when the device isn't answering in time passing a value back to the script I'm getting the following error. Can't use an undefined value as a HASH reference at ./smwalker.pl line 152. ### This is basically Line 152
for my $mac (values %{(values %$result)[0]}) { print "Mac addr: $mac\n"; }
How do I deal with this ? Thanks in advance,

Replies are listed 'Best First'.
Re^3: Data::Dumper / write output into Mysql
by hippo (Archbishop) on Nov 05, 2015 at 13:30 UTC

    On a modern enough perl you can use the defined-or operator // to use an empty hashref instead. eg:

    use strict; use warnings; my $result = { '10.4.20.21' => undef }; for my $mac (values %{(values %$result)[0] // {}}) { print "Mac addr: $mac\n"; }

    Of course, if your entire $result is undef then you could easily check for this separately before even hitting the for loop.

      I'm guessing I'll be doing something like this to check whether the entire $result is undef ?
      unless (defined($result)) { ....blah }