in reply to Re: Is this logic correct? Maybe can be rewritten better?
in thread Is this logic correct? Maybe can be rewritten better?

Thank you Dragonchild. I srted to use strict and warnigns as matter of fact that iam using it at this script i posted. Here si thw whole script:

#!/usr/bin/perl use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use CGI::Cookie; use DBD::mysql; use DBI; print header( -charset=>'iso-8859-7' ); print start_html( -style=>'../data/css/style.css', -title=>'Ψυχωφελή Π +νευματικά Κείμενα!', -background=>'../data/images/night.gif' ); my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', +'Sep', 'Oct', 'Nov', 'Dec'); my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = loc +altime; my $date = "$mday $months[$mon], $hour:$min"; my $ip = $ENV{'REMOTE_ADDR'}; my @numbers = split (/\./,$ip); my $address = pack ("C4", @numbers); my $host = gethostbyaddr ($address, 2) || $ip; $host = "Νίκος" if ( ($host eq "dell") or ($host eq "localhost") or ($ +host eq "dslcustomer-221-228.vivodi.gr") ); my ($sth, $row) = (); 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 @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 $script = param('select') or "Αρχική Σελίδα!"; $sth = $dbh->prepare( "SELECT host FROM guestlog" ); $sth->execute(); while( $row = $sth->fetchrow_hashref ) { if( $host eq $row->{host} ) { my $hostmatch = 1; } } if( param('select') and param('select') !~ /\.\./ ) { open(FILE, "<../data/text/$script.txt") or die $!; my @data = <FILE>; close(FILE); my $data = join('', @data); $dbh->do( "UPDATE guestlog SET script='$script' WHERE host='$host' +" ) or die $dbh->errstr; } elsif( $hostmatch == 1 ) { $dbh->do( "UPDATE guestlog SET hostcount = hostcount + 1 WHERE hos +t='$host'" ) or die $dbh->errstr; $sth = $dbh->prepare( "SELECT * FROM guestlog WHERE host='$host'" +); $sth->execute(); $row = $sth->fetchrow_hashref; $data = "Καλώς ήλθες " .$host. "! Χαίρομαι που βρίσκες την σελίδα +ενδιαφέρουσα!\n" . "Τελευταία φορά ήρθες εδώ ως " .$row->{host}. " στις " .$r +ow->{date}. " !!\n" . "Σύνολικές ήρθες εδώ " .$row->{hostcount}. " φορές!!!\n" . "Τελευταία είδες το κείμενο { " .$row->{script}. " }\n" . "Ποιό κείμενο θα μελετήσεςι αυτήν την φορά !?"; } elsif( $hostmatch != 1 ) { if ( $host ne "Νίκος" ) { $data = "Γειά σου " .$host. "!\n" . "Έρχεσαι για 1η φορά εδώ !!\n" . "Ελπίζω να βρείς τα κείμενα ενδιαφέροντα :-)"; $dbh->do( "INSERT INTO guestlog VALUES (null, '$host', '$date', + '$script', 1, 1)" ) or die $dbh->errstr; } 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 pagecount = 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")); pagecount++; if (pagecount >= 1800 && (c.charCodeAt(0) == 10 || c == ".")) { d.appendChild(document.createElement("br")); d.appendChild(document.createTextNode("Press any key...")); pagecount = 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=>'quote'}, Tr( td( {class=>'tip'}, $tip )) ); $dbh->do( "UPDATE guestlog SET pagecount = pagecount + 1" ) if ($host +ne "Νίκος"); $sth = $dbh->prepare( "SELECT pagecount FROM guestlog" ); $sth->execute(); $row = $sth->fetchrow_hashref; print br(); print table( {class=>'info'}, Tr( td( {class=>'host'}, $host )), Tr( td( {class=>'date'}, $date )), Tr( td( {class=>'hostcount'}, $row->{hostcount} )), Tr( td( {class=>'pagecount'}, $row->{pagecount} )) ); 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: 29/4/2005' ))));

I decided not to use html::Template although in another post i posted to you how i have done it. It gives me a headache. i will just use cgi.pm as i used too and css as well to help me lessen print formatting inside my perl cgi scripts.

Iam getting this errors too although i ahve set all my vars with my:
Global symbol "$hostmatch" requires explicit package name at D:\www\cg +i-bin\index.pl line 61. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 68. Global symbol "$hostmatch" requires explicit package name at D:\www\cg +i-bin\index.pl line 74. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 78. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 86. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 90. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 91. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 92. Global symbol "$data" requires explicit package name at D:\www\cgi-bin +\index.pl line 95. Execution of D:\www\cgi-bin\index.pl aborted due to compilation errors +.
Please help me rebuild this index.pl in a better more straightforwardway and please point out to me my errors. Thank you.

Replies are listed 'Best First'.
Re^3: Is this logic correct? Maybe can be rewritten better?
by ikegami (Patriarch) on Apr 29, 2005 at 15:32 UTC

    When you use my $hostmatch inside the if, you're creating a variable that only exists within the if. Any value you assign to it will disappear along with the variable once outside the if. That means elsif( $hostmatch == 1 ) is using a variable that no longer exists. The solution is:

    my $hostmatch; while( $row = $sth->fetchrow_hashref ) { if( $host eq $row->{host} ) { $hostmatch = 1; } }

    Same goes for $data. Change

    if( param('select') and param('select') !~ /\.\./ ) { my $data = ... ... }

    to

    my $data; if( param('select') and param('select') !~ /\.\./ ) { $data = ... ... }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: Is this logic correct? Maybe can be rewritten better?
by Fletch (Bishop) on Apr 29, 2005 at 16:23 UTC

    Don't roll your own localtime-to-string routine. Use POSIX and strftime, which aside from requiring no extraneous months array and being less code will also respect the date formatting conventions of the current locate.

      So how can i write the following lines iwth the use of strptime:
      my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', +'Sep', 'Oct', 'Nov', 'Dec'); my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = loc +altime; my $date = "$mday $months[$mon], $hour:$min";
      I didnt understand cpans explanation on this.

        See your OS' documentation, as it'll have all the escape codes (man 3 strftime or some variation thereon should find it). In this particular case you probably want strftime( "%d %b, %H:%M", localtime ).