Converting my index.pl(cgi) to html::template one

N

Nikos

!!! Finally i made it !!!
I accept congratulations :)

Here is th html template version of my index.pl which troubled me many
ma days! After reading and reading what you told me i finally get
succeeded!

Here it is:

<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-7" />
<title>Ψυχωφελή Πνευματικά Κείμενα!</title>
<link rel="stylesheet" type="text/css"
href="../data/css/style.css" />
<script type="text/javascript" language="javascript">
// <![CDATA[
var textToShow = "<TMPL_VAR name=data>";
var tm;
var pos = 0;
var counter = 0;

function init()
{ tm = setInterval("type()", 50) }

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>
</head>


<body onload=init()>
<div id=page>

<br>

<div id=select_form>
<form action="index.pl">Διάλεξε ένα από τα κείμενα για να διαβάσεις =>
<select name="select">
<TMPL_LOOP name=files>
<option value="<TMPL_VAR name=file>"><TMPL_VAR name=file></option>
</TMPL_LOOP>
</select>
<input type="submit" name="select" value="Επιλογή" />
</form>
</div>

<div id="DivText"></div>

<div id=user_form>
<form action="show.pl">
<table class="user">
<tr><td>Πώς σε λένε αδελφέ?</td><td class="two"><input name="onoma"
type="text" /></td></tr>
<tr><td>Ποιά είναι η γνώμη σου για την ευχοÏλα »ΚÏÏιε Î™Î·ÏƒÎ¿Ï Î§Ïιστέ,
Ελέησον Με« ?</td><td class="two"><textarea name="euxoula" rows="4"
cols="25"></textarea></td></tr>
<tr><td>ΠεÏιέγÏαψε μας, μία Ï€Ïοσωπική σου πνευματική εμπειÏία από
κάποιον γέÏοντα, Ï€Ïος ώφελος των υπολοίπων αδελφών ( αν φυσικά έχεις
:)</td><td class="two"><textarea name="sxolio" rows="6"
cols="25"></textarea></td></tr>
<tr><td>Ποιό είναι το email σου?</td><td class="two"><input name="email"
type="text" /></td></tr>
<tr><td><input type="submit" name="show" value="Εμφάνιση όλων των
απόψεων" /></td><td class="two"><input type="submit" name="send"
value="Αποστολή" /></td></tr>
</table>
</form>
</div>

<div id=tip><TMPL_VAR NAME=tip></div>

<div id=info><table>
<tr><td class="host"><TMPL_VAR NAME=host></td></tr>
<tr><td class="xronos"><TMPL_VAR NAME=xronos></td></tr>
<tr><td class="counter"><TMPL_VAR NAME=counter></td></tr>
</table>
</div>

<div id=links>
<a href="games.pl"><img src="../data/images/games.gif" /></a>
<p><a href="show.pl?onoma=showlog" name="showlog">Last Update:
25/4/2005</a></p>
</div>

</div>
</body>
</html>

and here is the index.pl file now in a pure format written since its
only code and with subs this time :)

#!/usr/bin/perl
use strict;
use warnings;

use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use HTML::Template;
use vars qw ($sec $min $hour $mday $mon $year $wday $yday $isdst);

print header;

my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec');
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
my $xronos = "$mday $months[$mon], $hour:$min";


my $dbh = ($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 die $DBI::errstr;

my ($match, $host) = get_host( $ENV{'REMOTE_ADDR'} );

my $data;
if( param('select') ) {
$data = open_keimeno( $host, param('select') );
} elsif( $match ) {
$data = welcome_old( $host );
} else {
$data = welcome_new( $host );
}

$data =~ s/\n/\\n/g;
$data =~ s/"/\\"/g;
$data =~ tr/\cM//d;

my $sth;
my $row;

$dbh->do( "UPDATE logs SET page = page + 1" ) if ($host ne "Ãßêïò");
$sth = $dbh->prepare( "SELECT page FROM logs" );
$sth->execute();
$row = $sth->fetchrow_hashref;

my $tmpl = HTML::Template->new(filename=>'./index.html');
$tmpl->param(
files => &select_form(),
tip => &tips(),
data => $data,
host => $host,
xronos => $xronos,
counter => $row->{'counter'}
);
print $tmpl->output;

sub get_host {
my ($url) = @_;

my @numbers = split (/\./,$url);
my $address = pack ("C4", @numbers);
my $host = gethostbyaddr ($address, 2) || $url;
$host = "Ãßêïò" if ( ($host eq "localhost") or ($host eq
"dslcustomer-221-228.vivodi.gr") or ($host eq "dell") );

my $ref = $dbh->selectall_hashref( "SELECT host FROM logs",
'host');
if (exists $ref->{$host}) {
return (1, $host);
} else {
return (0, $host);
}
}

sub open_keimeno {
my ($host, $file) = @_;

my $keimeno = $file;
open(FILE, "<../data/text/$keimeno.txt") or die $!;
my @data = <FILE>;
close(FILE);

my $data = join('', @data);
$dbh->do( "UPDATE logs SET keimeno='$keimeno' WHERE
host='$host'" );
$data;
}

sub welcome_old {
my ($host) = @_;

$dbh->do( "UPDATE logs SET visits = visits + 1 WHERE
host='$host'" );
my $sth = $dbh->prepare( "SELECT * FROM logs WHERE host='$host'" );
$sth->execute();
my $row = $sth->fetchrow_hashref;

return "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçí
óåëßäá åíäéáöÃñïõóá!\n".
"Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
..$row->{xronos}. " !!\n".
"ÓýíïëéêÃò Þñèåò åäþ " .$row->{visits}. " öïñÃò !!!\n".
"Ôåëåõôáßá åßäåò ôï êåßìåíï { " .$row->{keimeno}. " }\n".
"Ãïéü êåßìåíï èá ìåëåôÞóåéò áõôÞí ôçí öïñÜ !?";
}

sub welcome_new {
my ($host) = @_;

my $data;
if ( $host ne "Ãßêïò" ) {
$data = "Ãåéá óïõ " .$host. "!\n" .
"¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
"Åëðßæù íá âñåéò ôá êåßìåíá åíäéáöÃñïíôá !!!";
my $text;
$dbh->do( "INSERT INTO logs VALUES (null, '$host',
'$xronos', '$text', 1)" );
} else {
$data = "Ãåéá óïõ Ãéêüëá, ôé ÷áìðÜñéá !?! :)";
}
$data;
}

sub select_form {
my @files = </data/text/*.txt>;
my @display_files = map {file=>/([^\/]+)\.txt/}, @files;
\@display_files;
}

sub tips {
open(FILE, '<../data/text/tips') or die $!;
my @tips = <FILE>;
close(FILE);

@tips = grep { !/^\s*\z/s } @tips;
$tips[int(rand(@tips))];
};

It works ok! Teh only problem is that if i change the relative path from
this open(FILE, '<../data/text/tips') or die $!;

to this absolute one
open(FILE, '</data/text/tips') or die $!;

then the interpreter gives an error opening the file.... thats funyn
because other absolute parts work!

Gee, i am so happy i finalyl made it work!
 
M

Mark Clements

Nikos said:
!!! Finally i made it !!!
I accept congratulations :)

Here is th html template version of my index.pl which troubled me many
ma days! After reading and reading what you told me i finally get
succeeded!
I'm glad all your hard work paid off. No, seriously.

sub tips {
open(FILE, '<../data/text/tips') or die $!;
my @tips = <FILE>;
close(FILE);

@tips = grep { !/^\s*\z/s } @tips;
$tips[int(rand(@tips))];
};

It works ok! Teh only problem is that if i change the relative path from
this open(FILE, '<../data/text/tips') or die $!;

to this absolute one
open(FILE, '</data/text/tips') or die $!;

then the interpreter gives an error opening the file.... thats funyn
because other absolute parts work!
Well: is there such a file as "/data/text/tips"? Can you see it with ls?
Come to think of it, why am I asking this? You haven't once responded to
the many questions like this I have asked.

On a point of maintainability: "/data/text/" should be a constant at the
top of your file, since it is repeated everywhere. I'm not going to
bother explaining why constants are a good idea, since you won't read or
take on-board the explanation.

As far as I am concerned, you are now on your own. I give up trying to
help you.

Mark
 
N

Nikos

Mark said:
Well: is there such a file as "/data/text/tips"?

Of course! but it only works if set the path in relative way.
in absolute way it aint working.
The other paths that are absolute too work ok/
 
T

Tad McClellan

Nikos said:
use vars qw ($sec $min $hour $mday $mon $year $wday $yday $isdst);


You should always prefer lexical variables over package variables,
except when you can't.


And you can, so that should be:

my($sec $min $hour $mday $mon $year $wday $yday $isdst);
 
N

Nikos

Tad said:
You should always prefer lexical variables over package variables,
except when you can't.


And you can, so that should be:

my($sec $min $hour $mday $mon $year $wday $yday $isdst);

Thank you. its better and quicker this way :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top