Re: Perfecting index.pl some more!



Tassilo v. Parseval wrote:
[snip suggestions to make the code better]

Thanks a lot Tassilo! :-)
The code is much better now with your suggestions:
I dont know what to do with the long lines you said...
You said i dont have to use subs sicne this is nto 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);
my $date = strftime( "%d %b, %H:%M", localtime );
my $host = $ENV{'REMOTE_HOST'} or $ENV{'REMOTE_ADDR'};
$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',     '****')
        : 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'}, font( {class=>'tip'}, 'ÄéÜëåîå Ýíá áðü ôá êåßìåíá ãéá íá äéáâÜóåéò => ' ),
popup_menu( -name=>'select', -values=>\@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 (host, date, passage, counter) VALUES (?, ?, ?, ?)"
);


my ($data, $counter);
my $passage = param('select') || "Áñ÷éêÞ Óåëßäá!";

if (param('select') and param('select') !~ '..')
{
    open(FILE, "<../data/text/$passage.txt") or die $!;
         local $/;
         $data = <FILE>;
    close(FILE);

    $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($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() 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( 'ÅìöÜíéóç üëùí ôùí áðüøåùí' )), td( submit( 'ÁðïóôïëÞ' ))),
);
print end_form();
print br() x 2;


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

@tips = grep { !/^\s*$/ } @tips;
my $tip = $tips[ rand @tips ];

print table( {class=>'info'},  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;

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' ))));
.




Relevant Pages

  • first post in webdesign
    ... Dont know for now. ... get started and which host to use. ... I downloaded Yahoo's website builder program and can practice on that one. ...
    (microsoft.public.publisher.webdesign)
  • Re: I am sure I am dense - but I dont know why!!
    ... Re: I am sure I am dense - but I dont know why!!: ... >$ host ftp.freebsd.org ... dns1# host ftp.freebsd.org ...
    (freebsd-questions)
  • Re: help!!!!!!!! MySQL, PHP, and Apache configuration.
    ... > questions asked also if i dont put a pass or username it works... ... It is *very* insecure to have a MySQL server running that allows access ... Dont forget a HOST associated with each username. ...
    (comp.lang.php)
  • Re: Change computers name
    ... i dont see debian as a host ... all i see under the dns tab, ... > host name. ... Do you Yahoo!? ...
    (RedHat)
  • Re: Gross Out Thread
    ... >> lithe and somewhat emerald colored Mimic spake thusly to the host ... >> gathered 'round: ... >>>Add your gross out here, obviously we've all seen tubgirl and goatse. ... If you know what this is and dont like it, ...
    (alt.2600)