Here is how my script has been transformed thanks to your precious precious suggestions and mine's minor alternation:
I dont know what to do with the long lines you said...
Do i have to to use subs since this is not a big script
and actually iam not repeating any functions more than once?
Well, ok, i dont know if it can gets any shorten than this or if i can put tha javascript out in a seperate file but still pass the $data variable.
Also maybe thre is a better wau to create the $data variable instead of constant concatenations as i have it.
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBD::mysql;
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, $data);
my $date = strftime( "%d %b, %H:%M", localtime );
my $host = $ENV{'REMOTE_HOST'} || $ENV{'REMOTE_ADDR'};
$host = "dell" if ( ($host eq "dell") or ($host eq "localhost") or ($h
+ost =~ /vivodi.gr/) );
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 {RaiseError=>1};
#*********************************************************************
+**********
my @files = <../data/text/*.txt>;
my @display_files = map /([^\/]+)\.txt/, @files;
print start_form(-action=>'index.pl');
print p( {-align=>'center'}, span( {class=>'tip'}, "Διάλεξε ένα
+από τα κείμενα για να διαβάσεις => " ),
popup_menu( -name=>'select', -value
+s=>\@display_files ),
submit('Επιλογή'));
print end_form;
my %sql = (
get_row => 'SELECT * FROM visitorlog',
get_counter => 'SELECT counter FROM visitorlog',
get_host => 'SELECT host FROM visitorlog WHERE host=?',
update => 'UPDATE visitorlog SET passage=?, counter=counter+1
+ WHERE host=?',
insert_host => 'INSERT INTO visitorlog (host, date, passage, count
+er) VALUES (?, ?, ?, ?)'
);
my $passage = param('select') || "Αρχική Σελίδα!";
if ( param('select') )
{
open(FILE, "<../data/text/$passage.txt") or die $!;
local $/;
$data = <FILE>;
close(FILE);
$sth = $dbh->prepare( $sql{update} );
$sth->execute( $passage, $host );
}
else
{
$sth = $dbh->prepare( $sql{get_host} );
$sth->execute( $host );
if ($sth->rows)
{
$sth = $dbh->prepare( $sql{get_row} );
$sth->execute;
$row = $sth->fetchrow_hashref;
$data = "Καλώς ήλθες $host! Χαίρομαι που βρίσκες την σελίδα εν
+διαφέρουσα!
Τελευταία φορά ήρθες εδώ ως $row->{host} στις $row->{
+date} !!
Συνολικά ήρθες εδώ $row->{counter} φορές !!!
Τελευταία είδες το κείμενο [ $row->{passage} ]
Ποιό κείμενο θα μελετήσεις αυτήν την φορά !?";
$sth = $dbh->prepare( $sql{update} );
$sth->execute( $passage, $host );
}
elsif ($host ne "dell")
{
$data = "Γειά σου $host!
Έρχεσαι για 1η φορά εδώ !!
Ελπίζω να βρείς τα κείμενα ενδιαφέροντα ;-)";
$sth = $dbh->prepare( $sql{insert_host} );
$sth->execute( $host, $date, $passage, 1 );
}
else
{
$data = "Γειά σου Νικόλα, τι χαμπάρια?! Όλα δεξιά να σου πάνε
+πάντα! ;-)";
}
}
for ($data) {
s/\n/\\n/g;
s/"/\\"/g;
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 Magenta 5px;
width: 850px;
height: 500px;
color: LightSkyBlue;
font-family: Times;
font-size: 18px;">
</div
ENDOFHTML
#*********************************************************************
+**********
print br() x 3;
print start_form(-action=>'show.pl');
print table( {class=>'user_form'},
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( 'Εμφάνιση Guestbook!'
+ )), td( submit( 'Αποστολή' ))),
);
print end_form();
print br() x 2;
my @tips;
open(FILE, "<../data/text/tips") or die $!;
@tips = <FILE>;
close(FILE);
@tips = grep { !/^\s*$/ } @tips;
my $tip = $tips[ rand @tips ];
print table( {class=>'tip'},
Tr( td( {class=>'tip'}, $tip ))
), br;
$sth = $dbh->prepare( $sql{update_counter} ) if ($host ne "dell");
$sth->execute;
$sth = $dbh->prepare( $sql{get_counter} );
$sth->execute;
my $pagecounter;
while ($row = $sth->fetchrow_hashref)
{
$pagecounter += $row->{counter};
}
print table( {class=>'info'},
Tr( td( {class=>'host'}, $host )),
Tr( td( {class=>'date'}, $date )),
Tr( td( {class=>'counter'}, $pagecounter ))
);
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?
=========================================================
$data will now contain the entire contents of /data/text/$passage.txt. That is also what i want because then a javascript follows to get the $data varibale and produce nice effects.
$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";
{snip]
also $host never gets a hostanme just ip address.
I was wondering if there is a way to take out the js snippet from my index.pl but still pass the value of $data to that .js to produce the nice effects.....
Also i have to take out the backslash and " symbols or otherwise the javascript part wont work correctly since $data is enclosed to double quotes.
Is there some better way to do this as well?
Today i already updated this script 8 times!
When i amke a better version of it i'am updating the original to avoid reposting all over the beginning! ;-)
Janitored by Arunbear - added readmore tags, as per Monastery guidelines