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

I have a script running under Apache/cgi (not mod_perl). Building the sql statement two different ways gives different results, even though the statements are equal. The script:
sub _select { my ($self, $sql, $where, $values, $orderby) = @_; my @values = $values ? @$values : undef; $sql .= " where $where " if $where; $sql .= " order by $orderby " if $orderby; my $sql2 = "select * from content where name = 'practice' "; print $sql; print $sql2; print ($sql eq $sql2 ? 'equal' : 'NOT'); # print "DEBUG: $sql w/values: " . Dumper \@values; my $sth = $self->dbh->prepare($sql) or die "couldn't prepare: " . +$self->dbh->errstr(); # $sth->execute(@values) or die "couldn't execute: " . $self->dbh->e +rrstr(); $sth->execute() or die "could't execute: " . $self->dbh->errstr(); my @rows = (); while (my $row = $sth->fetchrow_hashref()) { push (@rows, $self->new($row) ); } return \@rows; }
The output is:
select * from content where name = 'practice' select * from content where name = 'practice' equal Software error: DBD::CSV::Statement=HASH(0x1c84c3c) is not a valid SQL::Statement obje +ct at E:/Perl/site/lib/DBD/File.pm line 171.
It dies on the $sth prepare if it is passed $sql. But works if passed $sql2. Is there some way the two vars are different even though they appear to be equal?

2001-03-19 Edit by Corion : Changed PRE tags to CODE tags

Replies are listed 'Best First'.
(tye)Re: Variable are equal but don't behave that way
by tye (Sage) on Mar 21, 2001 at 04:07 UTC

    It sounds to me like $sql is an object with overloaded operations so that it returns a query string in a string context (and knows how to append to that string).

    You need to show the results of ref($sql) and ref($sql2) to prove whether this is in fact the difference or not.

    You could also try this:

    my $sth = $self->dbh->prepare("$sql") or die "couldn't prepare: " . $self->dbh->errstr();
    (note the added quotes) which is normally a bad idea but might fix things here.

    My guess is that some DBx code is checking ref() when it shouldn't be. O-:

            - tye (but my friends call me "Tye")
Re: Variable are equal but don't behave that way
by voyager (Friar) on Mar 21, 2001 at 04:29 UTC
    What I neglected to say was that the $where passed in is built from a query parm and I had taint checking on. When taint checking is turned off it works, so I guess I need to untaint. I'll check doc on this.

    I'd heard lots of good things about -T, but I guess I expected an error message including the word "taint" :)