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

So I created a nifty little market checking program for my website. I have no idea how to format the perl program though so I can have the info display in a table on a web page, and right now, it is just a line of information. How would i format this code to appear in a table? Or better yet, how could I have another html webpage pull in variable values after running this page? Here is my code:
# create object my $q = Finance::Quote->new(); # retrieve stock quote my %data = $q->fetch('NYSE', '^GSPC'); my %data1 = $q->fetch('NYSE', '^DJI'); my %data2 = $q->fetch('NYSE', '^FTSE'); my %data3 = $q->fetch('NYSE', '^HSI'); my %data4 = $q->fetch('NYSE', '^N225'); my %data5 = $q->fetch('NYSE', '^GDAXI'); my %data6 = $q->fetch('NYSE', 'GCQ09.CMX'); my %data7 = $q->fetch('NYSE', 'CLV09.NYM'); my %data8 = $q->fetch('NYSE', '^TNX'); # print price print "Nikkei 225: " . $data4{'^N225', 'net'}, "\n"; print "Hang Seng: " . $data3{'^HSI', 'net'}, "\n"; print "DAX: " . $data5{'^GDAXI', 'net'}, "\n"; print "FTSE 100: " . $data2{'^FTSE', 'net'}, "\n"; print "S&P 500: " . $data{'^GSPC', 'net'}, "\n"; print "Dow Jones: " . $data1{'^DJI', 'net'}, "\n"; print "Gold: " . $data6{'GCQ09.CMX', 'net'}, "\n"; print "Oil: " . $data7{'CLV09.NYM', 'net'}, "\n"; print "US 10 Year Treasury Note: " . $data8{'^TNX', 'net'}, "\n";
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: HTML Formatting of perl code
by marto (Cardinal) on Aug 25, 2009 at 15:01 UTC

    You could simply use something like HTML::Template to populate a template file with the values you have.

    Martin

Re: HTML Formatting of perl code
by ikegami (Patriarch) on Aug 25, 2009 at 14:40 UTC
    Some options:
    • Print out HTML written by hand.
    • Print out HTML generated from a template.
    • Print out HTML generated by HTML::Table or some other module.
    • Set the content type to text/plain since that's what you're outputting
Re: HTML Formatting of perl code
by Your Mother (Archbishop) on Aug 25, 2009 at 16:12 UTC

    The template advice you got already is best. There is also this-

    use CGI ":standard"; print div({-class => "quotes"}, div({-class => "row"}, span({-class => "label"},"Nikkei 225:"), span({-class => "value"}, $data4{'^N225', 'net'}), ), div({-class => "row"}, span({-class => "label"},"Hang Seng:"), span({-class => "value"}, $data3{'^HSI', 'net'}), ), div({-class => "row"}, span({-class => "label"},"DAX:"), span({-class => "value"}, $data5{'^GDAXI', 'net'}), ), div({-class => "row"}, span({-class => "label"},"FTSE 100:"), span({-class => "value"}, $data2{'^FTSE', 'net'}), ), div({-class => "row"}, span({-class => "label"},"S&P 500:"), span({-class => "value"}, $data{'^GSPC', 'net'}), ), div({-class => "row"}, span({-class => "label"},"Dow Jones:"), span({-class => "value"}, $data1{'^DJI', 'net'}), ), div({-class => "row"}, span({-class => "label"},"Gold:"), span({-class => "value"}, $data6{'GCQ09.CMX', 'net'}), ), div({-class => "row"}, span({-class => "label"},"Oil:"), span({-class => "value"}, $data7{'CLV09.NYM', 'net'}), ), div({-class => "row"}, span({-class => "label"},"US 10 Year Treasury Note:"), span({-class => "value"}, $data8{'^TNX', 'net'}), ), );

    The underlying point either way being: learn some HTML and some CSS (note the classes above). It's quite easy, fun, and resisting it will make spaghetti of your code.

Re: HTML Formatting of perl code
by leocharre (Priest) on Aug 25, 2009 at 15:58 UTC
    I'm down with marto

    Here's an example.. (untested!)

    use HTML::Template; my $src = q{ <h1>Stock data..</h1> <ul> <TMPL_LOOP STOCKLOOP> <li><TMPL_VAR NAME> <TMPL_VAR NET></li> </TMPL_LOOP> }; my $tmpl = HTML::Template->new( scalarref => \$src ); my %stock = ( 'Nikkei 225' => ^N225 ); my @STOCKLOOP; for my $name ( keys %stock ) { my $symbol = $stock{$name}; my %dat = $q->fetch( $name, $symbol ... # or whatever you do to get +this push @STOCKLOOP, { name => $name, net => $net }; } $tmpl->param( STOCKLOOP => \@STOCKLOOP ); print "Content-Type: text/html\n\n"; print $tmpl->output;
Re: HTML Formatting of perl code
by LanX (Saint) on Aug 25, 2009 at 14:42 UTC
    I recommend perltidy for HTML Formatting of perl code.

    Cheers Rolf