A Foolish Manifesto

fREWdiculous!

Archive for May 6th, 2009

Testing with Perl: awesome

Sometimes when I get close to the end of the day and it isn’t feasible for me to start on something new I expand on my current project’s test suite. Recently I worked on one of the (seemingly) more complex ones. Basically it tests one of our autocompleters to ensure that it will search for the name and also the public facing id of a certain field. The id part was easy.

The name part was significantly more complex, but not too bad really:

1
2
3
4
5
6
7
8
9
10
11
my @data = $schema->resultset('Customer')->
   autocomplete_search({
      query => 'ame'
   })->all;
cmp_deeply @data, all(
   array_each(
      methods(
         name => re(qr/.*ame.*/i)
      )
   )
), "Name matches query";

So basically what that does is ensure that all of the items in @data have a method that match the regex listed. It doesn’t care how many items are returned or any of the other details. Elegance!

  • 0 Comments
  • Filed under: perl
  • Future Perl

    This is mostly stuff I’ve gathered from this talk and updated slightly.

    First off, have you ever tried to teach a programmer perl? I have. Note this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    sub foo {
       my ($self, $bar,$baz) = @_;
       #...
    }

    # or often
    sub station {
       my $self = shift;
       #...
    }

    The following is more palatable to most coders (also I like it better:)

    1
    2
    3
    4
    5
    6
    7
    8
    use <a href="http://search.cpan.org/search%3fmodule=Method::Signatures::Simple">Method::Signatures::Simple</a>;
    method foo($bar, $baz) {
       #...
    }

    method station {
       #...
    }

    That’s right, no fiddling with @_ at all. Sweet! Also note: this is not implemented with sketchy source filters. It’s quite robust. There is also MooseX::Method::Signatures (and MooseX::Declare which uses that) which can do even more, like defining named and optional params and type constraints.

    I’ve already mentioned IO::All, so I’ll just say that it’s been endorsed by Matthew S. Trout, so you don’t have to tell me that the Right Way is to use some crufty C based interface or some weird old perl module that has capital letters in the functions and method parameters.

    Next up, Moose::Autobox. I’ve also mentioned autobox, which this module uses, but Moose::Autobox goes further and defines some roles that your classes can also use to make them act like arrays etc. I wouldn’t be surprised if DBIx::Class 0.09x used some of these roles. Anyway, here’s a real example:

    1
    2
    3
    4
    5
    6
    7
    8
    use Method::Signature::Simple;
    use Moose::Autobox;

    method criticisms {
       return {
          data  => $self->files_criticized->values->map(sub { @{ $_->{criticisms} } })
       };
    }

    Not quite Perl 6/Ruby, but still much clearer than the original (at least to this function programmer!)

    And the of course there is the recently released (four months ago!) TryCatch. I haven’t used this module at all, but I look forward to it. It could really streamline some of the stuff we do at work.

    Lastly, CLASS is a tiny nicety. All it does is replace __PACKAGE__ with CLASS; the code looks cleaner with it and it’s certainly shorter and easier to type.

    I think we’ll be seeing a lot more of this kind of stuff in the near future. Devel::Declare seems to have mostly matured, so these modules will probably continue to crop up.

    Anyway, woohoo! The future!

  • 1 Comment
  • Filed under: perl
  • Flight of the Conchords live

    I’ve decided that seeing a concert warrants a blog post, and that a concert blog post can fill in for an Album of the Week post. If I pay the money to see a concert (typically more than the cost of a CD) I probably like the band that much :-) With that in mind I present to you my most recent concert: The Flight of the Conchords.

    You’ve probably already heard of Flight of the Conchords. It was the biggest single concert I’ve ever been to by far. (The Flaming Lips was actually a festival so that doesn’t count.) According to the band there were somewhere between four thousand and six thousand people there. Wow. Most concerts I go to have somewhere between one hundred and five hundred people. FLOTC is a comedy band that has a show on HBO. Typically I prefer to see their live stuff on Youtube though, so a concert seemed perfect.

    The tickets weren’t very expensive. Thirty five + ticketsuckster fee totaled to something like forty seven dollars. We actually arrived a little late so we hardly saw any of the opener, so I won’t comment on him at all. The transition from the opener to band was instant, which is excellent. The band came out in robot costumes and performed “Too Many Dicks on the Dance Floor.” They changed into normal clothes while the lights were off and after that it was basically songs and between song chit-chat. The music was great and the talking between was also very good.

    My only major complaint was the audience. I go to plenty of concerts, probably at least one, if not two, per month. The people at this concert just did not know how to behave! First off, it seemed that a large portion of the crowd felt obliged to get up and get a beer every 15-20 minutes, so of course we are standing up to let them pass or the people in front of us are or even the people behind us are knocking our heads with their largely shaped behinds as they squeeze out to the aisle to get one more carb packed alcoholic beverage. And then of course there was the people that had to yell to their bro five seats down that they really liked the current song. At first I thought I was just being uptight, but my two compatriots agreed that it was extremely obnoxious.

    Anyway, I give the concert a four out of five rating. Everything was great except for the use of a certain ticket provider and certain audience members.

  • 1 Comment
  • Filed under: music