CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
guestbook writes new data to the end of the file

 



elvar
Deleted

Jan 26, 2000, 3:54 PM

Post #1 of 11 (1051 views)
guestbook writes new data to the end of the file Can't Post

I have one guestbook, simple itself, but writes new data to the end of the file. So the last messages is in the end and needs to scroll to see them. I want that it writes new entry to he beginning of the file. Is there some simple command that does this or...
Well, part of this pl file is this (what i need to change):
{
open(BOOK, ">> $book_file");
print BOOK "<blockquote><b>Nimi</b>: $input{'name'}\n";
print BOOK "<br><b>E-Mail</b>: <a href=\"mailto:$input{'e-mail'}\">$input{'e-mail'}</a>\n";
print BOOK "<br><b>Kodulehekülg</b>: <a href=\"$input{'URL'}\">$input{'URL'}</a>\n";
print BOOK "<br><b>Kust tuli</b>: $input{'locale'}\n";
print BOOK "<br><b>Teade</b>: $input{'comments'}\n";
print BOOK "<hr></blockquote>\n";
close(BOOK);
print "Location: $book_url\n\n";
exit;
}
}

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$input{$name} = $value;
$input{'comments'} =~ s/\n/<br>/g;
}


elvar
Deleted

Jan 26, 2000, 4:25 PM

Post #2 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Sorry for posting, got help already - didn't look earlier postings. But darian's answer to the Cold-ice.com helped me (thanks darian). So new code is next:
open(BOOK, "$book_file");
@text = <BOOK>;
close(BOOK);
open(BOOK, ">$book_file");
print BOOK "<blockquote><b>Nimi</b>: $input{'name'}\n";
print BOOK "<br><b>E-Mail</b>: <a href=\"mailto:$input{'e-mail'}\">$input{'e-mail'}</a>\n";
print BOOK "<br><b>Kodulehekülg</b>: <a href=\"$input{'URL'}\">$input{'URL'}</a>\n";
print BOOK "<br><b>Kust tuli</b>: $input{'locale'}\n";
print BOOK "<br><b>Teade</b>: $input{'comments'}\n";
print BOOK "<hr></blockquote>\n";
print BOOK "@text";
close(BOOK);


japhy
Enthusiast

Jan 26, 2000, 6:18 PM

Post #3 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Um, print "@array"; is usually not what one wants... leave out the quotes, methinks.

Also, not to put Darien down, or anything, but Perl programs should be smart enough to do what one wants without having to move mountains.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open FILE, ">>file" or die "can't append to file: $!";
print FILE "new data\n";
close FILE;

# then, to get the content, MOST RECENT FIRST

open FILE, "file" or die "can't read file: $!";
unshift @contents, $_ while <FILE>;
close FILE;
</pre><HR></BLOCKQUOTE>

That stops you from doing time-consuming things like

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@contents = <FILE>;
</pre><HR></BLOCKQUOTE>

and opening a file more than you have to.


69_vette
Deleted

Jan 27, 2000, 3:49 AM

Post #4 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

That would work IF elvar was using cgi to read the book with...

If not then maybe you'd want to open the book in read/write then move to the top of the book then print the new data...

That would save from opening the book twice...

Then again what do I know, I can't get that stupid here doc to work...


brian.hayes
User

Jan 27, 2000, 7:37 AM

Post #5 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

I don't belive Japhy is either.
If I'm reading the code corectly it is just plain ol perl. "unshift" I think just reads from bottom to top. So what ever you use to read the data output you will still get the same results. But I could be wrong.

If file has:
1
2
3
4

and you use "unshift" in example.

you get:
4
3
2
1

Mabee Japhy could explain the code a little
more.

Brian Hayes


japhy
Enthusiast

Jan 27, 2000, 7:47 AM

Post #6 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Sure, I'll explain. You have a file that is to hold messages, like an error log, perhaps. The error log is written to like this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open LOG, ">>error_log";
$date = localtime;
print LOG "[$date] some-message\n";
close LOG;
</pre><HR></BLOCKQUOTE>

Now, what you want to do is display these error messages, MOST RECENT FIRST. But you don't want to change the way the error messages are logged, because appending is pretty efficient.

So, when you read from the file, instead of push()ing the lines into an array, and then having to reverse the array, or work with the array backwards, you can simple unshift() the lines to the array:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open LOG, "error_log";
unshift @msgs, $_ while <LOG>;
close LOG;

for (@msgs) {
# do something with $_
}
</pre><HR></BLOCKQUOTE>

That's what my code does.


69_vette
Deleted

Jan 27, 2000, 8:39 AM

Post #7 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

I was just saying that if it was me I wouldn't use a script just to view a guestbook with...

Instead, I'd write the book the way it's meant to be viewed, whether to an html file or just a text file to be inserted into an html file.

Which is what elvar is doing...


brian.hayes
User

Jan 27, 2000, 8:50 AM

Post #8 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Thanks Japhy, Now I understand it better.

Would it not be better to use SSI "Server side includes" for a guest book? Pointing to the script example that Japhy gives To include the txt file in the HTML file. This would give you a dynamic way to displaying the data from the txt file. Instead of directly writing to the HTML file. This would allow you to maintain the web site page a little easier. Just in case you are editing the page while the people are appending to it as well. You will find your self in some trouble unless you stop the Guest book while you work on the Web site.

Well thats my opinion anyway.

Thanks,

Brian Hayes


69_vette
Deleted

Jan 27, 2000, 12:04 PM

Post #9 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Well... I'd use ssi to point directly to the .txt file itself.

Would that not use less resources/time than having to "compile" the guestbook first by using a second script? (not that it would be that noticable)

Just put the new data at the top of the txt file rather than the bottom...

The only reason I would think of not to just put it on the top would be in Japhy's example where you want to keep the data in cronological order top to bottom, but would like to view the most recent ones first...


[This message has been edited by 69_vette (edited 01-27-2000).]


brian.hayes
User

Jan 27, 2000, 12:27 PM

Post #10 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Sure that would be just fine. You may want to consider using a small clean perl script instead of directly pointing to the txt file, well that is if would want to do some filtering on the file and like you said display from top to bottom. This would allow for some regex code to keep out things you do not want to diplay.

Also you will want to filter input becuase these types of service are known for Hacks.

Example:

if a user post to the Guest Book like this.

Brian Hayes

Then Brian Hayes would be added to the file.

BUT if a user post to the Guest Book with HTML code like this.

"h1"Brian Hayes"/h1" # replace "" with opening brakets and closing brackets.

You will get some variations and possible unwanted displays out of your web page.

The above would display Heading text of my name on your web site. This would be not wanted.

Please take a look at my posting:

How to remove HTML CODE sections. In the Beginner section of this forum. This will give you some idea of where I am tring to take this.

Thanks,

Brian Hayes


brian.hayes
User

Jan 27, 2000, 12:28 PM

Post #11 of 11 (1051 views)
Re: guestbook writes new data to the end of the file [In reply to] Can't Post

Sorry forgot you covered the part about parsing the input.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives