Perl: variable scope problem with CGI and DBI modules

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 .

+3
13

DBI: .

DBI , , , , . , , cgi-lib. CGI (go Catalyst), , .

+1

, , : https://metacpan.org/pod/CGI#COMPATIBILITY-WITH-CGI-LIB.PL

, CGI:: ReadParse(); . , , , , , , ;)

, $cgi- > param ('foo')? .

+4

use strict;. .

our %in;

, . strict .

+3

, , , :

  • . , $in{test} .
  • &. ( "", .)

ReadParse - . , % in . , , () - . CGI.pm, FETCH params() . , $dbh->do() .

+3

-, / . . , {} - , "" perl. () parens .

, , , , , , , .

? % IN ?

+2

- . Perl- , - , - . , ( . ).

, , , % in ( - ReadParse? &, btw? ). , , .

+2

DBI ? DBI changelog , 1.00 . , "" $in{test} undef, $dbh->do().

+2

, , , .

, DBI ( DBD, , ) . , , , , .

SQLite DBI 1.53 , :

$ perl -MDBI -we'sub TIEHASH { bless {} } sub FETCH { "42" } tie %x, "main" or die; my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","",""); $dbh->do("create table foo (bar char(80))"); $dbh->do("insert into foo values (?)", undef, $x{foo}); print "got: " . $dbh->selectrow_array("select bar from foo") . "\n"; $dbh->do("drop table foo")'
got: 42

, ?

+2

, :

use CGI;
my %in;
CGI::ReadParse(\%in);

, , , , , ( use strict , )

0

tie(), . foo.pl perl foo.pl "x=1"

use CGI;

CGI::ReadParse();
p($in{x}, "$in{x}");

sub p { my @a = @_; print "@a\n" }

1 1. , .

0

. , ( DBD ?)

0

http://www.carcomplaints.com/test/test.pl.txt, , . , . CGI, :

...
use CGI qw/-debug/;
...

(test=test), .

If you leave this, tt will introduce an empty string and two NULLs. This is because you are interpolating the value into a string. This will contain a string with a value $in{test}that is currently equal undef. undefbuilds an empty string that is inserted into the database.

0
source

try it

% in = ReadParse ();

but I doubt it. Are you trying to get query parameters or something else?

-1
source

Source: https://habr.com/ru/post/1697090/


All Articles