Perl Source Code: fedit.pl
#!/usr/bin/perl
use CGI qw(:standard);
use common;
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
# Get the ID (line number) of the item we want to edit
if (param('id') eq "") {
print ("Location: fread.pl\n\n");
}
my $id = param('id');
# Verify the file has this many lines
open (my $fh, "../data/courses.txt");
my @courses = <$fh>;
my $ncourses = @courses;
if ($id < 0 || $id > $ncourses-1) {
print ("Location: fread.pl\n\n");
}
my $line = myUtils::trim($courses[$id]);
my @coursedata = split (/\|/, $line);
myUtils::headers1();
myUtils::headers2();
print myUtils::sidebar ("perl", "fedit", "pl");
print " <div id='content'>\n";
print " <h2>Perl: Edit the data file</h2>\n";
print_form (@coursedata);
#----------------------------------------------------
# The main Perl code is here.
#----------------------------------------------------
if (param ('submit') eq 'Edit' && formHasData()) {
# write the new data to a temp file
#
open (my $fho, '>>', "../data/courses.tmp");
for ($i=0; $i<$ncourses; $i++) {
if ($i == $id) {
print $fho param('college') . "|" . param('dept') . "|" . param('course') . "|" . param('desc') . "\n";
}
else {
print $fho $courses[$i];
}
}
close ($fho);
close ($fh);
rename ("../data/courses.txt", "../data/courses.bak");
rename ("../data/courses.tmp", "../data/courses.txt");
print_feedback ();
}
print " <p><a href='fread.pl'>Show the contents of this file »</a></p>\n";
print " <p><a href='source.pl?f=4'>Show Perl source code »</a>\n";
print " </div>\n";
myUtils::footers();
#----------------------------------------------------
sub formHasData
#----------------------------------------------------
{
@formParams = ( 'college', 'dept', 'course', 'desc' );
foreach $p (@formParams) {
if (myUtils::trim(param($p)) ne "") {
return 1;
}
}
return 0;
}
#----------------------------------------------------
sub print_form
#----------------------------------------------------
{
print <<form;
<form name='coursesform' action='fedit.pl' method='POST'>
<fieldset>
<legend>Edit a course</legend>
<table>
form
print (" <tr><td>College:</td><td><input type='text' name='college' value='$_[0]' /></td></tr>\n");
print (" <tr><td>Department:</td><td><input type='text' name='dept' value='$_[1]' /></td></tr>\n");
print (" <tr><td>Course #:</td><td><input type='text' name='course' value='$_[2]' /></td></tr>\n");
print (" <tr><td>Course name:</td><td><input type='text' name='desc' value='$_[3]' /></td></tr>\n");
print (" <input type='hidden' name='id' value='$id' />\n");
print <<endform;
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
</table>
</fieldset>
</form>
endform
}
#----------------------------------------------------
sub print_feedback
#----------------------------------------------------
{
print ( "<p>The following data was edited in the data file: </p>\n");
print ( "<table class='results'>\n");
print ( " <tr><th>Field</th><th>Old data</th><th>New data</th></tr>\n");
print ( " <tr><td>College</td><td>$coursedata[0]</td><td>" . param('college') . "</td></tr>\n");
print ( " <tr><td>Department</td><td>$coursedata[1]</td><td>" . param('dept') . "</td></tr>\n");
print ( " <tr><td>Course #</td><td>$coursedata[2]</td><td>" . param('course') . "</td></tr>\n");
print ( " <tr><td>Course name</td><td>$coursedata[3]</td><td>" . param('desc') . "</td></tr>\n");
print ( "</table>\n");
}