Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: simple message board gone very wrong?

by cei (Monk)
on Dec 12, 2000 at 03:05 UTC ( [id://46174]=note: print w/replies, xml ) Need Help??


in reply to simple message board gone very wrong?

I looked at MSA's board and there were a handful of things I didn't like about it. I'm no perl saint, but here's what I came up with as a quick & dirty:
#!/usr/bin/perl -w use CGI; use strict; my ($CGID, $i, $date); my $P = CGI::new(); $P->import_names('NM'); if ($0=~m#^(.*)\\#){ $CGID = "$1"; } elsif ($0=~m#^(.*)/# ){ $CGID = "$1"; } else {`pwd` =~ /(.*)/; $CGID = "$1"; } my $gb = "$CGID/../www/html/fan/fan_main.html"; open (FH, "$gb") || die "Can't Open $gb: $!\n"; my @LINES=<FH>; close(FH); my $SIZE=@LINES; my $minutes = (localtime)[1]; ($minutes = "0".$minutes) if ($minutes < 10); $date = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')[(localtime)[6]] . ', ' . ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')[(localtime)[4]] . " " . (localtime)[3] . ", " . ((localtime)[5] + 1900) . " at " . (localtime)[2] . ":" . (localtime)[1]; open (FH,">$gb") || die "Can't Open $gb: $!\n"; for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { print FH "<!--begin-->\n"; $NM::message =~ s/(\S{20})/$1 /g; $NM::message =~ s/</&lt;/g; $NM::message =~ s/>/&gt;/g; $NM::message =~ s/\cM\n/<br>\n/g; print FH "<b>$NM::message</b><!-- $ENV{'REMOTE_HOST'} --><br>\n"; if ( $NM::email ){ print FH " \&lt;<a href = 'mailto:$NM::email'>$NM::name</a>\&gt;"; } else { print FH " \&lt;$NM::name\&gt;"; } print FH "<br>\n - $date<p><hr>\n\n"; } else { print FH $_; }} close (FH); open(FH, $gb) or die "Can't open $gb: $!"; local($/) = undef; my $template = <FH>; close(FH); print "Content-type: text/html\n\n$template"; exit (0);
I'd like to add flock to that, but I haven't quite gotten it working for some reason... Any suggestions welcome.

Replies are listed 'Best First'.
Re: Re: simple message board gone very wrong?
by AgentM (Curate) on Dec 12, 2000 at 03:35 UTC
    Nice ++, but you should know that even WITH flock(), you're not absolutely safe. In any interpreted language, ALL function calls not matter how harmless-looking, are interruptable. And "interruptable" means potential race condition when your functions access the same variable (in this case, a file). While with the simple flock() call, your chances are better that you will not end in a race condition (and basically, for messages less than _POSIX_PIPE_BUF in <limits.h> there is almost zero chance of it). But the possibility still exists, so in the interests of full security and functionality, the filesystem should not be depended on as a backbone to a multiuser interfaces. Sadly, that's not an option here and it looks like the %dbhash via Perl's DBM APIs are the best bet (hint, hint, jptxs).
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Re: simple message board gone very wrong?
by turnstep (Parson) on Dec 12, 2000 at 07:49 UTC

    At the top:

    use Fcntl qw(:flock);

    Immediately after the 'read open':

    flock(FH, LOCK_SH);

    Change the 'write open' to:

    open(FH, "<+ $gb") or die "Could not open $gb: $!\n"; flock(FH, LOCK_EX); seek(FH,0,0); truncate(FH,0) or die "Could not truncate $gb: $!\n";

    Once you use flock, make sure that every process that accesess the files also uses flock.

    Also, the "local $/" should be in it's own block, otherwise anything else you add to the bottom of this script will be in "ultraslurp mode"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://46174]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found