I usually do this:
my $IP = ENV('REMOTE_ADDR', '0.0.0.0');
print $IP;
exit;
###################
# This function returns an environment variable named in the
# first argument. If the environment variable doesn't exist,
# then DEFAULT is returned. However, if a third argument is
# given, then that will be returned in every case.
# The third argument is used for tweaking and testing purposes.
#
# Usage: VALUE = ENV( NAME, [DEFAULT, [OVERRIDE]] )
#
sub ENV
{
return '' unless @_; # No arguments at all?
my $NAME = shift; # Get NAME
my $DEFAULT = @_ ? shift : ''; # Get DEFAULT value
return shift if @_; # OVERRIDE?
return $DEFAULT unless defined $NAME; # NAME undefined?
return $DEFAULT unless $NAME; # NAME is blank?
if (exists($ENV{$NAME})) { return Trim($ENV{$NAME}); }
return $DEFAULT;
}
###################
#
# This function removes whitespace before and after STRING
# and returns a new string.
#
# Usage: STRING = Trim(STRING)
#
sub Trim
{
return '' unless @_;
my $T = shift;
return '' unless defined $T;
return '' unless length($T);
my $s = -1; # start ptr
my $e = 0; # end ptr
for (my $i = 0; $i < length($T); $i++)
{
if (vec($T, $i, 8) > 32)
{
if ($s < 0) { $s = $i; }
$e = $i;
}
}
return substr($T, $s, $e - $s + 1);
}
This means, even if REMOTE_ADDR is undefined for whatever reason, I get a string in $IP which is going to be 0.0.0.0. And then I know that it's because it was undefined!