[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] newbie problem: Why can't I write to a file?



  I'm new to MacPerl and so far am impressed with its power. However, I am
having a problem with a practice script I got from the book CGI Programming
by Shishir Gundavaram. 
  This is from his guestbook example. It requires that submitted
information be appended to a file (I call it guestbook.html). All my paths
seem to be correct. It just seems that the script can't open the file.
Another possible problem, before I start showing you code, is that I'm
using NetPresenz. I set it up guests to have read only access through
Netpresenz set-up, and I only allow guests 'see-folders' and 'read-files'
in my folder that has the cgi script and the html file (I don't allow them
to 'make-changes'). A third possible non-code problem is that the
guestbook.html file is just a plain text file created by Simpletext (also,
it is completely empty). Do the files that MacPerl scripts write to have to
be a certain creator type?
  Ok, onto the problem. I apologize for posting all of the code, but I'm
not sure what to include and it is fairly short. I removed all of the
comments and added new comments at the point where I get my error.

(The program is able to execute the first time you access it. In my case I
use
http://host-41.subnet-96.med.umich.edu/test/guestbook.cgi?add and that runs
through the appropriate code. However, this code doesn't require that any
files be opened. After presenting me a form to fill out, I fill this out
and hit submit. It then runs until it gets to the point that it has to open
the  file and insteads gives me an error.)

#!/usr/local/bin/perl
$webmaster = "comrade\@umich\.edu";
$method = $ENV{'REQUEST_METHOD'};
$script = $ENV{'SCRIPT_NAME'};
$query = $ENV{'QUERY_STRING'};

$document_root = "Pod People/Pub/www/test";
$guest_file = "/guestbook.html";
$full_path = $document_root . $guest_file;

$exclusive_lock = 2;
$unlock = 8;

if ($method eq "GET") {
        if ($query eq "add") {
                $date_time = &get_date_time();
                &MIME_header ("text/html",  "Robert Decker's guestbook");
                print <<End_Of_Guestbook_Form;

<P>
The current time is: $date_time
<HR>


<FORM METHOD="POST">
<PRE>
<EM>Full name</EM>:     <INPUT TYPE="text" NAME="name" SIZE=40>
<EM>Email Address</EM>:  <INPUT TYPE="text" NAME="from" SIZE=40>
<EM>WWW Server</EM>:     <INPUT TYPE="text" NAME="www"          SIZE=40>
</PRE>
<P>
<EM>Please enter the information that you'd like to add:</EM><BR>
<TEXTAREA ROWS=3 COLS=60 NAME="comments"></TEXTAREA><P>
<INPUT TYPE="submit" VALUE="Add to Guestbook">
<INPUT TYPE="reset" VALUE="Clear Information"><BR>
<P>
</FORM>
<HR>

End_Of_Guestbook_Form

        } else {
                if (open(GUESTBOOK, "<" . $full_path) ) {
                        flock (GUESTBOOK, $exclusive_lock);

                        &MIME_header ("text/html", "Here is my guestbook!");
                                                        
                        while (<GUESTBOOK>) {
                                print;
                                                        }

                        flock (GUESTBOOK, $unlock);
                        close(GUESTBOOK);

                        } else {
                                &return_error (500, "Guestbook File Error",
                                "Cannot read from the guestbook file [$full_path].");
                                }
                }






#The next few lines are where the problem occurs. I understand that the
">>" lets MacPerl know that I want to open the file to have information
appended to it. However, it can't seem to open it and instead goes down to
the error message below.

} elsif ($method eq "POST") {
        if ( open (GUESTBOOK, ">>" . $full_path) ) {
                flock (GUESTBOOK, $exclusive_lock);

                $date_time = &get_date_time();
                &parse_form_data (*FORM);


                $FORM{'name'} = "Anonymous User"                if !$FORM{'name'};
                $FORM{'from'} = $ENV{'REMOTE_HOST'}     if !$FORM{'from'};

                $FORM{'comments'} =~ s/\n/<BR>/g;

                print GUESTBOOK <<End_Of_Write;

<P>
<B>$date_time:</B><BR>
Message from <EM>$FORM{'name'}</EM> at <EM>$FORM{'from'}</EM>:
</P>
$FORM{'comments'}

End_Of_Write
                                        

                if ($FORM{'www'}) {
                        print GUESTBOOK <<End_of_Web_Address;
                        
<P>
$FORM{'name'} can also be reached at:
<A HREF="$FORM{'www'}">$FORM{'www'}</A>

End_of_Web_Address

                                                }

                print GUESTBOOK "<P><HR>";

                flock (GUESTBOOK, $unlock);
                close(GUESTBOOK);

                &MIME_header ("text/html", "Thank You!");

                print <<End_of_Thanks;

Thanks for visiting my guestbook. If you would like to see the
guestbook,
click <A HREF="$guest_file">here</A> (actual guestbook file),
or <A HREF="$script">here</A> (guestbook script without a query).

End_of_Thanks


                } else {
                        &return_error (500, "Guestbook File Error",
                                        "Cannot write to the guestbook file [$full_path].")
                                                }
        } else {
                &return_error (500, "Server Error",
                        "Server uses unsupported method");
                }
exit(0);


sub MIME_header
{
                        local ($mime_type, $title_string, $header) = @_;

                        if (!$header) {
                                                $header = $title_string;
                        }

                        print "Content-type: ", $mime_type, "\n\n";
                        print "<HTML>", "\n";
                        print "<HEAD><TITLE>", $title_string, "</TITLE></HEAD>", "\n";
                        print "<BODY>", "\n";
                        print "<H1>", $header, "</H1>";
                        print "<HR>";
}

sub get_date_time
{

        $months = "January/Febraury/March/April/May/June/July/" .
                        "August/September/October/November/December";
        $weekdays = "Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday";
                        
        local ($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst)
                        = localtime(time);
                                                        
        if ($hour > 12) {
                $hour -= 12;
                $ampm = "pm";
                } else {
                        $ampm = "am";
                        }

        if ($hour == 0) {
                $hour = 12;
                }

        $year += 1900;

        $week = (split("/", $weekdays))[$wday];
        $month = (split("/", $months))[$nmonth];

        $time_string = sprintf("%s, %s, %s, %s, - %02d:%02d:%02d %s",
        $week, $month, $day, $year, $hour, $min, $sec, $ampm);

        return ($time_string);
        }

sub parse_form_data
{
        local (*FORM_DATA) = @_;
                        
        local ( $request_method, $post_info, @key_value_pairs,
                $key_value, $key, $value);

        read (STDIN, $post_info, $ENV{'CONTENT_LENGTH'});

        @key_value_pairs = split (/&/, $post_info);
                        
        foreach $key_value (@key_value_pairs) {
                ($key, $value) = split (/=/, $key_value);
                $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

        if (defined($FORM_DATA{$key})) {
                $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
                } else {
                        $FORM_DATA{$key} = $value;
                        }
        }
}


sub return_error
{
        local ($status, $keyword, $message) = @_;

        print "Content-type: text/html", "\n";
        print "Status: ", $status, " ", $keyword, "\n\n";

        print <<End_of_Error;

<title>CGI Program - Unexpected Error</title>
<h1>$keyword</h1>
<hr>$message</hr>
Please contact $webmaster for more information.

End_of_Error

                        exit(1);
}

--                                                             G
/\    /\   /\   /\    /\   /\   /\    /\   /\   /\    /\   / T    A
001\/1100\/10\/1010\/0110\/00\/1111\/0111\/CT\/TCGA\/GTAC\/C  A \C
110/\0011/\01/\0101/\1001/\11/\0000/\1000/\GA/\AGCT/\CATG/\GA /  G
\/    \/   \/   \/    \/   \/   \/    \/   \/   \/    \/   \/  \  T
Robert A. Decker                 voice: (313) 936-9779        A   T
Computer Consultant I                                   
Department of Radiology   "...millionaire stuntman - half jackalope."
University of Michigan                                -Chris Elliott