Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: variable scope issue with CGI & DBI modules
    text
    copied!<p>I've run into what appears to be a variable scope issue I haven't encountered before. I'm using Perl's CGI module and a call to DBI's do() method. Here's the code structure, simplified a bit:</p> <pre><code>use DBI; use CGI qw(:cgi-lib); &amp;ReadParse; my $dbh = DBI-&gt;connect(...............); my $test = $in{test}; $dbh-&gt;do(qq{INSERT INTO events VALUES (?,?,?)},undef,$in{test},"$in{test}",$test); </code></pre> <p>The #1 placeholder variable evaluates as if it is uninitialized. The other two placeholder variables work.</p> <p><strong>The question: Why is the %in hash not available within the context of do(), unless I wrap it in double quotes (#2 placeholder) or reassign the value to a new variable (#3 placeholder)?</strong></p> <p>I think it's something to do with how the CGI module's ReadParse() function assigns scope to the %in hash, but I don't know Perl scoping well enough to understand why %in is available at the top level but not from within my do() statement.</p> <p>If someone does understand the scoping issue, is there a better way to handle it? Wrapping all the %in references in double quotes seems a little messy. Creating new variables for each query parameter isn't realistic.</p> <p>Just to be clear, my question is about the variable scoping issue. I realize that ReadParse() isn't the recommended method to grab query params with CGI.</p> <p>I'm using Perl 5.8.8, CGI 3.20, and DBI 1.52. Thank you in advance to anyone reading this.</p> <p>@Pi &amp; @Bob, thanks for the suggestions. Pre-declaring the scope for %in has no effect (and I always use strict). The result is the same as before: in the db, col1 is null while cols 2 &amp; 3 are set to the expected value.</p> <p>For reference, here's the ReadParse function (see below). It's a standard function that's part of CGI.pm. The way I understand it, I'm not meant to initialize the %in hash (other than satisfying strict) for purposes of setting scope, since the function appears to me to handle that:</p> <pre><code>sub ReadParse { local(*in); if (@_) { *in = $_[0]; } else { my $pkg = caller(); *in=*{"${pkg}::in"}; } tie(%in,CGI); return scalar(keys %in); } </code></pre> <p>I guess my question is what is the best way to get the %in hash within the context of do()? Thanks again! I hope this is the right way to provide additional info to my original question.</p> <p>@Dan: I hear ya regarding the &amp;ReadParse syntax. I'd normally use CGI::ReadParse() but in this case I thought it was best to stick to how <a href="http://search.cpan.org/src/LDS/CGI.pm-3.42/cgi-lib_porting.html" rel="nofollow noreferrer">the CGI.pm documentation has it</a> exactly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload