homepage Welcome to WebmasterWorld Guest from 54.166.228.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Dereferencing problems
with pointers to vars and arrays
jdMorgan




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

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'};
&sanitize;
$referer = $string;
}

$usragnt = "-";
if (exists($ENV{'HTTP_USER_AGENT'})) {
$string = $ENV{'HTTP_USER_AGENT'};
&sanitize;
$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 {
local($string);
my($senvar, $locvar, $defval) = @_;
$string = $defval;
if (exists($ENV{$senvar})) {
$string = $ENV{$senvar};
&sanitize;
}
$$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.

Thanks!
Jim

 

phranque




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

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, "-");

jdMorgan




msg:3990316
 3:06 pm on Sep 16, 2009 (gmt 0)

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...

Thanks,
Jim

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

janharders




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved