2015-05-09

Your Moosey Fate

"Are those walnuts?"
"Yes." — All of Dib's fears are confirmed, Invader Zim

If you write your daily scraper scripts in Moose, it's actually pretty easy to turn them into a singular Moosey wrapper script and just change the attributes as needed:

package MyThing;
  use Moose;

  has 'url' => (
    is => 'ro',
    isa => 'Str',
    default => sub { 'http://example.com' }
  );
  has 'string' => (
    is => 'ro',
    isa => 'Str',
    default => sub { 'magic string' }
  );

  sub do_something { ... }

  sub main {
    my ($self) = @_;
    $self->do_something;
    return 0;
  }

__PACKAGE__->meta->make_immutable;
1;

exit(MyThing->new->main);
die('NOT REACHED');

becomes

use MyThingWrapper;

my $mything = MyThingWrapper->new(
  url => 'http://example.com',
  string => 'magic string',
);

$mything->do_something;
exit(0);
die('NOT REACHED');

Lack of error-checking aside, you can templatize your script into a .pm Perl module and only keep one copy of it rather than having a dozen scripts that only have a few lines of difference between them. Put the module into source control and keep it someplace where every instance can get to it and you'll be happy as a clam.

It's super simple stuff, but I find myself happier to have done this kind of basic conversion a few times and saved myself a lot of fragmentation when I have to edit them. Now I just have to edit and test one thing.

No comments: