Hello, iam trying to convert tmy webpage from with the use of cgi.pm with use of html template to sepoarate perl code from html. here is the perl so far
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI::Cookie; use CGI qw(:standard); use DBI; use DBD::mysql; use HTML::Template; $tmpl = HTML::Template->new(filename => 'index.html'); @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Se +p', 'Oct', 'Nov', 'Dec'); ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localt +ime; $xronos = "$mday $months[$mon], $hour:$min"; $ip = $ENV{'REMOTE_ADDR'}; @numbers = split (/\./,$ip); $address = pack ("C4", @numbers); $host = gethostbyaddr ($address, 2) || $ip; $host = "Νίκος" if ( ($host eq "localhost") or ($host eq "dslcustomer- +221-228.vivodi.gr" or $host eq "dell") ); print header( -charset=>'iso-8859-7' ); $db = ($ENV{'SERVER_NAME'} ne 'nikolas.50free.net') ? DBI->connect('DBI:mysql:nikos_db', 'root', '') : DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db', '***** +***') or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0 +; #********************************************************************* +********** @files = <../data/text/*.txt>; @display_files = map( /([^\/]+)\.txt/, @files ); $keimeno = param('select') or "Αρχική Σελίδα"; $st = $db->prepare( "SELECT host FROM logs" ); $st->execute(); while( $row = $st->fetchrow_hashref ) { if( $host eq $row->{host} ) { $hostmatch = 1; } } if( param('select') and param('select') !~ /\.\./ ) { open(FILE, "<../data/text/$keimeno.txt") or die $!; @data = <FILE>; close(FILE); $data = join('', @data); $db->do( "UPDATE logs SET keimeno='$keimeno' WHERE host='$host'" ) + or die $db->errstr; } elsif( $hostmatch == 1 ) { $db->do( "UPDATE logs SET visits = visits + 1 WHERE host='$host'" +) or die $db->errstr; $st = $db->prepare( "SELECT * FROM logs WHERE host='$host'" ); $st->execute(); $row = $st->fetchrow_hashref; $data = "Καλώς ήλθες " .$host. "! Χαίρομαι που βρίσκες την σελίδα +ενδιαφέρουσα!\n" . "Τελευταία φορά ήρθες εδώ ως " .$row->{host}. " στις " .$r +ow->{xronos}. " !!\n" . "Σύνολικές ήρθες εδώ " .$row->{visits}. " φορές!!!\n" . "Τελευταία είδες το κείμενο { " .$row->{keimeno}. " }\n" . "Ποιό κείμενο θα μελετήσεςι αυτήν τη φορά !?"; } elsif( $hostmatch != 1 ) { if ( $host ne "Νίκος" ) { $data = "Γεια σου " .$host. "!\n" . "Έρχεσαι για 1η φορά εδώ !!\n" . "Ελπίζω να βρεις τα κείμενα ενδιαφεροντα !!!"; $db->do( "INSERT INTO logs VALUES (null, '$host', '$xronos', '$ +text', 1)" ) or die $db->errstr; } else { $data = "Γεια σου Νικόλα, τι χαμπάρια !?! :-)"; } } $data =~ s/\n/\\n/g; $data =~ s/"/\\"/g; $data =~ tr/\cM//d; $tmpl->param( data => $data ); # To fill TMPL_LOOPs you have to pass a reference to an array with # references to hashes inside. And the inner hashes contain the data # for the TMPL_VARs inside the TMPL_LOOP. Well, it's a bit tricky. @display_files = map ( /([^\/]+)\.txt/, glob '../data/text/*.txt' ); @array_of_hashes = map { option => $_ }, @display_files; $tmpl->param( options => \@array_of_hashes ); open(FILE, "<../data/text/tips") or die $!; @tips = <FILE>; close(FILE); @tips = grep { !/^\s*\z/s } @tips; $tip = $tips[int(rand(@tips))]; $db->do( "UPDATE counter SET counter = counter + 1" ) if ($host ne "Νί +κος"); $st = $db->prepare( "SELECT counter FROM counter" ); $st->execute(); $row = $st->fetchrow_hashref; $tip = $tips[ int rand @tips ]; $tmpl->param( tip => $tip ); $tmpl->param(host => $host); $tmpl->param(xronos => $xronos); $tmpl->param(counter => $counter); # Appropriate headers go here print $tmpl->output;
and the html:
<html> <head> <title>&#936;&#965;&#967;&#969;&#966;&#949;&#955;&#942; &#928;&#95 +7;&#949;&#965;&#956;&#945;&#964;&#953;&#954;&#940; &#922;&#949;&#943; +&#956;&#949;&#957;&#945;!</title> <link rel="stylesheet" type="text/css" href="../style.css" /> <script language="JavaScript" tape="text/javascript"> var textToShow = "<TMPL_VAR name=data>"; <!-- ***************************************************************** +***** --> <script type='text/javascript'> var textToShow = "$data"; var tm; var pos = 0; var counter = 0; function init() { tm = setInterval("type()", 45) } function type() { if (textToShow.length != pos) { d = document.getElementById("DivText"); c = textToShow.charAt(pos++); if (c.charCodeAt(0) != 10) d.appendChild(document.createTextNode(c)); else d.appendChild(document.createElement("br")); counter++; if (counter >= 1800 && (c.charCodeAt(0) == 10 || c == ".")) { d.appendChild(document.createElement("br")); d.appendChild(document.createTextNode("Press any key...")); counter = 0; clearInterval(tm); document.body.onkeypress = function () { document.getElementB +yId("DivText").innerHTML = ''; tm = setInterval("ty +pe()", 50); document.body.onkeyp +ress = null; }; } } else clearInterval(tm); } </script> <body onload=init()> <center> <div id="DivText" align="Left" style=" background-image: url(../data/images/kenzo.jpg); border: Ridge Orange 5px; width: 850px; height: 500px; color: LightSkyBlue; font-family: Times; font-size: 18px;"> </div <!-- ***************************************************************** +***** --> </script> </head> <body onload="init()"> <form method="post" action="index.pl" enctype="application/x-www-f +orm-urlencoded"> <p align="center"> <font color="Lime" size="5">&#923;&#972;&#947;&#959;&#962; &#9 +36;&#965;&#967;&#969;&#966;&#949;&#955;&#942;&#962; &#954;&#945;&#953 +; &#920;&#945;&#965;&#956;&#940;&#963;&#953;&#959;&#962; => </font> <select name="select"> <TMPL_LOOP name=options> <option value="<TMPL_VAR name=@array_of_hashes>"><TMPL_VAR + name=@array_of_hashes></option> </TMPL_LOOP> </select> <input type="submit" name="ok" value="ok" /> </p> </form> <br /> <center> <div id="DivText" align="Left" style="background-image: url(../dat +a/images/kenzo.jpg); border: Ridge Orange 5px; width: 850px; height: 500px; color: +LightSkyBlue; font-family: Times; font-size: 18px;"></div> <br /><br /><br /> <form method="post" action="show.pl" enctype="application/x-www-fo +rm-urlencoded"> <table class="user"> <tr><td>&#928;&#974;&#962; &#963;&#949; &#955;&#941;&#957;&#94 +9; &#945;&#948;&#949;&#955;&#966;&#941;?</td> <td><input type="text" +name="onoma" /></td></tr> <tr><td>&#928;&#959;&#953;&#940; &#949;&#943;&#957;&#945;&#953 +; &#951; &#947;&#957;&#974;&#956;&#951; &#963;&#959;&#965; &#947;&#95 +3;&#945; &#964;&#951;&#957; &#949;&#965;&#967;&#959;&#973;&#955;&#945 +; *&#922;&#973;&#961;&#953;&#949; &#921;&#951;&#963;&#959;&#973; &#93 +5;&#961;&#953;&#963;&#964;&#941; &#917;&#955;&#941;&#951;&#963;&#959; +&#957; &#924;&#949;* ?</td> <td><textarea name="euxoula" rows="4" col +s="25"></textarea></td></tr> <tr><td>&#924;&#959;&#953;&#961;&#940;&#963;&#959;&#965; &#956 +;&#945;&#950;&#943; &#956;&#945;&#962; &#956;&#943;&#945; &#954;&#945 +;&#964;&#940; &#964;&#951; &#947;&#957;&#974;&#956;&#951; &#963;&#959 +;&#965; &#952;&#945;&#965;&#956;&#945;&#963;&#964;&#942; &#960;&#961; +&#959;&#963;&#969;&#960;&#953;&#954;&#942; &#960;&#957;&#949;&#965;&# +956;&#945;&#964;&#953;&#954;&#942; &#949;&#956;&#960;&#949;&#953;&#96 +1;&#943;&#945; &#945;&#960;&#972; &#954;&#940;&#960;&#959;&#953;&#959 +;&#957; &#947;&#941;&#961;&#959;&#957;&#964;&#945; &#960;&#961;&#959; +&#962; &#974;&#966;&#949;&#955;&#959;&#962; &#964;&#969;&#957; &#965; +&#960;&#959;&#955;&#959;&#943;&#960;&#969;&#957; &#945;&#948;&#949;&# +955;&#966;&#974;&#957; (&#945;&#957; &#966;&#965;&#963;&#953;&#954;&# +940; &#941;&#967;&#949;&#953;&#962; :-)</td> <td><textarea name="sxol +io" rows="6" cols="25"></textarea></td></tr> <tr><td>&#928;&#959;&#953;&#972; &#949;&#943;&#957;&#945;&#953 +; &#964;&#959; e-mail &#963;&#959;&#965;?</td> <td><input type="text" + name="email" /></td></tr> <tr><td><input type="submit" name="&#917;&#956;&#966;&#940;&#9 +57;&#953;&#963;&#951; &#972;&#955;&#969;&#957; &#964;&#969;&#957; &#9 +45;&#960;&#972;&#968;&#949;&#969;&#957;" value="&#917;&#956;&#966;&#9 +40;&#957;&#953;&#963;&#951; &#972;&#955;&#969;&#957; &#964;&#969;&#95 +7; &#945;&#960;&#972;&#968;&#949;&#969;&#957;" /></td> <td><input typ +e="submit" name="&#913;&#960;&#959;&#963;&#964;&#959;&#955;&#942;" va +lue="&#913;&#960;&#959;&#963;&#964;&#959;&#955;&#942;" /></td></tr> </table> <div></div> </form> <br /><br /> <!-- table: tip --> <table class="quote"> <tr><td><TMPL_VAR name=tip></td></tr> </table> <br /> <!-- table: host, date and counter --> <table class="info"> <tr><td><TMPL_VAR name=host></td></tr> <tr><td><TMPL_VAR name=xronos></td></tr> <tr><td><TMPL_VAR name=counter></td></tr> </table> <br /> <a href="games.pl"><img src="../data/images/games.gif" /></a> <p class="first"><a href="show.pl?onoma=showlog"><b>Last Update: 2 +1/4/2005</b></a></p> </body> </html>
For some reason the page wont correctly run. ithis is the far as i can go with it. even the value to tmpl_var=name wont appear when i run the script. please help me unstuck and thank you.

20050426 Edit by ysth: change title from "html::tempalte help"


In reply to HTML::Template help by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.