in reply to Re: mod-perl2 re-using $ENV{REMOTE_ADDR}
in thread mod-perl2 re-using $ENV{REMOTE_ADDR}

Thank you for your feedback, searching for 'splice 0,0 v unshift' appears to show that unshift is twice as performant as splice. This really isn't a large array though but if it's easier to read and faster in the process then why not... ;)


It appears I wasn't initialising $ip, when I added 'undef $ip' it now works as expected:

Apache's configuration stanza:
ScriptAlias /cgi-bin/ /var/www/perl-cgi/ PerlSwitches -wT <Directory /var/www/perl-cgi> SetHandler perl-script PerlHandler ModPerl::Registry PerlSendHeader On AllowOverride None Options +ExecCGI -MultiViews </Directory>

And the script where I now undefine the $ip variable at start:
use Data::Validate::IP qw(is_ip is_public_ip is_loopback_ip); @IPs=''; undef $ip; if (defined $ENV{HTTP_X_FORWARDED_FOR}) { @IPs=split /,/, $ENV{HTTP_X_FORWARDED_FOR}; }; unshift (@IPs, $ENV{REMOTE_ADDR}); foreach $check (@IPs) { $check =~ s/^\s*(.*?)\s*$/\1/g; if (is_ip($check)) { if (defined $ip) { if (not is_public_ip($check)) { $check = $ip; }; if (is_loopback_ip($check)) { $check = $ip; }; if ($check eq "41.79.21.90") { $check = $ip; }; }; $ip = $check; }; }; print "Content-type: text/html\nCache-Control: max-age=0,no-cache,no-s +tore,post-check=0,pre-check=0\n\n<html><head><title>Current IP Check< +/title></head><body>Current IP Address: $ip</body></html>";

Replies are listed 'Best First'.
Re^3: mod-perl2 re-using $ENV{REMOTE_ADDR}
by Corion (Patriarch) on May 01, 2023 at 16:00 UTC

    Making the variables lexical variables would likely also help:

    use strict; my @IPs; my $ip;