S
Sergei Shelukhin
I am starting to write my first Perl module ever, I want to create standart
DBGrid thing...
However, I have osmething messed up and I cannot figure out what is wrong.
#!!!!!!!!!!!!!
line indicates the problem. Problem is that, even though function reference
seems to be passed and everything seems to be ok, it doesn't launch, and
instead, the script is terminating with errors.
What am I doing wrong?
Here's the test script
use strict;
use lib qw(e:\\workplace);
use CGI qw/standart/;
use CGI::Carp qw/fatalsToBrowser/;
use DBI;
use dbgrid;
sub draw_callback
{
my ($field,$key,$value) = @_;
return "Callback says $value";
}
my $db = DBI->connect( "DBI:mysql:blogbase:localhost", "root", "pwd" ) or
croak "MySQL seems to be down, or something is fubared.";
my $sql = "SELECT * FROM Record";
my $eventscript = "test.pl";
my $key = "RecordId";
my (%column1,%column2);
$column1{Type} = "ctLabel";
$column1{Title} = "ID";
$column1{FieldName} = "RecordId";
$column2{Type} = "ctCallback";
$column2{Title} = "TITLE";
$column2{FieldName} = "Title";
my @columns = (\%column1,\%column2);
my $test = DBGrid->create($db,$sql,$eventscript,\@columns,$key);
$test->SetCallbacks(\&draw_callback,0,0);
$test->output_columns();
Here's my module in its current state (unimportant pieces have been cut out)
package DBGrid;
use strict;
use CGI qw/standart/;
use CGI::Carp qw/fatalsToBrowser/;
use DBI;
my %props;
###############################################################
sub output_columns
{
my $columns = $props{columns};
my ($header,$footer,@rows) = ("","",0);
#
# ...
#
my $i = 0;
while ( my $row = $props{query}->fetchrow_hashref() )
{
#
# ...
#
for my $column (@$columns)
{
#
# ...
#
my $type = $$column{Type};
if ( !$$column{Custom} )
{
if ( $type eq "ctLabel" )
{
$rows[$i] .= $$row{$$column{FieldName}};
}
#
# ...
#
if ( $type eq "ctCallback" )
{
#!!!!!!!!!!!!! error here
$rows[$i] .=
$props{DrawCallback}($$column{FieldName},$key,$$row{$$column{FieldName}});
}
}
else
{
#
# ...
#
}
#
# ...
#
++$i;
}
print $header;
print join('',@rows) if ($#rows+1);
print $footer;
}
###############################################################
sub create
{
shift;
($props{db},$props{sql},$props{EventScript},$props{columns},$props{KeyField}
) = @_;
$props{query} = $props{db}->prepare($props{sql}) or croak "Error
initialising database object: ".$props{db}->errstr;
$props{query}->execute() or croak "Error executing SQL statement :
".$props{db}->errstr;
build_columns() if !defined $props{columns} or !$props{columns};
my $self = \%props;
bless $self;
return $self;
}
###############################################################
sub build_columns
{
#
# ...
#
}
###############################################################
sub SetCallbacks
{
#improve to ignore zeros
($props{DrawCallback},$props{CustomCallback},$props{OutputCallback}) = @_;
}
###############################################################
return 1;
DBGrid thing...
However, I have osmething messed up and I cannot figure out what is wrong.
#!!!!!!!!!!!!!
line indicates the problem. Problem is that, even though function reference
seems to be passed and everything seems to be ok, it doesn't launch, and
instead, the script is terminating with errors.
What am I doing wrong?
Here's the test script
use strict;
use lib qw(e:\\workplace);
use CGI qw/standart/;
use CGI::Carp qw/fatalsToBrowser/;
use DBI;
use dbgrid;
sub draw_callback
{
my ($field,$key,$value) = @_;
return "Callback says $value";
}
my $db = DBI->connect( "DBI:mysql:blogbase:localhost", "root", "pwd" ) or
croak "MySQL seems to be down, or something is fubared.";
my $sql = "SELECT * FROM Record";
my $eventscript = "test.pl";
my $key = "RecordId";
my (%column1,%column2);
$column1{Type} = "ctLabel";
$column1{Title} = "ID";
$column1{FieldName} = "RecordId";
$column2{Type} = "ctCallback";
$column2{Title} = "TITLE";
$column2{FieldName} = "Title";
my @columns = (\%column1,\%column2);
my $test = DBGrid->create($db,$sql,$eventscript,\@columns,$key);
$test->SetCallbacks(\&draw_callback,0,0);
$test->output_columns();
Here's my module in its current state (unimportant pieces have been cut out)
package DBGrid;
use strict;
use CGI qw/standart/;
use CGI::Carp qw/fatalsToBrowser/;
use DBI;
my %props;
###############################################################
sub output_columns
{
my $columns = $props{columns};
my ($header,$footer,@rows) = ("","",0);
#
# ...
#
my $i = 0;
while ( my $row = $props{query}->fetchrow_hashref() )
{
#
# ...
#
for my $column (@$columns)
{
#
# ...
#
my $type = $$column{Type};
if ( !$$column{Custom} )
{
if ( $type eq "ctLabel" )
{
$rows[$i] .= $$row{$$column{FieldName}};
}
#
# ...
#
if ( $type eq "ctCallback" )
{
#!!!!!!!!!!!!! error here
$rows[$i] .=
$props{DrawCallback}($$column{FieldName},$key,$$row{$$column{FieldName}});
}
}
else
{
#
# ...
#
}
#
# ...
#
++$i;
}
print $header;
print join('',@rows) if ($#rows+1);
print $footer;
}
###############################################################
sub create
{
shift;
($props{db},$props{sql},$props{EventScript},$props{columns},$props{KeyField}
) = @_;
$props{query} = $props{db}->prepare($props{sql}) or croak "Error
initialising database object: ".$props{db}->errstr;
$props{query}->execute() or croak "Error executing SQL statement :
".$props{db}->errstr;
build_columns() if !defined $props{columns} or !$props{columns};
my $self = \%props;
bless $self;
return $self;
}
###############################################################
sub build_columns
{
#
# ...
#
}
###############################################################
sub SetCallbacks
{
#improve to ignore zeros
($props{DrawCallback},$props{CustomCallback},$props{OutputCallback}) = @_;
}
###############################################################
return 1;