in reply to perl/cgi question: script works from unix command line, but not web page

Be sure to always check return values.
I hope you find this rewrite helpful -- I think it's roughly equivalent to what you're trying to do.
Notice how easily the CGI.pm can handle the reading and writing for you :-)

#!/usr/bin/perl -w # -w to turn on warnings # XXX These should be fully qualified paths our $DATA_FILE = 'character.data'; our $FORM = 'save.htm'; use strict; # you should almost always use this use IO::File; use CGI qw/ -debug /; # parse the user's submission my $q = CGI->new; # fetch previously stored data my $fh = IO::File->new($DATA_FILE) or die "unable to read '$DATA_FILE' +: $!"; my $disk = CGI->new($fh); # overwrite values foreach my $key ($q->param) { $disk->param($key, $q->param($key)); } # XXX What happens when two users run this script simultaneously? # Answer: data corruption. We need locking. $fh = IO::File->new("> $DATA_FILE") or die "unable to write '$DATA_FIL +E': $!"; $disk->save($fh); # XXX Better to move the common code into a module. # How do we know if system() worked? See $? in perlvar # This should be fully qualified too. #system 'menu.cgi' ; my $out = join "\n", map { "$_=" . $disk->param($_) } $disk->param; $fh = IO::File->new($FORM) or die "unable to read '$FORM': $!"; print $q->header, <$fh>, $out;

Replies are listed 'Best First'.
Re: Re: Stupid newbie question - perl/cgi
by geoffhanna (Initiate) on Apr 03, 2002 at 04:03 UTC
    This is a stunning amount of work just to help out a newbie.

    Thanks! Of course I still don't understand half of what you have in here (grin). This is cool though, most of the examples I've found on the web so far are either too simple to be very useful, or insufficiently explained/commented and I can't tell what they are supposed to do.

    What does this (use IO::File) do? It looks like you are instantiating an object, but I can't find a reference for the 'use' command. I've been using this language reference.

    Any suggestions for a better online reference?

      The perlfunc section of docs in your development environment is probably most convenient. In ActiveState those are distributed in a winhelp format and accessable through the AS start menu. On unix just type 'perldoc -f use'. use is like #include in C, but with specific behavior at compile time, and with regard to multiple inclusion.

      1. perldoc perlfunc
      2. perldoc perlsyn
      3. perldoc perlop
      4. perldoc IO::File

      After Compline,
      Zaxo

      The reference you're using is for Perl 4, so it's obsolete. Take a look at www.perldoc.org.

      I'd suggest you install Perl locally if you haven't already. It comes with a wealth of documentation that is very easy to use. For example, if you want to find out what use does you can simply run perldoc -f use.

      The use IO::File just loads the IO::File module. And then, yes, I'm instantiating it. I just wanted to show you that there's an OO interface available for file operations. And again, if you have Perl installed, you should be able to just type perldoc IO::File to read about it.