http://www.perlmonks.org?node_id=207313

true has asked for the wisdom of the Perl Monks concerning the following question:

Howdy, Trying to debug a script. Running perl5.005 under mod_perl. I have taint checking on and 'use strict;'. My httpd log reports...
Use of uninitialized value at index.cgi line 37.
Here's line 37...
print NEW "$ENV{'QUERY_STRING'}\n";
How do i initialize the ENV Hash so it won't report this error? I want to reference the environmental variables in my script. Declaring %ENV = (); didn't work either ;>. Thanks for help monks.

Replies are listed 'Best First'.
mod_perl does not initialize $ENV{QUERY_STRING}
by lestrrat (Deacon) on Oct 23, 2002 at 10:24 UTC

    No offense, but like the other question, you kinda need to go and read up on mod_perl programming before asking a bazillion questions.

    No, this is not a taint issue. It's exactly what the error says: $ENV{QUERY_STRING} is not defined. Why is it not defined? Because you're running under mod_perl, not mod_cgi

    Basically, $ENV{QUERY_STRING} is only available when you're running under mod_cgi, which populates %ENV with some CGI-specific information.

    mod_perl doesn't do that. All the information is in the Apache object ($r).

    If you insist on using Apache::Registry, you should stop sending headers with print and trying to get the query string directly -- just use CGI.pm, which handles these things correctly for you

Re: initialize ENV variables under strict
by BrowserUk (Patriarch) on Oct 23, 2002 at 09:10 UTC

    You could prevent the warning by testing the value before trying to use it like this

    my $qs = $ENV{'QUERY_STRING'}; print NEW "$qs\n" if defined $qs and not $s='';

    However, when $ENV{'QUERY_STRING'} is undefined or has a null value, it normally means you need to take some other action in your script to correct that situation.

    Thats what makes the warning so useful.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: initialize ENV variables under strict
by broquaint (Abbot) on Oct 23, 2002 at 08:48 UTC
    How do i initialize the ENV Hash so it won't report this error?
    This is actually a warning which says that the value in that particular operation was undefined. To fix that you just need to assign a value to that particular key in the %ENV hash, however this is probably not what you want as the %ENV hash should be populated by default.
    HTH

    _________
    broquaint

Re: initialize ENV variables under strict
by krusty (Hermit) on Oct 23, 2002 at 08:55 UTC
    Are you trying to use what an end system is set to by default?
    (if indeed this environment var is set on some system....... not yours ;-)

    Perhaps try:
    $ENV{'QUERY_STRING'} = "" unless defined $ENV{'QUERY_STRING'};
    Environment variables taken from your system, not set by you, are tainted. You may need to untaint it first.
Re: initialize ENV variables under strict
by true (Pilgrim) on Oct 23, 2002 at 10:02 UTC
    krusty said:
    "Environment variables taken from your system, not set by you, are tainted. You may need to untaint it first."

    This is how i should have phrased my question.
    I want to untaint the tainted %ENV.
    This untainting would seem like a very common desire among monks.
    Anybody know a simple way to do this in perl5.005?
    I can always just check each ENV key as suggested earlier.

    thanks folks.

      There is no simple way to untaint variables. I can't give you an answer that would fit for every program you'll ever want to write under taint mode because I don't know the details of every program. You have to ask, "Exactly what kind of input do I expect here?" and then you can craft a regular expression to match only that type of input.