
japhy
Enthusiast
Feb 18, 2000, 5:42 AM
Post #7 of 8
(669 views)
|
|
Re: Deleting from data file...?? - Syntax help...
[In reply to]
|
Can't Post
|
|
Tim, the following code is just fine: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use Fcntl ':flock'; # for LOCK_EX open PWD, "+<$members_file"; flock PWD, LOCK_EX; $_ = do { local $/, <PWD> }; $found = s/^\Q$username\E:.*\n//mg; seek PWD, 0, 0; truncate PWD, 0; print PWD; close PWD; if ($found) { print "$username deleted" } else { print "$username not found" } </pre><HR></BLOCKQUOTE> I changed the regex (removed the final $) to make it work as you asked. You should use the Fcntl.pm constant LOCK_EX instead of 2 for flock(), and you should not unflock a file manually. Perl does it for you. This is a perfectly fine method of doing this. You can also do: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> { local ($^I, @ARGV) = ("", $members_file); /^\Q$username\E:/ or print while <>; } </pre><HR></BLOCKQUOTE> There's magic going on there -- explainable magic, but magic nonetheless -- and it's Perl's version of in-place editing. It's rather graceful.
|