in reply to Uninitialized value from hostname()

I also find your usage of 'our' to be quite strange. Please try stevieb's advise to see if your error goes away by using 'my $host = ...'. Additionally I recommend to rewrite your get_local_ip function as follows:

minimum - use return value to provide 'local ip'
sub get_local_ip { my $sock = IO::Socket::INET->new( PeerAddr=> $mrtgc2, PeerPort=> 5432, Proto => "tcp"); return $sock->sockhost; } my $localip = get_local_ip();
refinement - with extracted parameters:
sub get_local_ip { my $host = shift; my $port = shift; my $sock = IO::Socket::INET->new( PeerAddr=> $host, PeerPort=> $port, Proto => "tcp"); return $sock->sockhost; } my $localip = get_local_ip($mrtgc2, 5432);

Replies are listed 'Best First'.
Re^2: Uninitialized value from hostname()
by eepstein (Initiate) on Oct 13, 2016 at 20:38 UTC
    I also find your usage of 'our' to be quite strange.
    I'm new to Perl, so I'm sure some of it is going to look "strange" :)

    Thank you for the rewrite. I'm still having difficulty understanding our, my, and local. If I change it from 'our' to 'my', how do I properly "access" $localip from within nested IF/ELSE and WHILE? That in a nutshell is my biggest challenge with Perl currently, is watching the warnings and errors complain about my syntax for using variables.

    Verifying whether this has worked or not will take some time. This script is running out in the field, and I would need to wait for another network event to see what happens. Under normal conditions I have no warnings or errors returned from running the script. It also runs on many other systems and returns no errors or warnings at all. Since it's all logged I can verify what occurs on the other systems and that the script is working.

      long answer: Variable Scoping in Perl: the basics

      short answer:
      • forget about 'our' and 'local' (for now)
      • identify the relevant blocks (= something enclosed in '{ }')
      • if you need the variable outside of the block, define it outside

      minimalistic example to set a value inside a conditional / loop / block:
      (I intentionally used the variable '$value' inside the block to demonstrate 'my'. It is not mandatory, you can also assign directly.)
      my $variable; if (1 == 1) { my $value = 'a'; $variable = $value; } else { my $value = 'b'; $variable = $value; } # => '$variable' now contains 'a'