Giving thanks as thanks

Ricardo wrote an article "promoting little gifts", which @perlbuzz tweeted as "giving little gifts as thanks".

I like that idea, and it inspired me to remind people to please also consider just giving "thanks", too.

Sure, I'd love a song or a small ebook (and maybe I should set up a wishlist), but I also love getting an email saying thanks, particularly when it is from someone I don't already know telling me that they like a module I released to CPAN or telling me how they are using my code to solve some problem they have. Email is cheap, but still has huge impact.

Perl is a community built on altruism. Don't forget to say thank you!

Posted in perl programming | Tagged , , | 5 Comments

Code munging with vim: if modifier to if block

Not infrequently, I discover I want to turn an if modifier into an if block so I can add extra logic when the conditional is true.

# from this
do_something()
  if condition();

# to this
if ( condition() ) {
  do_something();
}

I decided to automate that with a vim macro:

map ,if V:s/\(\s*\)  if \(.*\);/\1if (\2) {/<CR>kVdp>>$a;<CR><BS>}<CR><Esc>kkk^

For it to work, the if modifier has to be on a line of its own (which I usually do for code clarity) and the cursor needs to be on that line when running the macro.

Feel free to copy and adapt to your own needs and preferences.

Posted in perl programming | Tagged , , , | Leave a comment

Five ways to install module prereqs by hand

Have you ever tried installing a CPAN module's dependencies by hand? Imagine that you've downloaded a strange tarball or cloned a strange module repository. Running "perl Makefile.PL" or "perl Build.PL" gives you a long list of missing dependencies. How do you quickly install all those dependencies?

Here are some options. See the last for a new way that I just cooked up and released to CPAN.

Use your favorite CPAN client against the current directory

Example:

$ cpan .

Beware that this also installs the module in the current directory, which may not be what you want.

cpanminus is smarter than that if you give it an option

$ cpanm --installdeps .

For Module::Build-based modules: Build installdeps

Example:

$ perl Build.PL
$ Build installdeps

For (some) Module::Install-based modules: make installdeps

Example:

$ perl Makefile.PL
$ make installdeps

Note that this requires the Makefile.PL to use the 'auto_install' plugin.

For Dist::Zilla-based modules: dzil listdeps

Example:

$ dzil listdeps | cpanm
# or
$ cpan $(dzil listdeps)

This requires installing Dist::Zilla any any dist.ini dependencies first:

$ cpanm Dist::Zilla
$ dzil authordeps | cpanm

(Notice a pattern here?)

Anything with Makefile.PL or Build.PL: MYMETA and mymeta-requires

Install the latest ExtUtils::MakeMaker, Module::Build and App::mymeta_requires. Then configure as usual and run 'mymeta-requires' to get a list of dependencies.

Example:

$ perl Makefile.PL
$ mymeta-requires | cpanm
# or
$ cpan $(mymeta-requires)

A nice feature of 'mymeta-requires' is that it gives you access to all the different prerequisite types defined in the CPAN::Meta::Spec. So if a module offers 'develop' prerequisites, you can include those like this:

$ mymeta-requires --develop | cpanm

See the documentation for other ways to control which prerequisites are included in the output.

One drawback is that you'll still need to install any configuration prerequisites needed to run Makefile.PL or Build.PL, but that is true of all other recipes above (except that ones that use a CPAN client).

Update: as of version 0.002, 'mymeta-requires' will fallback to a META.json|yml file and also picks up configuration prerequisites. So now you can bootstrap completely like this:

# get configuration prerequisites
$ mymeta-requires | cpanm

# run configuration
$ perl Makefile.PL

# get any additional dynamic prerequisites from configuration
$ mymeta-requires | cpanm

Easy!

Posted in perl programming, toolchain | Tagged , , | 4 Comments

What are your top binary library dependencies?

On the #toolchain IRC channel, jawnsy and I were discussing how cool it would be if there were Alien::* packages for the top 10 or so binary libraries that people use with Perl. I.e. things like libssl, libxml2, libexpat, etc.

I don't know what it would take to make that happen, because getting Alien packages right across platforms is tricky, but a first step is figuring out what libraries people wind up needing the most.

If you install libraries/headers by hand, what do you wind up using most often? Please answer in the comments.

Posted in perl programming | Tagged , , | 18 Comments

Sorting photos by timestamp with Perl

My digital picture frame does a slideshow in filename order. I have hundreds of pictures taken over several years on different cameras with different file naming schemes. I've even rolled over the counter on one of the cameras, so I might have IMG1234.jpg twice (saved in different directories).

Here's how I used Perl to rename these pictures into the right order based on the timestamp metadata inside the JPEG files:

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use autodie;
use Digest::MD5 'md5_hex';
use Image::ExifTool 'ImageInfo';
use Path::Class;

for my $f ( dir()->children ) {
  next if $f->is_dir;
  my $exif = Image::ExifTool->new;
  $exif->ExtractInfo($f->stringify);
  my $date = $exif->GetValue('DateTimeOriginal', 'PrintConv');
  next unless defined $date;
  $date =~ tr[ :][T-];
  my $digest = md5_hex($f->slurp);
  $digest = substr($digest,0,7);
  my $new_name = "$date-$digest.jpg";
  unless ( $f->basename eq $new_name ) {
    rename $f => $new_name;
    say "renamed $f => $new_name";
  }
}

This program renames everything in the current directory into a format like this "2011-05-28T14-48-20-7fc2e71.jpg" -- which is an ISO8601 datetime (with "-" instead of ":") plus the first 7-hex-characters of an MD5 checksum. I use the checksum on the off-chance that two pictures were taken at the exact same second (e.g. from two cameras) or that one picture was a cropped/retouched version of an original. Anything that doesn't have EXIF datetime information is skipped and then I have to examine those manually to sort them into the rest.

It's not much, but it's quick and it worked.

Posted in perl programming | Tagged , , | 2 Comments

© 2009-2012 David Golden All Rights Reserved