[Boulder.pm] Boulder-pm Digest, Vol 39, Issue 11

Rob Nagler nagler at bivio.biz
Thu Aug 23 10:52:59 PDT 2012


> open MYSELF, '<', $0;
> print while (<MYSELF>);

Since this is a perl list, and people have mentioned learning perl,  I
have a comment about this style.  Walter has known me a long time so
I'm sure he knows this is in the best interest of the programming
public:

* Don't use <>.  It's an ambiguous operator, meaning glob and
readline.  Searching for all instances of <bla> is almost impossible.
You might as why would you ever do that?  Well, if you wanted to
modularize access to the file system, because you now have to share
state across multiple servers.

* Don't use implicit $_.  That's something that has bitten us more
times than I can count.  If you do insist on using $_, make it
explicit and do a local($_).  Avoid multiple calls when you can.  It's
convenient to suck in lines and print them out, but that's extra
parsing, calls, and doesn't describe what you are doing.  Use sysread
and syswrite, because you don't need the buffering of stdio anyway.

* Check error codes, always.  If the program can't read itself, it
should die.  You may say, "it's just a hack?"  Hmmmm... How many of
you have had a credit card stolen?  Even though it is handled by the
credit card company, it's a total hassle to not have that credit card
for 10 days and/or have to update all your autopays to the new credit
card.  Good code begins with the hacks, and works its way upward.

* Don't rely on file handles closing.  Do it explicitly. If you are
relying on a stdio library (readline does), then check the return
result of the close, because it may return an error.

Just my $.02.

Rob


More information about the Boulder-pm mailing list