I don't know where else this goes, so I'm putting in here.
Add to this list as you see fit. Make it a good place for
people to look. While there are only a few things here now,
I'd like to see this get quite large.
Do as little as possible
Compare these two lines of code:
@small = grep { $_->size < 100 } sort $query->all_records;
# vs.
@small = sort grep { $_->size < 100 } $query->all_records;
Notice a difference? As the number of records increases,
the first method will get slower. Why? Because you weren't
smart enough to sort only the data you need. The two produce
identical lists (unless you have severe magic going on, in
which case, I don't wanna hear about it), but the second will
produce it in better time, since you're only sorting the
matches, not all the records.
Use the proper tool
Don't use
m// where
substr() will do. Don't
use
substr() where
unpack() will do. Don't
use
m// where
index() (or even better,
rindex()) will do. Don't use
s/// where
tr/// will do.
Knowing when to use a hammer and when to use a
jackhammer is a valuable skill in programming; and
even moreso in Perl, where TMTOWTDYOG (there's more than one
way to dig your own grave). Here are examples of the above:
- m// vs. substr()
($short) = $desc =~ /^(.{0,100})/;
# breaks on embedded newlines, and is better as
$short = substr($desc, 0, 100);
- substr() vs. unpack()
$name = substr($rec, 0, 20);
$age = substr($rec, 20, 5);
$job = substr($rec, 25, 25);
# repeated calls to substr() better as unpack()
($name,$age,$job) = unpack 'A20 A5 A25', $rec;
- m// vs. index() or rindex()
if ($str =~ /jeff/) { ... }
# why not
if (index($str,'jeff') > -1) { ... }
# or if you know that if it's there, it's toward the end:
if (rindex($str,'jeff') > -1) { ... }
- s/// vs. tr///
# (on average)
s/[abc]*//g; # is FAR slower than
s/[abc]//g; # is slower than
s/[abc]+//g; # which is slower than
tr/abc//d;
Love your string functions
Regexes are not the answer. They're
an answer. I
prefer
lc($A) eq lc($B) to some convoluted, anchored
regex like
$A =~ /\A\Q$B\E\z/i (for reasons I hope
you can see). And check this crazy trick -- to find the first
occurrence of a lowercase or uppercase letter in a string,
there's no need to use a regex, just use:
$pos = index(lc($str), lc($c)).
Add more. That's more than a suggestion.
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.