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

Hello, When i compile a script it throws an error. Exception in testMain(): Can't use an undefined value as an ARRAY reference at Statistics.pm line 187. below is the line  return scalar @{ $self->{'netstat_array'} }, $self->{'netstat_array'} ;

I'd like to add that the error is not thrown always. If the return value is >=1 script doesn't fail. If the return value is 0 then it throws an error.

Any suggestions?

Replies are listed 'Best First'.
Re: Can't use an undefined value as an ARRAY reference
by ikegami (Patriarch) on Aug 05, 2014 at 20:29 UTC

    That means $self->{'netstat_array'} is sometimes undefined, which means it doesn't contains a reference to an array.

    If it's suppose to be undefined at times, then maybe you're looking for

    $self->{'netstat_array'} ||= []; return scalar @{ $self->{'netstat_array'} }, $self->{'netstat_array'};
      thanks,it works
Re: Perl array reference error
by Anonymous Monk on Aug 05, 2014 at 20:29 UTC

    That means that $self->{'netstat_array'} is sometimes undef. The first questions I'd ask myself are: why is it undef, and is undef a valid value for that variable? A quick workaround might be to write @{ $self->{'netstat_array'} // [] }, but that won't solve the root of the issue. For more help please read and follow How do I post a question effectively?

Re: Perl array reference error
by Laurent_R (Canon) on Aug 05, 2014 at 21:11 UTC
    You've now got the immediate source of the error, but if you're after the root cause, maybe your should show the code with which you call that module (and quite possibly also the data).