Fabian Pilkowski wrote:
[snip explained code]
Thanks a lot Fabian now its more clear to me but i still cant convert
this index.pl script i made with the use of cgi.pm to the HTML::Template
way. Here is is btw:
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
use CGI::Cookie;
use CGI qw
standard);
use DBI;
use DBD::mysql;
@months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec');
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
$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' );
print start_html( -style=>'../style.css', -title=>'Øõ÷ùöåëÞ ÐíåõìáôéêÜ
Êåßìåíá!' );
$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', 'tiabhp2r')
or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0;
#*******************************************************************************
@files = <../data/text/*.txt>;
@display_files = map( /([^\/]+)\.txt/, @files );
print start_form(-action=>'index.pl');
print p( {-align=>'center'}, font( {-size=>5, -color=>'Lime'},
'Ëüãïò Øõ÷ùöåëÞò êáé ÈáõìÜóéïò => ' ),
popup_menu( -name=>'select',
-values=>\@display_files ),
submit('ok'));
print end_form(), br();
$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(FH, "<../data/text/$keimeno.txt") or die $!;
@data = <FH>;
close(FH);
$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}. " óôéò "
..$row->{xronos}. " !!\n" .
"ÁõôÞ åßíáé ç " .$row->{visits}. " öïñÜ ðïõ åðéóêÝöôåóáé ôçí
óåëßäá ìïõ êáé ÷áßñïìáé ðïëý ðïõ ôçí âñßóêåéò åíäéáöÝñïõóá !!!\n" .
"Ôçí ôåëåõôáßá öïñÜ äéÜâáóåò ôï êåßìåíï [ "
..$row->{keimeno}. " ]\n" .
"Ðïéo ëüãï èá Þèåëåò íá ìåëåôÞóåéò áõôÞí ôç öïñÜ ?!?";
}
elsif( $hostmatch != 1 )
{
if ( $host ne "ÁÖÅÍÔÉÊÏ" )
{
$data = "Êáëþò üñéóåò " .$host. "!\n" .
"ÁõôÞ åéíáé ç 1ç óïõ åðßóêåøç åäþ ðÝñá! Åëðßæù íá óïõ
áñÝóåé!\n" .
"Áíïßãïíôáò ôï ðáñáðÜíù drop-down menu åðÝëåîå ôï êåßìåíï
ðïõ èá Þèåëåò íá äéáâÜóåéò...";
$db->do( "INSERT INTO logs VALUES (null, '$host', '$xronos',
'$text', 1)" ) or die $db->errstr;
}
else
{
$data = "Ôé ÷áìðÜñéá $host
ÈõìÞèçêåò íá ìå ðåñéðïéçèåßò êáé
åìÝíá ëéãÜêé !?!";
}
}
$data =~ s/\n/\\n/g;
$data =~ s/"/\\"/g;
$data =~ tr/\cM//d;
#*******************************************************************************
print <<ENDOFHTML;
<html><head><title></title>
<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.getElementById("DivText").innerHTML = '';
tm =
setInterval("type()", 50);
document.body.onkeypress = 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
ENDOFHTML
#*******************************************************************************
print br(), br(), br();
print start_form(-action=>'show.pl');
print table(
Tr( {-align=>'center'}, td( "Ðþò óå ëÝíå áäåëöÝ?" ),
td( textfield( 'onoma' ))),
Tr( {-align=>'center'}, td( "ÐïéÜ åßíáé ç ãíþìç óïõ ãéá ôçí åõ÷ïýëá
\"Êýñéå Éçóïý ×ñéóôÝ ÅëÝçóïí Ìå\"?"
), td( textarea( -name=>'euxoula', -rows=>4, -columns=>25 ))),
Tr( {-align=>'center'}, td( "ÌïéñÜóïõ ìáæß ìáò ìßá êáôÜ ôç ãíþìç óïõ
èáõìáóôÞ ðñïóùðéêÞ ðíåõìáôéêÞ åìðåéñßá
áðü êÜðïéïí ãÝñïíôá ðñïò þöåëïò ôùí
õðïëïßðùí áäåëöþí (áí öõóéêÜ Ý÷åéò
" ), td( textarea( -name=>'sxolio', -rows=>6, -columns=>25 ))),
Tr( {-align=>'center'}, td( "Ðïéü åßíáé ôï e-mail óïõ?" ),
td( textfield( 'email' ))),
Tr( {-align=>'center'}, td( submit( 'ÅìöÜíéóç üëùí ôùí áðüøåùí'
)), td( submit( 'ÁðïóôïëÞ' ))),
);
print end_form(), br(), br();
open(FH, "<../data/text/tips") or die $!;
@tips = <FH>;
close(FH);
@tips = grep { !/^\s*\z/s } @tips;
$tip = $tips[int(rand(@tips))];
print table(
Tr( td( {class=>'tip'}, $tip ))
);
$db->do( "UPDATE counter SET counter = counter + 1" ) if ($host ne
"ÁÖÅÍÔÉÊÏ");
$st = $db->prepare( "SELECT counter FROM counter" );
$st->execute();
$row = $st->fetchrow_hashref;
print br();
print table(
Tr( td( {class=>'host'}, $host )),
Tr( td( {class=>'xronos'}, $xronos )),
Tr( td( {class=>'counter'}, $row->{counter} ))
);
print a( {href=>'games.pl'},
img{src=>'../data/images/games.gif'} );
print p( {-align=>'right'}, a( {href=>'show.pl?onoma=showlog'}, font(
{-size=>2, -color=>'Lime'}, b( 'Last Update: 18/4/2005' ))));
What i am trying to figure out is if the above script would be more
straightforward with the use of HTML::Template.
I tried to do ti but i didnt managed it....as expected.
The idea is to Separate the html from the pure perl code(here we dont
have html really since i use cgi.pm) but by trying that i *must* write
from scratch the pure html code(which i am trying to avoid) of the
script and the pure perl code which of course this time would be much
smaller. But i also have Javascript inserted here....
Do you really think that the above script can be rewritten in a more
straightforward way considering the use of the methods we discussed?
If yes, can you guys help me convert it so to see if it fits me better...
I tried a little according to the directives you provided, but because i
made a lot of errors i stopped before i hit the keyboard and screen.