Considering that the code you just provided is not even close to what your guidelines were suggestion he do, you just cheated. A program following your guidelines would have looked like the following, which is long winded. And by long-winded, I don't mean code length, I mean that you're doing more work than is necessary (in this case, the overhead of unnecessary file output). Note that I didn't bother with file locking this time around.
#!c:/perl/bin/perl -w $|++; use strict; print "Enter new student's name: "; chomp( my $student = <STDIN> ); open my $fh, '<', 'students.dat' or die "open failed: $!"; open my $tmp, '>', 'students.tmp' or die "open failed: $!"; my $found = 0; while (<$fh>) { print $tmp $_; $found = 1 if /^\Q$student\E$/i; } print $tmp $student, "\n" unless $found; close $fh; close $tmp; rename 'students.tmp', 'students.dat';
In reply to Re^4: appending to a file
by Anonymous Monk
in thread appending to a file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |