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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|