I advocate brevity and conciseness in my code, as well as using idioms and any little efficiency-fiend out there. (This is not to say my code is not readable or maintainable -- I just choose to code in a form that is understandable and brief.) As such, I use
tr/// for character deletions instead of some
s///g. I use tricks where they save time and effort. For my job, we keep information in a form of XML in files. When I need to go through the file and change a section, I don't use regexes. I use
$/:
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;
}
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.
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.