For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2004 > function reference errors...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author function reference errors...
Sergei Shelukhin

2004-07-23, 8:56 pm

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{EventScri
pt},$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{CustomCallb
ack},$props{OutputCallback}) = @_;
}

########################################
#######################
return 1;








A. Sinan Unur

2004-07-23, 8:56 pm

"Sergei Shelukhin" <raven_at@home.domonet.ru> wrote in
news:2md8o4FlusbqU1@uni-berlin.de:

> 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.

--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)

Bob Walton

2004-07-24, 3:56 am

Sergei Shelukhin wrote:

> 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.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

Sergei Shelukhin

2004-07-28, 9:00 pm

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.




Tad McClellan

2004-07-28, 9:00 pm

Sergei Shelukhin <raven_at@home.domonet.ru> wrote:

> 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}->( ... );
^^
^^

--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Anno Siegel

2004-07-28, 9:00 pm

Tad McClellan <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
> Sergei Shelukhin <raven_at@home.domonet.ru> wrote:
>
>
>
> Hmmm, I dunno what's going on with that...
>
>
>
>
> 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.
>
>
> ^^
> ^^
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com