Lisp and Perl comic
This comic from xkcd gave me a laugh.
Summer reading 2006
I’ve been busy with everything else in my life, so I haven’t done much reading. Things have quieted down enough and I’ve picked up:
- [Perl Hacks](http://isbn.nu/0596526741). Cool little book, full of ideas and tricks you might have known and many more you didn’t. Reminded me of looking over the shoulder of a clever programmer and going, “Aha.” I owe [dha](http://www.panix.com/~dha) a couple of good beers to make up for the review blurb I promised to write but didn’t when I was caught up in closing on a house and moving.
- [Extending and Embedding Perl](http://isbn.nu/1930110820). I’m
working through it now and so far I can say: a) this book was necessary,
b) thank you Tim and Simon, and c) XS is never going to be easy but at least it needn’t be a mystery.
Reflow using Text::Autoformat
Filladapt mode is good but the Perl module [Text::Autoformat](http://www.perl.com/CPAN/modules/by-module/Text/Text-Autoformat-1.13.tar.gz) is better at reflowing text for email. I cobbled together the following elisp to make that easy.
(defun rel-autoformatter (begin end rightmargin)
"Run Text::Autoformat on region"
(shell-command-on-region begin end
(format "perl -MText::Autoformat -e 'autoformat({all=>1,right=>%d})'" rightmargin) t t )
(message "done."))
(defun rel-autoformat (begin end)
Text::Autoformat on region, RFC recommended right margin of 72"
(interactive "r")
(rel-autoformatter begin end 72))
(defun rel-autoformat-quote (begin end)
"Text::Autoformat on region, right margin of 60, suitable for quoting replies"
(interactive "r")
(rel-autoformatter begin end 60))
(global-set-key (kbd "C-c f f") 'rel-autoformat)
(global-set-key (kbd "C-c f q") 'rel-autoformat-quote)
It might be nice to handle parameters other than right margin- and do it in a cleaner way than explicitly specifying them. I doubt I’ll get around to it since Text::Autoformat usually does the right thing for me with just the margin hint.
Clever timestamps but is it a good idea?
At work I have a collection of long-running Perl programs exec-ed by a rc script that have their noise redirected to a single intermingled log. The scripts themselves and the modules they use are littered with carps, warns and print STDERRs depending upon when and by whom they were written. I can differentiate their output (more or less) but I’d like it timestamped for forensic purposes.
My first thought, heaven help me, involving tying filehandles is exampled by this hack:
!/usr/local/bin/perl -w use Tie::Handle; use Carp; use strict; package TimeStamper; @ISA = qw(Tie::Handle); sub wrap { my $fh = shift; my $fh_name = $fh; $fh_name =~ s/^\*//; local *MYFH; open(MYFH, “>&$fh_name”) or die(“Failed to dupe [$fh_name]: $!”); tie($fh,‘TimeStamper’,*MYFH); } sub TIEHANDLE { my ($class,$fh) = @_; my $obj = bless [$fh],$class; return $obj; } sub PRINT { my $self = shift; my $fh = $self->0; myt = localtime();
print sprintf("[%4d/%02d/%02d %02d:%02d:%02d] ",
$t[5]+1900,t[4,3,2,1,0]),@_;
}
package main;
TimeStamper::wrap(*STDERR);
print STDERR “This is timestamped.\n”;
warn(‘This is timestamped.’);
carp(‘This is timestamped.’);
print “This is not timestamped.\n”;
Neat. Even half clever. But I’m not sure it’s a good idea.
Perl Best Practices
One of my colleagues mentioned that they had their reviewer copy of Damian Conway’s [Perl Best Practices](http://isbn.nu/0596001738), that several other colleagues had contributed, that it was damn good and that it should be company policy to have everyone read it before writing a line of code. Not many technical books get that kind of rave (Hunt and Thomas’s [The Pragmatic Programmer](http://isbn.nu/020161622X) is another, if you code and don’t have it, get it) so I preordered.
I’m glad I did. I’ve been understandably busy but I read through it, made my notes, updated my environment and began adopting the advice. It is every bit as good as expected and immediately useful. I was particularly struck by the refinements on building “inside-out” classes (a trick I was exposed to in one of Damian’s courses and similar to the Flyweight pattern in his book [Object Oriented Perl](http://isbn.nu/1884777791)) but it’s full of simple and pointedly obvious tips like using the modules List::Utils, List::MoreUtils and Fatal and applying Regexp::Common instead of rolling your own. Not all the advice is easy to accept, and I admit to taking it piecemeal, but it’s a great book.
Link Propagation: Chip Salzenberg
A well-respected programmer, seeing that his employer was engaging in behavior that was unethical and in some jurisdictions illegal, sent a lengthy letter distancing himself from their activity and stating that he cannot continue their relationship and may contact the authorities. Their reaction was unsurprising.
Here’s the link: Health Market Science is evil
Ross Lonstein