#!/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_FILE': $!"; $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;