2015
Perl Dependency Isolation

I gave a talk about Perl dependency isolation at Salt Lake Perl Mongers.

2015-01-14    
Devel::Cover Notes

Some notes for using Devel::Cover.

Gathering coverage

CPAN::Reporter doesn’t have a build step, so we prove -l; we also don’t want to cover the world, so we set -inc=lib:

HARNESS_PERL_SWITCHES=-MDevel::Cover=-inc=lib prove -l

or:

PERL5OPT=-MDevel::Cover prove t/some-test.t

This will run the prove utility and turn on code coverage.

Selecting only one file to cover

cover -select=lib/Some/Module.pm

You may add multiple -select options.

Ignoring files to cover

cover -ignore_re=^/var/core_lib -ignore=/usr/bin/prove
2015-01-14    
2014
Hashing to an 8-bit Integer

I needed to come up with a way to spread out chunks of a subnet among a handful of users, so I came up with this routine:

sub hash256 {
    my $str  = shift;
    my $hmac = hmac_sha256($str);
    my $val  = unpack "L*" => $hmac;
    my $hash = $val % 256;

    return $hash;
}

Used:

for my $str ( qw/scott paul mark jayce rvd james/ ) {
    say sprintf "%-15s %d" => $str, hash256($str);
}

Now the result of my $hash = hash256('joe') can be used as, say, the third octet in a cidr: 192.168.$hash.0/24.

2014-12-16    
tmux Notes

scroll mode

tmux has a scroll mode where you can use page up/page down and arrows to scroll the buffer back. To enter scroll mode, C-b <page up>. To get out of scroll mode, <Esc>. Also C-b [ turns on scroll mode.

sharing

Poor man’s screen sharing:

$ tmux -S /tmp/tmux-shared new-session -s session-name
(detach)
$ chmod 777 /tmp/tmux-shared
(attach)

Person 2 attaches:

$ tmux -S /tmp/tmux-shared attach-session -s session-name
2014-12-15    
Functional Programming with Perl

I gave a talk about functional programming with Perl at Salt Lake Perl Mongers.

2014-11-19    
procmail Notes

I don’t mess with procmail much anymore, but maybe these will be useful to someone.

Split a mailbox into separate files for each message:

$ formail -s sh -c 'cat - > foo.$FILENO' < klez.file

Resend a mailbox (foo) through a set of filters (rc.test):

$ formail -s procmail ./rc.test < foo

Move the last 10 messages from folder foo and put them in folder bar:

$ MSGS=`egrep '^From ' foo | wc -l`
$ formail +`expr $MSGS - 10` -s < foo > bar

Check which recipes triggered most often (based on a verbose log format):

2014-10-26    
Asynchronous Programming Patterns in Perl

I gave a talk about asynchronous programming patterns in Perl at Salt Lake Perl Mongers. This presentation ultimately landed me a new job—thank you to SLC.pm!

non-blocking

2014-10-15    
Perl Log Processing Goodies

Here are some Perl goodies I forget to write down once I’ve remembered them long enough to solve my problem.

Line range

If you want to print all the lines in a file after a certain line number:

perl -ne 'print if (2655641 .. -1)' some.log

This will print from line 2655641 to the last line (-1).

Omiting chunks

Sometimes you have chunks of lines, or multi-line records, that you want to skip.

2014-09-24    
DBD::mysql Integer Type Coercion

DBD::mysql knows that certain MySQL column types are integers with this call:

$sth->execute;
$nums = $sth->{'mysql_is_num'} || [];

You can iterate over $nums and see which columns are numeric-type columns. However, when you pull the data out, it’s converted to a string. So, what used to be as simple as this:

my $accounts = $sth->fetchall_arrayref({});

now requires this kind of trickery:

my @accounts = ();
my $is_num   = $sth->{'mysql_is_num'} || [];
while (my $row = $sth->fetchrow_arrayref) {
    push @accounts, {
        map {
            $Acct_Fields[$_] =>   ## key
              ($is_num->[$_]      ## check to see if it's int
                ? 0 + $row->[$_]  ## coerce to integer/numeric
                : $row->[$_])     ## leave it alone
        } (0 .. $#Acct_Fields)    ## number of keys
    };
}

This seems to work as a primary test to see if a scalar ($val) is an number-ish thing:

2014-09-10    
Use git-crypt to Store Secrets in Git

git-crypt (github) keeps your secrets safe in a git repository. It decrypts on checkout and encrypts at commit using standard git hooks. Once configured, it is completely transparent.

I had a situation where the secrets were already in the repo and I needed to encrypt them (if you’re in this situation, you should also change your secrets because git log -p).

To encrypt files (foo.conf, bar.conf) already in the repo:

2014-07-15