For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2005 > hello all









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author hello all
Don Doucette

2005-02-17, 3:55 am

hello everyone.
my name is don doucette and I am 38 years old and have been involved with
computers since the Timex Sinclair.

I have recently set up a server and am hosting a web site and forum for my
community association.

I am running the YaBB forum (http://www.yabbforum.com/) and I would like to
do the following...

I would like to use perl to look in a directory of numerical named .txt
files (as in 0123456789.txt), find the file name with the largest number
(as in 1234567890.txt is greater numerically than 0123456789.txt) then open
the file and extract data from that file so it can be posted into a web page?

For instance...
I have a directory named Messages, in this directory there are the
following files...

1108577587.txt
1108519222.txt
1108490078.txt
1108489912.txt

Obviously 1108577587.txt is greater numerically than the rest, this also
happens to signify that this is the newest message. In this file is the
following information...

Title of Post|Author|email@address.com|02/16/05 at
12:13:07|Group|xx|0|192.168.1.1|Message||

As you can see this file is delimited by | and ends with ||
I would like to parse out the Message field first then the Author field and
assign their value to a variable then insert the variable into html on a page.

Something like...
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p>Here is the newest post to the forum<BR><BR>
$Message <BR><br>
Posted by $author </p>
</body>
</html>

The idea is when the main web page loads it always shows the newest post to
the forum and who posted it.

My question REALLY is do you think this can be easily done or is this a
huge programming effort for someone just trying to figure out perl... I
have been thumbing through my Perl book (The Complete Reference Perl Second
Edition) but it hasn't really been helpful so far.

Thanks for your advice.


Mike Garner

2005-02-17, 3:56 pm

Don-
Not sure if anyone has replied to you but here's my $.02:

That's nearly the perfect first perl programming task. You need to look into
using the system command or `` (backticks) to get system commands into a
variable or readdir to read the contents of a directory. Look at arrays &
sort to sort your array of filenames. Look at Push, Pop, & split for ways to
take apart & build arrays as well as break apart your | separated string.
Then comes the HTML output. You should look at the CGI module. You can craft
object oriented code for just simple print statements for the output once
the right header is in place. The docs for CGI are quite extensive.

Break your process apart and go step by step. As you run into specific
hang-ups, post back here.

Good luck.

~Mike

PS. And maybe come up with a more informational subject line.


-----Original Message-----
From: Don Doucette [mailto:doucetted@accesscomm.ca]
Sent: Wednesday, February 16, 2005 7:43 PM
To: beginners-cgi@perl.org
Subject: hello all

hello everyone.
my name is don doucette and I am 38 years old and have been involved with
computers since the Timex Sinclair.

I have recently set up a server and am hosting a web site and forum for my
community association.

I am running the YaBB forum (http://www.yabbforum.com/) and I would like to
do the following...

I would like to use perl to look in a directory of numerical named .txt
files (as in 0123456789.txt), find the file name with the largest number
(as in 1234567890.txt is greater numerically than 0123456789.txt) then open
the file and extract data from that file so it can be posted into a web
page?

For instance...
I have a directory named Messages, in this directory there are the
following files...

1108577587.txt
1108519222.txt
1108490078.txt
1108489912.txt

Obviously 1108577587.txt is greater numerically than the rest, this also
happens to signify that this is the newest message. In this file is the
following information...

Title of Post|Author|email@address.com|02/16/05 at
12:13:07|Group|xx|0|192.168.1.1|Message||

As you can see this file is delimited by | and ends with ||
I would like to parse out the Message field first then the Author field and
assign their value to a variable then insert the variable into html on a
page.

Something like...
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p>Here is the newest post to the forum<BR><BR>
$Message <BR><br>
Posted by $author </p>
</body>
</html>

The idea is when the main web page loads it always shows the newest post to
the forum and who posted it.

My question REALLY is do you think this can be easily done or is this a
huge programming effort for someone just trying to figure out perl... I
have been thumbing through my Perl book (The Complete Reference Perl Second
Edition) but it hasn't really been helpful so far.

Thanks for your advice.



Zentara

2005-02-17, 8:55 pm

On Wed, 16 Feb 2005 20:42:48 -0600, doucetted@accesscomm.ca (Don
Doucette) wrote:

>I would like to use perl to look in a directory of numerical named .txt
>files (as in 0123456789.txt), find the file name with the largest number
>(as in 1234567890.txt is greater numerically than 0123456789.txt) then open
>the file and extract data from that file so it can be posted into a web page?
>
>Title of Post|Author|email@address.com|02/16/05 at
>12:13:07|Group|xx|0|192.168.1.1|Message||
>
>As you can see this file is delimited by | and ends with ||
>I would like to parse out the Message field first then the Author field and
>assign their value to a variable then insert the variable into html on a page.
>
>Something like...
><html>
><head>
><title>Untitled Document</title>
></head>
><body>
><p>Here is the newest post to the forum<BR><BR>
>$Message <BR><br>
>Posted by $author </p>
></body>
></html>
>
>The idea is when the main web page loads it always shows the newest post to
>the forum and who posted it.
>
>My question REALLY is do you think this can be easily done or is this a
>huge programming effort for someone just trying to figure out perl... I
>have been thumbing through my Perl book (The Complete Reference Perl Second
>Edition) but it hasn't really been helpful so far.


Yeah, this is super-easy. Here is a sample script, I dont' know if you
need an html header on it or not, or a link, all that is extra. Here is
the simplest way, and I've printed out alot of debugging info, so you
will see what is happening after each step. Just modify the script
to fit into your bb program's output.

#!/usr/bin/perl
use warnings;
use strict;

my @files = glob("Messages/*.txt");
(@files) = map{ $_ =~ /Messages\/(.*).txt$/ } @files;
#print "@files\n";

#this may be faster than sort on a huge list
my $max = $files[0];
for (@files) {
$max = $_ if $_ > $max;
}
#print "max-> $max\n";

open (FH,"<Messages/$max.txt") or die "Cant open $!\n";
my $buf;
read( FH, $buf, -s FH );
close FH;

my (@fields) = split(/\|/, $buf);
#print join "\n", @fields,"\n\n\n";

print "Content-Type: text/html\n\n";

print<<EOHTML
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p>Here is the newest post to the forum<BR><BR>
$fields[8] <BR><br>
Posted by $fields[1] </p>
</body>
</html>
EOHTML
__END__




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
John W. Krahn

2005-02-21, 8:55 am

Don Doucette wrote:
> hello everyone.


Hello,

> my name is don doucette and I am 38 years old and have been involved
> with computers since the Timex Sinclair.
>
> I have recently set up a server and am hosting a web site and forum for
> my community association.
>
> I am running the YaBB forum (http://www.yabbforum.com/) and I would like
> to do the following...
>
> I would like to use perl to look in a directory of numerical named .txt
> files (as in 0123456789.txt), find the file name with the largest number
> (as in 1234567890.txt is greater numerically than 0123456789.txt) then
> open the file and extract data from that file so it can be posted into a
> web page?
>
> For instance...
> I have a directory named Messages, in this directory there are the
> following files...
>
> 1108577587.txt
> 1108519222.txt
> 1108490078.txt
> 1108489912.txt
>
> Obviously 1108577587.txt is greater numerically than the rest, this also
> happens to signify that this is the newest message. In this file is the
> following information...
>
> Title of Post|Author|email@address.com|02/16/05 at
> 12:13:07|Group|xx|0|192.168.1.1|Message||
>
> As you can see this file is delimited by | and ends with ||
> I would like to parse out the Message field first then the Author field
> and assign their value to a variable then insert the variable into html
> on a page.
>
> Something like...
> <html>
> <head>
> <title>Untitled Document</title>
> </head>
> <body>
> <p>Here is the newest post to the forum<BR><BR>
> $Message <BR><br>
> Posted by $author </p>
> </body>
> </html>
>
> The idea is when the main web page loads it always shows the newest post
> to the forum and who posted it.
>
> My question REALLY is do you think this can be easily done or is this a
> huge programming effort for someone just trying to figure out perl... I
> have been thumbing through my Perl book (The Complete Reference Perl
> Second Edition) but it hasn't really been helpful so far.


It should be fairly easy. :-)

#!/usr/bin/perl -T
use warnings;
use strict;
use CGI ':standard';
#use CGI::Carp 'fatalsToBrowser'; # only required for debugging

my $directory = '/some/dir';
opendir DIR, $directory or die "Cannot open $directory: $!";

my $number = 0;
my $filename;
while ( my $file = readdir DIR ) {
$file =~ /^(\d+)\.txt$/ and $1 > $number and ( $number, $filename ) = (
$1, $file )
}
closedir DIR;
die "Cannot find a file!\n" unless defined $filename;

open FILE, '<', "$directory/$filename" or die "Cannot open
$directory/$filename: $!";
my ( $author, $message ) = ( split /\|/, <FILE> )[ 1, 8 ];
close FILE;
die "Cannot find a message!\n" unless defined $message;

print header,
start_html( 'Untitled Document' ),
p,
"Here is the newest post to the forum<BR><BR>
$message <BR><br>
Posted by $author",
end_html;

__END__



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com