N
Nikos
Here is how my script has been transformed thanks to your precious
precious suggestions and mine's minor alternation:
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qwstandard);
use CGI::Cookie;
use DBD::mysql;
use DBI;
use POSIX qw(strftime);
print header( -charset=>'iso-8859-7' );
print start_html( -style=>'../data/css/style.css', -title=>'Øõ÷ùöåëÞ Ã
+ÃåõìáôéêÜ ÊåßìåÃá!', -background=>'../data/images/night.gif' );
my ($sth, $row);
my $date = strftime( "%d %b, %H:%M", localtime );
my $host = $ENV{'REMOTE_HOST'};
$host = "Ãßêïò" if ( ($host eq "dell") or ($host eq "localhost") or ($
+host ~= /vivodi.gr/) );
my $dbh = ($ENV{'SERVER_NAME'} ne 'nikolas.50free.net')
? DBI->connect('DBI:mysql:nikos_db', 'root', 'c
+ensored')
: DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db', 'c
+ensored')
or {RaiseError=>1};
#*********************************************************************
+**********
my @files = <../data/text/*.txt>;
my @display_files = map( /([^\/]+)\.txt/, @files );
print start_form(-action=>'index.pl');
print p( {-align=>'center'}, font( {-size=>5, -color=>'Lime'}, '
+ÄéÜëåîå ÃÃá áðü ôá êåßìåÃá ãéá Ãá äéáâÜóåéò => ' ),
popup_menu( -name=>'select', -value
+s=>\@display_files ),
submit('ÅðéëïãÞ'));
print end_form(), br();
my %sql = (
get_counter => "SELECT counter FROM visitorlog",
get_host => "SELECT host FROM visitorlog WHERE host=?",
update_visitor => "UPDATE visitorlog SET counter+=1 WHERE host=?",
update_passage => "UPDATE visitorlog SET passage=? WHERE host=?",
insert_host => "INSERT INTO visitorlog (null, host, date, passa
+ge, counter) VALUES (?, ?, ?, ?, ?, ?)"
);
my $passage = param('select') || "Ãñ÷éêÞ Óåëßäá!";
my ($data, @data);
if (param('select') and param('select') != '..')
{
open(FILE, "<../data/text/$passage.txt") or die $!;
@data = <FILE>;
close(FILE);
$data = join('', @data);
$sth = $dbh->prepare( $sql{update_passage} );
$sth->execute($passage, $host);
}
else
{
my $sth = $dbh->prepare( $sql{get_host} );
$sth->execute($host);
if ($sth->rows)
{
$sth = $dbh->prepare( $sql{update_visitor} );
$sth->execute($host);
$sth = $dbh->prepare( $sql{get_host} );
$sth->execute($host);
$row = $sth->fetchrow_hashref;
$data = "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçà óåë
+ßäá Ã¥ÃäéáöÃñïõóá!\n" .
"Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
+ .$row->{date}. " !!\n" .
"ÓýÃïëéêÃò Þñèåò åäþ " .$row->{counter}. " öïñÃò!!!\n"
+ .
"Ôåëåõôáßá åßäåò ôï êåßìåÃï { " .$row->{passage}. " }\
+n" .
"Ãïéü êåßìåÃï èá ìåëåôÞóåòé áõôÞà ôçà öïñÜ !?";
}
else
{
if ($host ne "Ãßêïò")
{
$data = "ÃåéÜ óïõ " .$host. "!\n" .
"¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
"Åëðßæù Ãá âñåßò ôá êåßìåÃá Ã¥ÃäéáöÃñïÃôá ";
$sth = $dbh->prepare( $sql{insert_host} );
$sth->execute(null, $host, $date, $passage, $counter);
}
else
{
$data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ Ãá óïõ ð
+ÃœÃÃ¥ ðÜÃôá! ;-)";
}
}
}
$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( {class=>'user'},
Tr( td( 'Ãþò óå ëÃÃÃ¥ áäåëöå?'
+ ), td( textfield( 'name' ))),
Tr( td( 'ÃïéÜ åßÃáé ç ãÃþìç óïõ ãéá ôçà åõ÷ïýëá
»Êýñéå Éçóïý ×ñéóôÃ, ÅëÃçóïà Ìå« ?'
+ ), td( textarea( -name=>'pray', -rows=>4, -columns=>25 ))),
Tr( td( 'ÃåñéÃãñáøå ìáò ìéá ðñïóùðéêÞ óïõ
ðÃåõìáôéêÞ åìðåéñßá áðü êÜðïéïà ãÃñïÃôá ðñïò
þöåëïò ôùà õðïëïßðùà áäåëöþà ( áà öõóéêÜ Ã÷åéò '
+ ), td( textarea( -name=>'remark', -rows=>6, -columns=>25 ))),
Tr( td( 'Ãïéü åßÃáé ôï email óïõ?'
+ ), td( textfield( 'email' ))),
Tr( td( submit( 'ÅìöÜÃéóç üëùà ôùà áðüøåùÃ'
+ )), td( submit( 'ÃðïóôïëÞ' ))),
);
print end_form(), br(), br();
open(FILE, "<../data/text/tips") or die $!;
my @tips = <FILE>;
close(FILE);
@tips = grep { !/^\s*\z/s } @tips;
my $tip = $tips[ int(rand(@tips)) ];
print table( {class=>'tip'}, Tr( td( {class=>'tip'}, $tip ))), br();
$sth = $dbh->prepare( $sql{update_counter} ) if ($host ne "Ãßêïò");
$sth->execute;
$sth = $dbh->prepare( $sql{get_counter} );
$sth->execute;
#here i decided to add together all the times visitors visit
#my webpage so to get a total counter in order to avoid
#create a new mysql table called counters and store that
#value there. Or even to a flat file!
while ($row = $sth->fetchrow_hashref)
{
$counter += $row->{counter};
}
print table( {class=>'info'},
Tr( td( {class=>'host'}, $host )),
Tr( td( {class=>'date'}, $date )),
Tr( td( {class=>'counter'}, $counter ))
);
print br(), a( {href=>'games.pl'}, img{src=>'../data/
+images/games.gif'} );
print p( {-align=>'right'}, a( {href=>'show.pl?name=showlog'}, font(
+ {-size=>2, -color=>'Lime'}, b( 'Last Update: 30/4/2005' ))));
How i can i shorter it, perfect it some more by dividing into subs?
What must be/not be in a sub?
precious suggestions and mine's minor alternation:
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qwstandard);
use CGI::Cookie;
use DBD::mysql;
use DBI;
use POSIX qw(strftime);
print header( -charset=>'iso-8859-7' );
print start_html( -style=>'../data/css/style.css', -title=>'Øõ÷ùöåëÞ Ã
+ÃåõìáôéêÜ ÊåßìåÃá!', -background=>'../data/images/night.gif' );
my ($sth, $row);
my $date = strftime( "%d %b, %H:%M", localtime );
my $host = $ENV{'REMOTE_HOST'};
$host = "Ãßêïò" if ( ($host eq "dell") or ($host eq "localhost") or ($
+host ~= /vivodi.gr/) );
my $dbh = ($ENV{'SERVER_NAME'} ne 'nikolas.50free.net')
? DBI->connect('DBI:mysql:nikos_db', 'root', 'c
+ensored')
: DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db', 'c
+ensored')
or {RaiseError=>1};
#*********************************************************************
+**********
my @files = <../data/text/*.txt>;
my @display_files = map( /([^\/]+)\.txt/, @files );
print start_form(-action=>'index.pl');
print p( {-align=>'center'}, font( {-size=>5, -color=>'Lime'}, '
+ÄéÜëåîå ÃÃá áðü ôá êåßìåÃá ãéá Ãá äéáâÜóåéò => ' ),
popup_menu( -name=>'select', -value
+s=>\@display_files ),
submit('ÅðéëïãÞ'));
print end_form(), br();
my %sql = (
get_counter => "SELECT counter FROM visitorlog",
get_host => "SELECT host FROM visitorlog WHERE host=?",
update_visitor => "UPDATE visitorlog SET counter+=1 WHERE host=?",
update_passage => "UPDATE visitorlog SET passage=? WHERE host=?",
insert_host => "INSERT INTO visitorlog (null, host, date, passa
+ge, counter) VALUES (?, ?, ?, ?, ?, ?)"
);
my $passage = param('select') || "Ãñ÷éêÞ Óåëßäá!";
my ($data, @data);
if (param('select') and param('select') != '..')
{
open(FILE, "<../data/text/$passage.txt") or die $!;
@data = <FILE>;
close(FILE);
$data = join('', @data);
$sth = $dbh->prepare( $sql{update_passage} );
$sth->execute($passage, $host);
}
else
{
my $sth = $dbh->prepare( $sql{get_host} );
$sth->execute($host);
if ($sth->rows)
{
$sth = $dbh->prepare( $sql{update_visitor} );
$sth->execute($host);
$sth = $dbh->prepare( $sql{get_host} );
$sth->execute($host);
$row = $sth->fetchrow_hashref;
$data = "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçà óåë
+ßäá Ã¥ÃäéáöÃñïõóá!\n" .
"Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
+ .$row->{date}. " !!\n" .
"ÓýÃïëéêÃò Þñèåò åäþ " .$row->{counter}. " öïñÃò!!!\n"
+ .
"Ôåëåõôáßá åßäåò ôï êåßìåÃï { " .$row->{passage}. " }\
+n" .
"Ãïéü êåßìåÃï èá ìåëåôÞóåòé áõôÞà ôçà öïñÜ !?";
}
else
{
if ($host ne "Ãßêïò")
{
$data = "ÃåéÜ óïõ " .$host. "!\n" .
"¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
"Åëðßæù Ãá âñåßò ôá êåßìåÃá Ã¥ÃäéáöÃñïÃôá ";
$sth = $dbh->prepare( $sql{insert_host} );
$sth->execute(null, $host, $date, $passage, $counter);
}
else
{
$data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ Ãá óïõ ð
+ÃœÃÃ¥ ðÜÃôá! ;-)";
}
}
}
$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( {class=>'user'},
Tr( td( 'Ãþò óå ëÃÃÃ¥ áäåëöå?'
+ ), td( textfield( 'name' ))),
Tr( td( 'ÃïéÜ åßÃáé ç ãÃþìç óïõ ãéá ôçà åõ÷ïýëá
»Êýñéå Éçóïý ×ñéóôÃ, ÅëÃçóïà Ìå« ?'
+ ), td( textarea( -name=>'pray', -rows=>4, -columns=>25 ))),
Tr( td( 'ÃåñéÃãñáøå ìáò ìéá ðñïóùðéêÞ óïõ
ðÃåõìáôéêÞ åìðåéñßá áðü êÜðïéïà ãÃñïÃôá ðñïò
þöåëïò ôùà õðïëïßðùà áäåëöþà ( áà öõóéêÜ Ã÷åéò '
+ ), td( textarea( -name=>'remark', -rows=>6, -columns=>25 ))),
Tr( td( 'Ãïéü åßÃáé ôï email óïõ?'
+ ), td( textfield( 'email' ))),
Tr( td( submit( 'ÅìöÜÃéóç üëùà ôùà áðüøåùÃ'
+ )), td( submit( 'ÃðïóôïëÞ' ))),
);
print end_form(), br(), br();
open(FILE, "<../data/text/tips") or die $!;
my @tips = <FILE>;
close(FILE);
@tips = grep { !/^\s*\z/s } @tips;
my $tip = $tips[ int(rand(@tips)) ];
print table( {class=>'tip'}, Tr( td( {class=>'tip'}, $tip ))), br();
$sth = $dbh->prepare( $sql{update_counter} ) if ($host ne "Ãßêïò");
$sth->execute;
$sth = $dbh->prepare( $sql{get_counter} );
$sth->execute;
#here i decided to add together all the times visitors visit
#my webpage so to get a total counter in order to avoid
#create a new mysql table called counters and store that
#value there. Or even to a flat file!
while ($row = $sth->fetchrow_hashref)
{
$counter += $row->{counter};
}
print table( {class=>'info'},
Tr( td( {class=>'host'}, $host )),
Tr( td( {class=>'date'}, $date )),
Tr( td( {class=>'counter'}, $counter ))
);
print br(), a( {href=>'games.pl'}, img{src=>'../data/
+images/games.gif'} );
print p( {-align=>'right'}, a( {href=>'show.pl?name=showlog'}, font(
+ {-size=>2, -color=>'Lime'}, b( 'Last Update: 30/4/2005' ))));
How i can i shorter it, perfect it some more by dividing into subs?
What must be/not be in a sub?