I came across what seems like a problem with a variable scope that I have not encountered before. I am using the Perl CGI module and calling the DBI do () method. Here's the code structure, a simplified bit:
use DBI;
use CGI qw(:cgi-lib);
&ReadParse;
my $dbh = DBI->connect(...............);
my $test = $in{test};
$dbh->do(qq{INSERT INTO events VALUES (?,?,?)},undef,$in{test},"$in{test}",$test);
The replacement variable # 1 is evaluated as if it were not initialized. The other two placeholder variables work.
Question: why the% in the hash is not available in the context of do () if I do not wrap it in double quotes (# 2 placeholder) or reassign the value of a new variable (# 3 placeholder)
I think this is due to the way in which the CGI module ReadParse () function sets the area to% in hash, but I don’t know how much Perl looks good enough to understand why% in is available at the top level, but not from in my do () instructions.
If someone understands the problem with the scope, is there a better way to handle this? Wrapping all% links in double quotes seems a bit messy. Creating new variables for each query parameter is unrealistic.
Just to be clear, my question is about the problem of changing variables. I understand that ReadParse () is not the recommended method for capturing query parameters using CGI.
I am using Perl 5.8.8, CGI 3.20 and DBI 1.52. Thank you in advance to everyone who reads this.
@Pi and @Bob, thanks for the suggestions. Pre-declaring the scope for% in has no effect (and I always use strict). The result will be the same as before: in db, col1 is null, and cols 2 and 3 are the expected value.
, ReadParse (. ). , CGI.pm. , , % in hash ( ) , , :
sub ReadParse {
local(*in);
if (@_) {
*in = $_[0];
} else {
my $pkg = caller();
*in=*{"${pkg}::in"};
}
tie(%in,CGI);
return scalar(keys %in);
}
, - % do()? ! , .
@Dan: & ReadParse. CGI:: ReadParse(), , , CGI.pm .