Click to See Complete Forum and Search --> : Regular expression just not working...


Stavrogin
01-25-2004, 05:31 PM
I don't get it. The third regular expression in this program ($all_entries =~ s/%/$newline/;) works just fine. But the first two, trying to launder the $name and $comment variables from any double quotes, just refuses to work. I look in the datafile afterwards, and the script has executed and written to the datafile, but without substituting the double quotes for single ones.

here's the code:

#!/usr/bin/perl


$data_file = '/Library/Webserver/Documents/datafile.txt';


use CGI;
use Fcntl;
$query = new CGI;

print "Content-type:text/html\n\n";
$name = $query->param('Name');
$comment = $query->param('Comment');
$entryid = $query->param('entryid');

#launder the name of any double quotes
$name =~ s/"/'/g;
$cleanname = $name;

#launder the comment of any double quotes
$comment =~ s/"/'/g;
$cleancomment = $comment;

#assemble finished entry
$newline = <<"walnuts";
%//
if (entryid=='$entryid')
{com_pane.document.writeln("<P>$cleanname</p><p>$cleancomment</p>");
}
walnuts

#open non-destructively, feed entries into a string
open ENTRIES, "$data_file", or die "can't open $data_file: $!";
flock(ENTRIES, 2) or die "can't LOCK_EX $data_file: $!";
while (<ENTRIES>) {
$all_entries .= $_;
}
$all_entries =~ s/%/$newline/;
$newdatafile = $all_entries;
close ENTRIES;
open NEWENTRIES, ">$data_file" or die "Can't open $data_file for write: $!";
print NEWENTRIES "$newdatafile" or die "can't print to $data_file: $!";
close NEWENTRIES or die "can't close $data_file: $!";


Anyone know what the problem is? Syntax checks out ok, according to perl -c.

Thanks.