I gave a talk about Perl dependency isolation at Salt Lake Perl Mongers.
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
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
.
I gave a talk about functional programming with Perl at Salt Lake Perl Mongers.
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!
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.
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:
Here’s a nice trick from perlfunc
’s map
entry:
To force an anon hash constructor use "+{":
@hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs
# comma at end
to get a list of anonymous hashes each with only one entry apiece.
Say you have a hash:
my %hash = (foo => 'fooey',
bar => 'barey',
baz => 'bazey',
blech => 'blechey');
and you want to rename some of the hash keys, create another hash that holds the new names of the keys:
## old-name => new-name
my %new = (foo => 'foooo',
bar => 'baaar');
Then this is a fast way to rename the keys. Measured on 2.4Ghz i5 MacBook Pro.
To selectively profile during only a portion of the code, invoke like this:
$ NYTPROF=start=no perl -d:NYTProf t/load.t -v
Then inside t/load.t
add the following directives which tell Devel::NYTProf
to start and stop profiling:
DB::enable_profile();
... ## profile this section
DB::finish_profile();
Add use Devel::NYTProf;
in either the test file or in the module.