in reply to trouble dereferencing a hash reference
Welcome to the Monastery! Congratulations on solving the problem, and thanks for updating the OP accordingly.
I couldn't help but notice the subroutine in your code that generates a table in HTML. While this may work, it is usually a better idea to separate the logic code from the display so you can more easily change one without having to worry about the other. In this case, I'd recommend using an HTML templating module, such as HTML::Template. There is a very small learning curve, and once you try it you'll be hooked. :-)
Here is a quick example. I put the template in a separate file, but you can just as easily keep it in the same file as the rest of your code (see the docs). You can print the resulting HTML directly to a file or you can store it in another variable (and return it from a subroutine, as in your case).
use strict; use warnings; use HTML::Template; my $template = HTML::Template->new( filename => 'html_template.tmpl' ) +; # set the VAR1 parameter $template->param( VAR1 => 'this is var 1' ); # create data for the table and set TESTLOOP my @loopdata; for( 0 .. 3 ) { push( @loopdata, { cell1 => "row $_ cell 1", cell2 => "row $_ cell 2" } ); } $template->param( TESTLOOP => \@loopdata ); # print to output file open( my $outfh, '>', 'html_template_output.html' ) or die "error opening output file:\n$!"; print $outfh $template->output(); close $outfh; # get output as a string of HTML my $output = $template->output(); print $output;
Here is the template file:
<html> <head> <title>This is a test template for HTML::Template</title> </head> <body> Var1 = <TMPL_VAR NAME=VAR1> <br> Loop: <br> <table> <TMPL_LOOP NAME=TESTLOOP> <tr> <td>Cell 1 = <TMPL_VAR NAME=CELL1></td> <td>Cell 2 = <TMPL_VAR NAME=CELL2></td> </tr> </TMPL_LOOP> </table> </body> </html>
And the output:
<html> <head> <title>This is a test template for HTML::Template</title> </head> <body> Var1 = this is var 1 <br> Loop: <br> <table> <tr> <td>Cell 1 = row 0 cell 1</td> <td>Cell 2 = row 0 cell 2</td> </tr> <tr> <td>Cell 1 = row 1 cell 1</td> <td>Cell 2 = row 1 cell 2</td> </tr> <tr> <td>Cell 1 = row 2 cell 1</td> <td>Cell 2 = row 2 cell 2</td> </tr> <tr> <td>Cell 1 = row 3 cell 1</td> <td>Cell 2 = row 3 cell 2</td> </tr> </table> </body> </html>
I hope this helps. There is a lot of information around here (and CPAN) on templating. Super Search is your friend. :-)
|
|---|