Profiling perl

After I’ve complained on #perl channel that dprofpp (perl profiler) dies on me, I received a number of suggestions on how to profile the code without using dprofpp. Ideas ranged from elemetary insertion of ‘print “Started”, time,”\n”‘ to all sorts of dynamic subroutine overloading. Here is one elegant solution that was suggested:

sub foo { print "foo (@_)\n" }
sub bar { print "bar (@_)\n" }

for (qw(foo bar)) {
  my $name = "main::" . $_;
  my $old = *$name{CODE};
  *$name = sub {
    print scalar(localtime), " Started $name\n";
    &$old;
    print scalar(localtime), " Finished $name\n";
  };
}

foo(qw(one two));
bar(qw(three four));

Oh, and someone also mentioned that dprofpp is known to be buggy. Here is a quote from the “Profiling Perl” article at perl.com:

The other problem you might face is that Devel::DProf, being somewhat venerable, occasionally has problems keeping up on certain recent Perls, (particularly the 5.6.x series) and may end up segfaulting all over the place.

Sending bulk HTML emails with perl

First of all, I have to warn you that using HTML in emails is almost always inappropriate. Use plain text. If you have to send HTML, than send it as an attachment.

For those cases, where HTML message has to be sent and there is no choice in the matter, here is a small perl script – send_html_mail.pl

Edit a few lines at the beginning of the file to suit your needs. From address and Subject line must be changed. Locactions of two files – list of recepients and the body of the message – can be either specified on the command line or will be taken from default values hardcoded in the script.

Run the script as:

send_html_mail.pl message.html recepients.txt

If you changed the location of data files or if you are using defaults, than you don’t have to pass any arguments to the script.

List of recepients is expected to be a comma separated list of names and emails. An example file could look like this:

You There,you@there.org
Him Somewhere,him@somewhere.net

Message body file should contain all your HTML, CSS, JavaScript and whatever else is that you want to send. Here is an example file for you:

<HTML>
<BODY>
<H1>Hi there!</H1>
<DIV>This is an HTML message.</DIV>
</BODY>
</HTML>

It is easy to change the script to send plain text messages instead of HTML. Just change the line that specifies Content-Type as “text/html” to “text/plain”.

find2perl

I think I’ve heard about find2perl before, but I never used it. Today I was trying to invent a wheel once again and somehow remembered about this useful tool. Here is a quote from the man 1 find2perl:

NAME
find2perl – translate find command lines to Perl code

SYNOPSIS
find2perl [paths] [predicates] │ perl

DESCRIPTION
find2perl is a little translator to convert find command lines to equivalent Perl code. The resulting code is typically faster
than running find itself.

This utlity usually comes together with perl.

On hiring programmers

Ovid, who is a rather famous Perl programmer himself, has a few tips about hiring programmers for companies who want to do so. Before reading the article, try writing your own ad for Perl programmer position available. Than see if you’ve got it right.

The main thing you want out of an ad is for people to read it. If you have nothing compelling in the ad, the people who will read and respond are the unemployed or the unsatisfied. Why overlook the competent, happy programmers? You want everyone reading your ad.

Perl obfuscations

The flexibility of Perl allows for extremely nicely looking code. On the other hand, the same flexibility allows people to do ugly things, which Perl is very well known for. Sometimes, though, I come across a piece of code which is both ugly and beautiful. Here is one example written by one of my collegues:

sub _uintvar {
    my ($v) = @_;
    $v = sprintf("%b",$v);
    $v = '0'x(-length($v)%7).$v;
    my @v = map {"1$_"} $v=~/(.{7})/g;
    substr($v[-1],0,1) = "0";
    $v = pack("B*",join('',@v));
}

This was found in the production system. It works. And it was tested.

Now imagine that the name of the subroutine would be different in some non-selfdescriptive way. Would you be able to parse it, understand it, and say what it does? How about fixing a bug in there?