Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Dereferencing problems

with pointers to vars and arrays

5:04 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I am trying to check for the existence of server environment variables and then sanitize them and copy them into local variables if they exist. The following code works fine:

$referer = "-";
if (exists($ENV{'HTTP_REFERER'})) {
$string = $ENV{'HTTP_REFERER'};
$referer = $string;

$usragnt = "-";
if (exists($ENV{'HTTP_USER_AGENT'})) {
$string = $ENV{'HTTP_USER_AGENT'};
$usragnt = $string;

However, I have a fairly long list of such variables, and I'd like to use a subroutine instead of hard-coding for each server environment variable. The following code doesn't work -- I guess because I'm confused about the $ENV array pointers and symbolic references. It seems that the environment variable array pointer isn't de-referenced properly, and that the symbolic reference to the named program variable doesn't work either.

&sanivar ("\'HTTP_REFERER\'", $referer, "-");
&sanivar ("\'HTTP_USER_AGENT\'", $usragnt, "-");

# If the server environment variable exists, sanitize it and copy it to the named program variable.
sub sanivar {
my($senvar, $locvar, $defval) = @_;
$string = $defval;
if (exists($ENV{$senvar})) {
$string = $ENV{$senvar};
$$locvar = $string;

'$string' always ends up holding the default string value specified in the calling parameters list, and the 'exists' check apparently always fails.

Also, the local variable pointed to by symbolic reference '$$locvar' is not updated.

Any suggestion or corrections to this code, or suggestions on a better implementation would be most welcome.


10:24 am on Sep 1, 2009 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

i think you need to pass the dereferenced variable and you don't need the escaped single quotes.
try this:
&sanivar ("HTTP_REFERER", \$referer, "-");
&sanivar ("HTTP_USER_AGENT", \$usragnt, "-");
3:06 pm on Sep 16, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The problem comes in, though, that if the environment variable doesn't exist (i.e. is undefined), then either PERL gives it a (defined) value of NULL or throws an error, depending on the PERL warning level.

I ended up just using:

$referer = &sanivar($ENV{'HTTP_REFERER'} "-");

and modifying the subroutine to suit. If the variable isn't defined or is NULL, then the 'OR' function kicks in and assigns it a value of "-".

If I get some more time, I'll go back and try your approach, though -- I hate leaving 'mysteries' unsolved just because I run out of time on a project...


Note to readers: Replace the broken pipe "" characters with solid pipe characters; Posting on this forum modifies the pipe characters.

8:29 pm on Sep 28, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I think phranque's first post is right on track, the single quotes were probably the biggest problem.

btw: you don't need the & in front of subroutine-calls, iirc, there was even something possibly bad with using them, but I don't quite remember what it was.

also, when using ¦¦, you're checking for three things: an empty string, a zero-value or an undefined value. that might, in this case, be just fine for you, but it's not always what you'd want, so watch out.


Featured Threads

Hot Threads This Week

Hot Threads This Month