I'm glad you have something that appears to be working for you. However I find the logic somewhat confusing, and somewhat perverse.

I think the intention is to find a valid external IP, or failing that to fall back to any valid internal IP. If that is indeed the intention, I think it could be expressed more clearly with something like:

my($valid_ip, $valid_external); foreach my $check (@IPs) { # clean it up $check =~ s/^\s*(.*?)\s*$/\1/; # skip if not a valid IP next unless is_ip($check); # overwrite with the last IP we saw that's valid $valid_ip = $check; # skip unless it's an external IP next if (not is_public_ip($check)) or (is_loopback_ip($check)) or ($check eq "41.79.21.90"); # overwrite with the last valid IP that is external $valid_external = $check; } # choose last external IP if we found one, else last valid IP my $ip = $valid_external // $valid_ip;

Your current code favours the last external IP rather than the first one, and the last internal IP rather than the first one, but I cannot tell if either of those choices are intentional. My proposed code honours that preference, but if you actually want the _first_ external IP from the list you could add last; at the end of the loop to short-circuit it; if you want the first internal IP from the list, you can replace $valid_ip = $check with $valid_ip //= $check.

Side-note: the //g option on the cleanup regexp serves no useful purpose, so I removed it.

Hope this helps.


In reply to Re^4: mod-perl2 re-using $ENV{REMOTE_ADDR} by hv
in thread mod-perl2 re-using $ENV{REMOTE_ADDR} by bbs2web

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.