function reference errors...

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;
 
A

A. Sinan Unur

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/;

That should be:

use CGI qw /standard/;

Did you copy and paste this from the actual source you are running or did
you type it in to your newsreader?

I am going to stop reading your code at this point.
sub draw_callback
{
my ($field,$key,$value) = @_;
return "Callback says $value";
}

Please properly indent your code if you want others to read your code.
package DBGrid;

use strict;
use CGI qw/standart/;

Same here.
 
B

Bob Walton

Sergei said:
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 errors, *exactly*, would those be? We can't read your mind.

What am I doing wrong?


The error messages you don't bother to mention probably offer some hints
about that.

Here's the test script
use strict;
use lib qw(e:\\workplace);
use CGI qw/standart/;

:standard----^^^^^^^^

Correct spelling is very important. And so is the leading : .

use CGI::Carp qw/fatalsToBrowser/;
use DBI;
use dbgrid;

DBG---^^^
Below you define this as DBGrid. Case is important, even on Windoze.
Maybe more important, because Perl will find the module file due to the
case-insensitive file system and won't give that error, but the package
won't work -- but there is no error message.


....
Here's my module in its current state (unimportant pieces have been cut out)

package DBGrid;

use strict;
use CGI qw/standart/;

:standard----^^^^^^^^

You did it again.

use CGI::Carp qw/fatalsToBrowser/;
use DBI;

my %props;
....


I didn't go over the rest of your code in detail -- I didn't immediately
see anything else. Maybe it will work with those fixes?

It would really help if you could whittle the posted code down to a
minimal-length script that exhibits the problem and can be
copy/paste/executed by anyone. I suspect, for example, that all the
database stuff is irrelevant to your problem, as is the fact that it is
a CGI script. And, probably, in the course of doing that, you will
discover the source of the error for yourself.
 
S

Sergei Shelukhin

Ok, I fixed the callback problem, I forgot that the first parameter passed
to a method is the object itself in SetCallbacks... Thanks for corrections
too, I typoed standard in the "use" statement and copy-pasted it to test
script; I didn't get to using it anyway (thus far), so I didn't notice the
error.

Now, there's a version problem. I have ActiveState Perl installed both at
work and at home; of course, both boxes are running Windows, and the script
is working as it should.
However, when I tried launching the script on my Linux box (Perl version is
5.005_03), it returned syntax errors near "})" (no additional comments, the
error is just "syntax error").
The line where the error resides is:

$rows[$i] .=
$props{DrawCallback}($$column{FieldName},$key,$$row{$$column{FieldName}});

How do I make this version compartable? I tried googling it, but i canot
really come up with good word combo to find something appropriate.
 
T

Tad McClellan

Sergei Shelukhin said:
Now, there's a version problem. I have ActiveState Perl installed both at
work and at home; of course, both boxes are running Windows, and the script
is working as it should.


Hmmm, I dunno what's going on with that...

However, when I tried launching the script on my Linux box (Perl version is
5.005_03), it returned syntax errors near "})" (no additional comments, the
error is just "syntax error").
The line where the error resides is:


The line reported in the error message is NOT necessarily the
line where the error is.

The line reported is the line where perl _noticed_ that there
is a syntax error. The actual error may be several lines before
the line reported.

$rows[$i] .=
$props{DrawCallback}($$column{FieldName},$key,$$row{$$column{FieldName}});
^^
^^

Don't you need to _de_reference the coderef?


$props{DrawCallback}->( ... );
^^
^^
 
A

Anno Siegel

Tad McClellan said:
Sergei Shelukhin said:
Now, there's a version problem. I have ActiveState Perl installed both at
work and at home; of course, both boxes are running Windows, and the script
is working as it should.


Hmmm, I dunno what's going on with that...

However, when I tried launching the script on my Linux box (Perl version is
5.005_03), it returned syntax errors near "})" (no additional comments, the
error is just "syntax error").
The line where the error resides is:


The line reported in the error message is NOT necessarily the
line where the error is.

The line reported is the line where perl _noticed_ that there
is a syntax error. The actual error may be several lines before
the line reported.

$rows[$i] .=
$props{DrawCallback}($$column{FieldName},$key,$$row{$$column{FieldName}});
^^
^^

Don't you need to _de_reference the coderef?


$props{DrawCallback}->( ... );
^^
^^

In new-ish Perls you don't, apparently. I'm not too surprised, it's
in the spirit of leaving out the arrow between brackets, which has
been around all along. But I'm not too surprised either that this
doesn't work for subrefs in earlier versions. The documentation in
perlref only talks about [] and {}.

To the OP: Do both, add the arrow, and upgrade Perl on that machine.
The arrow-dropping mechanism should be used sparingly. As a rule,
use it only when a series of arrows can be removed, not for a single
one.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top