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

Hello.. I thought I would try to write a generic table object but i'm running into problems.. no error codes are being sent back and i'm to new to oop in perl to know what I'm doing wrong, any help would be greatly appreciated..(any suggestions for improvement would be welcome too) below is my attempt at the code:
package Table; # Generic HTML Table Object #define class sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{TABLE_OPEN} = undef; $self->{TABLE_CLOSE} = undef; $self->{ROW_OPEN} = undef; $self->{ROW_CLOSE} = undef; $self->{COL_OPEN} = undef; $self->{COL_CLOSE} = undef; $self->{NAME} = undef; $self->{VALUE} = undef; bless ($self, $class); return $self; } ################################################# # begin table methods sub table_open { my $self = shift; if (@_) { $self->{TABLE_OPEN} = shift } return print "<table>\n"; } sub table_close { my $self = shift; if (@_) { $self->{TABLE_CLOSE} = shift } return print "</table>\n"; } sub row_open { my $self = shift; if (@_) { $self->{ROW_OPEN} = shift } return print "\t<tr>\n"; } sub row_close { my $self = shift; if (@_) { $self->{ROW_CLOSE} = shift } return print "\t</tr>\n"; } sub col_open { my $self = shift; if (@_) { $self->{COL_OPEN} = shift } return print "\t\t<td>\n"; } sub col_close { my $self = shift; if (@_) { $self->{COL_CLOSE} = shift } return print "\t\t</td>\n"; } sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME}; } sub value { my $self = shift; if (@_) { $self->{VALUE} = shift } return $self->{VALUE}; } 1; ################################################ # begin script that i'm using to test the object #!/usr/bin/perl require 5; use Web; # my web processing module use Table; $website = ''; &parse(); # parse html post/get using the Web module &check_referer($website); # check refering url using the Web module &check_required('test'); # check required fields using the Web module print "Content-type:text/html\n\n"; print "<html><body>\n"; print "test = " . $in{'test'} . "\n"; my($table) = new Table(); $table->open_table; foreach $i (sort keys %in){ $table->open_row; $table->open_col; $table->name($i); $table->name; $table->close_col; $table->open_col; $table->value($in{$i}); $table->value; $table->close_col; $table->close_row; } $table->close_table; print "got here\n"; exit;

Replies are listed 'Best First'.
Re: new to oop (problem with my object)
by btrott (Parson) on Nov 17, 2000 at 02:11 UTC
    You're calling the methods open_row, open_table, etc. but in your Table class definition you called them row_open, table_open, etc.! That's not going to work.

    Plus everything Fastolfe said.

    Besides, you have a real mess in your methods: some of them call print to print out your table tags, and others simply return values. That's not going to work--decide on one or the other and stick to it. If I were you I'd have all my methods return data rather than printing it out, because it's more flexible that way.

    So, for example, your row_open method prints out "<tr>", but then you set a value using the "name" method, then attempt to do something with it, and the name method just returns that value. And you don't do anything with it.

      Which tells me there should have been a much more serious problem with the script than he was leading on.

      If you're getting "Internal Server Error", the first place you need to go is the web server's error logs (e.g. /var/log/httpd/error_log), which should contain full STDERR output from your script. An alternative is to use another module in the CGI arsenal:

      use CGI::Carp 'fatalsToBrowser';
      This would send errors to your web browser in addition to the server's log files, greatly aiding in your CGI development. In the future, it would help if you would tell us what you are seeing, in addition to simply what you aren't seeing.
        ahhh! i appreciate you guys looking at it..the printing of the html is actually working well now.. the 'fatalsToBrowser' told me what btrott was telling me about my mis-named method calls....it also appears as though i need to dereference my hash? I'm getting Table=HASH(0x100da2e8)->value when it does the following print: print "$table->value\n";
Re: new to oop (problem with my object)
by Fastolfe (Vicar) on Nov 17, 2000 at 02:06 UTC
    Have you tried using some standard pre-troubleshooting techniques? Specifically, 'use strict' and running Perl with the -w flags to catch warnings?

    The code looks fine to me at a first glance. Have you tested your assumptions? Does %in exist and have the keys/values you're expecting? Try turning buffering off ($|=1).

    Also, is there some reason you're not using the CGI module for all of this? It has built-in form processing and HTML generation code. Looks like you're trying to re-invent it here.

      I've tried -w flags and use strict and %in does exist. I turned the buffer off re your suggestion and nothing seemed to change..could be my own ignorance.. i'm not really familiar with the cgi module, but i was writing this myself so that it could be small portable and reusable for all my scripts cleaning up all the junky table code i've been writing..i'll look at the CGI mod again..but i'm resistant to change! :)