While idly reading the Camel (3rd edition) I noticed on page 99 that if you do a stat or file test on a file, then you can do more tests on the same file using _ as Perl caches the initial file test - thus reducing the number of system calls Perl has to make.
Thus you can do:
if (-e $file && -T _) {
print "foo";
}
instead of the more obvious, though slower:
if (-e $file && -T $file) {
print "bar";
}
I was quite inpressed with this, indeed it's mentioned again in chapter 24. I tried some tests this morning, and using the underscore is quite a bit faster on my Wintel box, than separate tests.
I've never seen this construct before, though it's easy enough to miss, and even though I've seen it now it's hard to search for an underscrore like this, as underscore is used in some many other places.
Q1 Does anybody actually use this construct? I don't recall seeing it in any books, though I've just this minute found it in the excellent Effective Perl Programming
Q2 What other little tricks like this have I missed? I know there are many things I must have missed, so this question is like asking how long is a piece of string, but what are peoples favourite coding constructs that are "cool" but uncommon
As ever, thanks in advance...
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.