in reply to Code for readablity or fewer lines?
Before I introduced this trick, they'd been reading through the file line-by-line, and using regexes (without using qr// objects) which got a bit expensive. So this was a speed boost, and the brief comments are all that was needed to explain the process.sub updateRecord { my ($srml_file,$n,$new) = @_; my ($offset, $data, $post); local $/; # no concurrent access, so no locking open CACHE, "+< $srml_file" or return setError($!); $/ = "<!-- START RECORD $n -->\n"; <CACHE>; # get up to the good stuff $offset = tell CACHE; $/ = "<!-- END RECORD $n -->\n"; <CACHE>; # waste what's there undef $/; $post = <CACHE>; # get what's left seek CACHE, $offset, 0; truncate CACHE, $offset; # in case new data is shorter print CACHE "<!-- START RECORD $n -->\n", getSRML($new), "<!-- END RECORD $n -->\n", $post; close CACHE; return -s $srml_file; }
The measure of efficiency comes not from the number of lines used, but from the algorithms used. Redundancy can occur in small programs as well as large ones.
japhy --
Perl and Regex Hacker
|
|---|