Perl- Diagnostic hash print needed

software development

#1

I’m having a lot of trouble getting past this point in a Perl script that takes user input search criteria and searches our publications and formats the output. I’ve inserted debug print statements in to discover the SQL statement is exactly what I want. When I replace the fetchrow_hashref and the call to the display_listing routine with:

while (my @row= $sth->fetchrow_array ())
{
print “$row[0] $row[1] $row[2] $row[3]
\n”;
}

I get the correct but unformatted data.

I can’t seem to get anything out to the original hash. I must have a fundamental misunderstanding about how hashes work. What would be a way to insert a diagnostic dump of the hash contents before I jump into the display_listing?

The code in part:

#@ CONSTRUCT_QUERY
#$col_list = “*”;#

WHERE clause listing the conditions

$where = "WHERE
(tblLitDtlYear.ID = tblLitMaster.ID) AND
(tblLitMaster.ID = tblLitDtlCatgy.ID) AND
(tblLitMaster.ID = tblLitDtlAuthor.ID) AND “
. join (” AND ", @condition) if @condition;
$where = “” unless $where;

complete query

$stmt = “SELECT DISTINCT
tblLitMaster.Title,
tblLitMaster.ID,
tblLitDtlYear.Year,
tblLitDtlCatgy.Category,
tblLitMaster.RptPhysAddress
FROM
tblLitMaster,
tblLitDtlYear,
tblLitDtlCatgy,
tblLitDtlAuthor
$where
ORDER BY Year LIMIT 100”;

print “condition array = “, @condition,”\n\n”;

print “placeholder array = “, @placeholder,”\n\n”;

print “\n\n”;

print “where statement = $where\n\n”;

print “SQL statement = $stmt\n\n”;

#@ CONSTRUCT_QUERY

#@ EXECUTE_QUERY
$sth = $dbh->prepare ($stmt);
$sth->execute (@placeholder);
$count = $sth->rows;
while (my $ref = $sth->fetchrow_hashref ())
{
printf $ref->{Title},$ref->{RptPhysAddress};

display_listing ($ref);

}
$sth->finish ();

print (“Sorry, no qualifying listings were found.”) if !$count;
#@ EXECUTE_QUERY

This signature line intentionally blank.


#2

One thing you can try is the Data::Dumper module. It should already be on the Dreamhost servers.

Here’s a very simple example:

[code]#!/usr/bin/perl
use Data::Dumper;
use strict;

my $a = { a => 1, b => 2, c => 3 };

print Dumper($a) . “\n”;
[/code]
Save that as a perl file and run it to get a basic idea of how it works. It’s an exceptionally handy tool.

http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm


#3

Dumper worked for me in a little test script, but when I inserted it in my problem script:
while (my $ref = $sth->fetchrow_hashref ())
{
print Dumper($ref) . “\n”;

display_listing ($ref);

}
$sth->finish ();

I got nothing. So can I confidently say that nothing is getting put into $ref by fetchrow_hashref? Will Dump display on a http output form?

This signature line intentionally blank.


#4

It sounds like something isn’t working quite right - even if the variable is undefined Dumper should still give you something (ie. $VAR1 = undef;).

If this is being output to a browser, have you tried viewing the page source to see if it is showing up there? It may be muddled by some sort of HTML markup oddity. If it isn’t there, you want want to try running the script from a command line if you are able. That way you could see all of the information as it is output from the program.


#5

I just completed an elaborate debug version to use Data::Dumper to see what was retrieved by fetchrow_hashref. When I couldn’t see anything printed out in html, I set up a version that would run from the command line. I commented out all the html header stuff, bypassed the input form and set up a fixed where clause. I had the script print out the value of the SQL statement and $ref. I got printed out the expected values for $ref so I guess my problem then has to be in the display routine.:

#@ EXECUTE_QUERY
$sth = $dbh->prepare ($stmt);
$sth->execute (@placeholder);
$count = $sth->rows;
while (my $ref = $sth->fetchrow_hashref ())
{
print Dumper($ref) . “\n”;

display_listing ($ref);

}

SQL statement = SELECT DISTINCT
tblLitMaster.Title,
tblLitMaster.ID,
tblLitDtlYear.Year,
tblLitDtlCatgy.Category,
tblLitMaster.RptPhysAddress
FROM
tblLitMaster,
tblLitDtlYear,
tblLitDtlCatgy,
tblLitDtlAuthor
WHERE
(tblLitDtlYear.ID = tblLitMaster.ID) AND
(tblLitMaster.ID = tblLitDtlCatgy.ID) AND
(tblLitMaster.ID = tblLitDtlAuthor.ID) AND
(tblLitDtlYear.Year = 1990)
ORDER BY Year LIMIT 100

$VAR1 = {
‘Year’ => ‘1990’,
‘ID’ => ‘SI0004’,
‘RptPhysAddress’ => ‘http://www.efw.bpa.gov/Publications/P01483-1.pdf’,
‘Category’ => ‘Species Interactions’,
‘Title’ => ‘Yakima River Species Interactions Studies, Annual Report FY 1990’
};

This signature line intentionally blank.


#6

Is your SQL query working?

I mean, I see backticks in the double quotes for your SQL query.
I’m not sure but backticks may be interpreted as shell escape, and totally messing up the query.

Have you tried single quotes?

...* I’d be happier if DH had free shared SSL and redundant server setup. ..*.