PERL - Help!

They have: 1 posts

Joined: Mar 2009

Hi all,
Any help is great appreciated.

I am working on a script for class that is meant to create a guestbook and display the entries. For some reason, my save_to_file is not working. It doesn't put any information into the "comments.txt." file and when the form is submitted, a blank page is created in the browser.

Any suggestions?

#!/usr/bin/perl
#coffee.cgi - saves form data to a file, and creates
#three different dynamic Web pages
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);

#prevent Perl from creating undeclared variables
use strict;

#declare variables
my ($fname, $lname, $city, $state, $country, $email, $comments, $data_ok, $msg );

if ($ENV{'REQUEST_METHOD'} eq "POST") {
($fname, $lname, $city, $state, $country, $email, $comments) = get_input();
($fname, $lname, $city, $state, $country, $email, $comments) = format_input();
($data_ok, $msg) = validate_input();
if ($data_ok eq "Y") {
save_to_file();
create_acknowledgment_page();
}
else  {
create_error_page();
}
}
else {
create_comments_page();
}

exit;

#*****user-defined functions*****
sub get_input {
return param('fname'), param('lname'), param('city'), param('state'), param('country'), param('email'), param('comments');
} #end get_input

sub format_input {
#declare and assign values to temporary variables
my ($fn, $ln, $ci, $st, $cou, $com, $e);
($fn, $ln, $ci, $st, $cou, $com, $e) = ($fname, $lname, $city, $state, $country, $comments, $email);
#remove leading and trailing spaces from fname
$fn =~ s/^ +//;
$fn =~ s/ +$//;
#remove leading and trailing spaces from lname
        $ln =~ s/^ +//;
        $ln =~ s/ +$//;
#remove leading and trailing spaces from city
        $ci =~ s/^ +//;
        $ci =~ s/ +$//;
#remove leading and trailing spaces from state
        $st =~ s/^ +//;
        $st =~ s/ +$//;
#remove leading and trailing spaces from country
        $cou =~ s/^ +//;
        $cou =~ s/ +$//;
#remove leading and trailing spaces from email
        $e =~ s/^ +//;
        $e =~ s/ +$//;
#remove leading and trailing whitespace character from comments
$com =~ s/^\s+//;
$com =~ s/\s+$//;
#replace return and new line combination within comments with a space
$com =~ tr/\r\n/ /;
#remove extra spaces from within comments
$com =~ tr/ //s;
return $fn, $ln, $ci, $st, $cou, $com, $e;
} #end format input

sub validate_input {
my $valid ="Y";
my $errormsg;
if ($fname eq "" or $lname eq "" or $city eq "" or $state eq "" or $country eq "" or $email eq "" or $comments eq "") {
$valid = "N";
$errormsg = "complete all items";
}
return $valid, $errormsg;
} #end validate_input

sub save_to_file {
open(OUTFILE, ">>", "comments.txt")
or die "Error opening comments.txt for save. $!, stopped";
print OUTFILE "$fname|$lname|$city|$state|$country|$email|$comments\n";
close(OUTFILE);
} #end save_to_file

sub create_acknowledgment_page {
print "<HTML>\n";
print "<HEAD><TITLE> Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>$fname $lname, from $city, $state,  thank you for the following \n";
print "comments:<BR><BR>$comments\n";
print "</H2></BODY></HTML>\n";
} #end create_acknowledgment_page

sub create_error_page {
print "<HTML>\n";
print "<HEAD><TITLE>Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Please return to the form and \n";
print "$msg.</H2>\n";
print "</BODY></HTML>\n";
} #end create_error_page

sub create_comments_page {
my ($fname_field, $lname_field, $city_field, $state_field, $country_field,  $email_field, $comments_field);

open(INFILE, "<", "comments.txt")
or die "Error opening comments.txt. $!, stopped";

print "<HTML>\n";
print "<HEAD><TITLE>Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>What other visitors have to say say \n";
print "about our site:</H2>\n";
while (<INFILE>) {

chomp($_);
($fname_field, $lname_field, $city_field, $state_field, $country_field,  $email_field, $comments_field) = split(/\|/, $_);
print "<B>Name:</B> $fname_field $lname_field<BR>\n";
print "<B>City:</B> $city_field<BR>\n";
print "<B>State:</B>$state_field<BR>\n";
print "<B>Country:</B>$country_field<BR>\n";
print "<B>Email:</B>$email_field<BR>\n";
print "<B>Comments:</B> $comments_field<BR>\n";
print "<HR>";
}
close (INFILE);
print "</BODY></HTML>\n";
} #end create_comments_page

There's my Perl.

Thanks again Shocked)

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Hi, welcome to TWF Smiling

I'm not familiar with perl, so some of the above means nothing to me and someone who knows perl might see an error in your code.

However, I can suggest you echo out the $comments var throughout your script. Right after you use the $comments, echo it out to see if it still contains the data it should.
I find this works very well for finding where an issue occurs so I can then investigate that part of the code.

Try using your function to write some simple text to the file, not using your vars, just write "test text" and see if that works.
If yes then there is something wrong with your var data in that function (perhaps global vars like in PHP?), if it doesn't work then there is something else wrong with the function, the file opening, permissions, file writing etc.

As for the blank page:
I presume it's opening the file ok, as you have a 'die' clause and you didn't state it outputs an error.
So, try using your function to write some simple text to the page (as with the suggestion for the comments.txt file)

Again I don't know perl, but...
From a quick look on the net, I find this:
open (OUTFILE, ">>file.txt")
where you have:
open(OUTFILE, ">>", "comments.txt")
I.E. you close the >> with ",.
But perhaps your method is also ok.

They have: 121 posts

Joined: Dec 2008

It should work - It works in my test environment.

Do you have permissions set right on 'comments.txt'? It will need to be writable by the user running the webserver.

Since you're only appending ('>>'), comments.txt is created, right? (touch comments.txt and change permissions if required)

Cheers,
Shaggy.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.