Finally a better script!

N

Nikos

You understand very quickly what i wanted to do and you helped me write
the correct way.
After all of the guys suggestions here is how the script transformed:


=========================================================
my $script = param('select') || "Welcome Page!";
my ($data, @data);


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

$data = join('', @data);

$dbh->do( "UPDATE guestlog SET script='$script' WHERE host='$host'"
) or die $dbh->errstr;
}
else
{
$sth = $dbh->prepare( "SELECT host FROM guestlog WHERE host=?" );
$sth->execute($host);

if ($sth->rows)
{
$sth = $dbh->prepare( "UPDATE guestlog SET
hostcount=hostcount+1 WHERE host=?" );
$sth->execute($host);

$sth = $dbh->prepare( "SELECT * FROM guestlog WHERE host=?" );
$sth->execute($host);
$row = $sth->fetchrow_hashref;

$data = "Êáëþò Þëèåò " .$host. "! ×áßñïìáé ðïõ âñßóêåò ôçí
óåëßäá åíäéáöÃñïõóá!\n" .
"Ôåëåõôáßá öïñÜ Þñèåò åäþ ùò " .$row->{host}. " óôéò "
..$row->{date}. " !!\n" .
"ÓýíïëéêÃò Þñèåò åäþ " .$row->{hostcount}. " öïñÃò!!!\n" .
"Ôåëåõôáßá åßäåò ôï êåßìåíï { " .$row->{script}. " }\n" .
"Ãïéü êåßìåíï èá ìåëåôÞóåòé áõôÞí ôçí öïñÜ !?";
}
else
{
if ($host ne "Ãßêïò")
{
$data = "ÃåéÜ óïõ " .$host. "!\n" .
"¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
"Åëðßæù íá âñåßò ôá êåßìåíá åíäéáöÃñïíôá :)";

$sth = $dbh->prepare("SELECT * FROM guestlog WHERE host=?");
$sth->execute($host);

$sth = $dbh->prepare( "INSERT INTO guestlog (host, date,
script, hostcount, pagecount) VALUES (?, ?, ?, ?, ?)" );
$sth->execute($host, $date, $script, $hostcount, $pagecount);
}
else
{
$data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ íá óïõ
ðÜíå ðÜíôá! ;-)";
}
}
}
=========================================================


I wish i can test ti but iam currently having a mysql problem but thing
it will work :)
If we still can make it shorter please let me know!
 
H

Henry Law

You understand very quickly what i wanted to do and you helped me write
the correct way.
After all of the guys suggestions here is how the script transformed:

Well, you missed one of mine.
=========================================================
my $script = param('select') || "Welcome Page!";
my ($data, @data);


if (param('select') and param('select') ne '..')

And I'm sure in my own mind that this statement isn't going to do what
you want. For a start you do realise that "ne" is higher priority
than "and" so this is going to evaluate (param('select') ne '..')
first (yielding a true/false value), and then take the result and
logically "and" it with the _same_ param('select'). I may be
completely wrong and it is exactly what you want (I'm not a skilled
Perl-ist myself) but I can't imagine what value of param('select')
will yield sense in that statement.
{
open(FILE, "<../data/text/$script.txt") or die $!;

Where did $script come from? Not this program, evidently.

$data = join('', @data);

$data never seems to get used. All the occurrences of it from here on
are on the left hand side of an assignment. I can't the point of
slurping the contents of $script.txt in and jamming it all together.

I wish i can test ti but iam currently having a mysql problem but thing
it will work :)

What? You're posting all this stuff and you haven't tested it? I'm
wasting my time then. Sheesh.
If we still can make it shorter please let me know!

Shorter != better.

Now get this: Unless you post code that RUNS and doesn't contain
unnecessary material unrelated to your question, and show YOUR output
from it, and explain accurately why it's not doing what you thought it
should do, then I'm not going to write another word to try to help
you. Most other people have bailed out already.
 
D

David K. Wall

Henry Law said:
[snip]
=========================================================
my $script = param('select') || "Welcome Page!";
^^^^^^^

[snip]
open(FILE, "<../data/text/$script.txt") or die $!;

Where did $script come from? Not this program, evidently.

It was there, you just missed it. I'd guess that maybe you expected
it to not be there. Understandable. :)

Now get this: Unless you post code that RUNS and doesn't contain
unnecessary material unrelated to your question, and show YOUR
output from it, and explain accurately why it's not doing what you
thought it should do, then I'm not going to write another word to
try to help you. Most other people have bailed out already.

I've been lurking, not posting, but I also have little interest in
trying to help someone who admits to being uninterested in Reading
The Fine Manual and other helpful resources.
 
A

A. Sinan Unur

Nikos said:
You understand very quickly

Well, yes, I tend to, but who is this comment directed to?
my ($data, @data);

Always declare variables in the smallest applicable scope. I am not sure
what you want to do with these variables.

See http://perl.plover.com/FAQs/Namespaces.html
open(FILE, "<../data/text/$script.txt") or die $!;
@data = <FILE>;
close(FILE);

$data = join('', @data);

1. Where did $script come from?

2. If you just want to slurp the file, then do so:

my $data;
{
my $fn = "/data/text/$script.txt";
open my $file, '<', $fn or die "Cannot open $fn: $!";
local $/;
$data = <$file>;
close $file or die "Cannot close $fn: $!";
}

$data will now contain the entire contents of /data/text/$script.txt.

Or, you can use the excellent File::Slurp module.

Sinan
 
N

Nikos

Nikos wrote: old_script [snip]

Here is the script modified again implementing new ideas:

<code>
my %sql = (
get_counter => "SELECT counter FROM visitorlog",
get_host => "SELECT host FROM visitorlog WHERE host=?",
update_counter => "UPDATE visitorlog SET counter+=1",
update_visitorcounter => "UPDATE visitorlog SET visitorcounter+=1
WHERE host=?",
update_passage => "UPDATE visitorlog SET passage=? WHERE
host=?",
insert_host => "INSERT INTO visitorlog (host, date,
passage, visitorcounter, counter) VALUES (?, ?, ?, ?, ?)",
);

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


if (param('select') and param('select') ne '..')
{
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_host} );
$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->{guestcounter}. "
öïñÃò!!!\n" .
"Ôåëåõôáßá åßäåò ôï êåßìåíï { " .$row->{passage}. " }\n" .
"Ãïéü êåßìåíï èá ìåëåôÞóåòé áõôÞí ôçí öïñÜ !?";
}
else
{
if ($host ne "Ãßêïò")
{
$data = "ÃåéÜ óïõ " .$host. "!\n" .
"¸ñ÷åóáé ãéá 1ç öïñÜ åäþ !!\n" .
"Åëðßæù íá âñåßò ôá êåßìåíá åíäéáöÃñïíôá :)";

$sth = $dbh->prepare( $sql{insert_host} );
$sth->execute($host, $date, $passage, $guestcounter, $counter);
}
else
{
$data = "ÃåéÜ óïõ Ãéêüëá, ôé ÷áìðÜñéá?! ¼ëá äåîéÜ íá óïõ
ðÜíå ðÜíôá! ;-)";
}
}
}
</code>

I had trouble selecting good variable names but i think i have made good
selections. Of course i changes the mysql columns names as well.

I like your %hash idea a lot in fact i put all the sql statements there.

I was also wondering if we can shorter it and perfect it even more maybe
with subdivisions. :)
 
N

Nikos

A. Sinan Unur said:
$data will now contain the entire contents of /data/text/$script.txt.

Yes Sinan, that is also what i want because then a javascript follows to
get the $data varibale and produce ncie 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]

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 soem better way to do this?
 
F

Fabian Pilkowski

* Henry Law said:
And I'm sure in my own mind that this statement isn't going to do what
you want. For a start you do realise that "ne" is higher priority
than "and" so this is going to evaluate (param('select') ne '..')
first (yielding a true/false value), and then take the result and
logically "and" it with the _same_ param('select'). I may be
completely wrong and it is exactly what you want (I'm not a skilled
Perl-ist myself) but I can't imagine what value of param('select')
will yield sense in that statement.

Try to read this condition as: if param is true and not equal "..". Due
to the very low precedence of »and« this construct makes sense, usually.

Consider the undefined value as param. Just comparing *undef* with the
string '..' throws a warning ("Use of uninitialized value in string ne
at ..."). Hence one is checking if param is not the undefined value (ok,
here the check tests for trueness instead of definedness).

regards,
fabian
 
E

Eric Schwartz

Nikos said:
Make the above if (param('select') and param('select') != '..') and
its fine :)

Um, no. Read 'perldoc perlop'-- and no, I don't care if you claim to
have a hard time with it. I won't read it for you. At the very
least, you should be able to search it for != and ne and find out why
they're different, and why you're wrong.

-=Eric
 
F

Fabian Pilkowski

* Nikos said:
<script type='text/javascript'>
var textToShow = "$data";

{snip]

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 soem better way to do this?

Yes, it is. I've mentioned it in another of your threads. Admittedly, it
is hard to find, there are so many (and long) threads you started the
last days. Perhaps you want to stop that -- it would be easier to find
information one has given to you, then. Read my posting with message-id

<
again (the last sentences concern about this). Meanwhile, it's too often
that you don't take the information one gives you. You're asking and
asking but don't take care of the answers. I've answered this question
already but you're going to ask it once again. Why? I don't understand.

OTOH, I suggest you do your page without JavaScript -- it's hard enough
to learn Perl and HTML first. Once you are familiar with them you could
add some JavaScript lessons to your timetable.

And, of course, this group is not for talking about JavaScript nor any
detail you need to set up your website with that language inside.

regards,
fabian
 
N

Nikos

Fabian said:
* Nikos said:
<script type='text/javascript'>
var textToShow = "$data";

{snip]

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 soem better way to do this?


Yes, it is. I've mentioned it in another of your threads. Admittedly, it
is hard to find, there are so many (and long) threads you started the
last days. Perhaps you want to stop that -- it would be easier to find
information one has given to you, then. Read my posting with message-id

<
again (the last sentences concern about this). Meanwhile, it's too often
that you don't take the information one gives you. You're asking and
asking but don't take care of the answers. I've answered this question
already but you're going to ask it once again. Why? I don't understand.

True but i have quit that.
If you see my posts from yesterday and on i *do* listen.
OTOH, I suggest you do your page without JavaScript -- it's hard enough
to learn Perl and HTML first. Once you are familiar with them you could
add some JavaScript lessons to your timetable.

No need to learn Javascript. I wont be using this except this
char_by_char nice producing effect i want.
I must pass the value though to js if i put in in a diff file, how?
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top