on Tue, 09 Apr 2002 08:26:01 GMT, Connie Chan wrote:
> I have this statement
> if ( $ENV{REQUEST_METHOD} eq "GET")
> {
> .......
> .......
> }
>
> That works fine if I don't use -w , but if I use -w,
> it tells "Use of uninitialized value in string eq at....."
It doesn't work fine without '-w'!
By using the '-w' flag (or the 'use warnings;' pragma) you instruct perl
to tell you when something is wrong. In this case it tells you that
'$ENV{REQUEST_METHOD}' is not initialized, which means that there is no
variable 'REQUEST_METHOD' in your environment (maybe you are testing a
CGI-program from the command line?).
> Is that related to declare it first such as :
> my $req = $ENV{REQUEST_METHOD};
No. Here you assign an uninitialized value to a lexical variable.
'$ENV{REQUEST_METHOD}' is still undefined, as is '$req' after the
assignment.
You can test this with the following statement:
print '$req is not defined' unless defined($req);
'use strict;', or "use strict 'vars';" to be more precise, generates an
error message when you access a variable that was not declared with
'our', 'my' or 'use vars', unless it is fully qualified (as in
'$foo::var').
%ENV is a global special hash, which doesn't care about 'use strict;'.
However, you can test the existence of the 'REQUEST_METHOD' key in the
'%ENV' hash as follows:
print '$ENV{REQUEST_METHOD} does not exist'
unless exists($ENV{REQUEST_METHOD});
But the real message here is that you should *not* use this method for
CGI interaction. Use CGI.pm instead.
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]