New to Me Archives

A generator object for Perl 5

I have recently started a new job and it has forced me to learn more Python than I have ever had need to learn. I decided that I should take this as an opportunity to learn, and as Miyagawa-san has often done, steal when possible.

One thing that fascinated me is Python’s yield or generator pattern. In this pattern, you can make a function (or my case an object) which implements a lazy iterator returning a value (or possibly values (see below)) without leaving the while loop that generates them.

Type::Tiny rescues Moo

I have just ranted about removing old bad code from the Perl core. Let me lighten the mood by talking about some good, new code.

I have loved Moose for some time now, but like others, I disliked how heavy it was. Then Moo came along and it was great … until I found myself not availing myself of the type system, because it was harder to get to than in Moose. I was skipping validation more and more.

A recent project came up and I really wanted to do it right, so I tried Toby Inkster’s new Type::Tiny and may I say, “hat’s off to you sir!” The combination of Moo and Type::Tiny brings that Moosey feeling back, while still being light and responsive and even fat-pack-able! Great work to all involved in both projects!

Have you seen perl-reversion?

I just wanted to share a “new to me” script in case its new to any of you too. After a quick twitter exchange between me and @rjbs he told me about the script perl-reversion which is a part of the module Perl::Version.

Just run

perl-reversion -bump

in your distribution folder and it will find and bump the version numbers in all the files! What a time saver when developing! Thanks RJBS for sharing this with me and thanks Andy Armstrong for releasing it!

Edit: A previous version of this post decried the fact that the script was not installed but rather was in the examples folder. After reading further I see that it IS in fact installed by using some Makefile.PL trickery. Why not just put the script in a directory named bin rather than examples and it will be installed without trickery?

A Little Light(?) Reading

Courtesy of the Hacker News Twitter feed, I read a really interesting piece from slate.

Where's _why? by Annie Lowrey

Ostensibly, the article is about the former Ruby developer, enthusiast, mascot and resident oddball named "_why". He dramatically disappeared from the open-source world dramatically one day, taking all of his software and writings with him. But that's not the only theme of t…

Mojolicious + Bootstrap = Awesome

I have some news coming soon about Alien::Base but to avoid burnout, I’ve spent some time in the last few days playing with some things that are new to me. I enjoy doing this any time I’ve spent too much time on one project.

While I have spent some time using Mojolicious it has always been to hack together a quick UI for some code, rather than pulling out Tk. I never have really taken the time for pretty-fication, nor for any kind of interface logic.

I have a friend who thinks highly of my programming abilities and has recommended me to another of his friends to put together a website for a startup company. While I could put together a Joomla or Drupal site in no time, I thought I would investigate a Perl solution. I know that there are a few Perl CMSes out there (WebGUI 8 sounds interesting), but I wondered if I could hack something together myself.

Here’s the problem, I know Perl, thats about it. I don’t know much JavaScript, CSS or even HTML for that matter. Forget about DOM, and I’m no graphic designer.

However, surprisingly, Mojolicious, along with Twitter’s Bootstrap for building page elements, has made this really easy. I don’t know if I can sell it to a client yet. But maybe by the next client, or the one after that, I can offer a website as a Perl/Mojolicious/Bootstrap/PSGI app!

I love open source!

Method::Signatures : Some relief for MooseX::Declare users

I have been excited about OO programming in Perl thanks to MooseX::Declare but I have never especially liked its performance hit and its cryptic warnings. It turns out that much of this problem is due to MooseX::Method::Signatures, which is used under the hood.

Many moons ago, I was curious about Moose and MooseX::Declare and I posted a question on StackOverflow. Venerable Perl guy Schwern then posted as a comment, that Method::Signatures was better than MooseX::Method::Signatures, and that there was a mod in the works to use it with MX::D.

Recently I have been working on a side-project (non-sciency) which lent itself to an OO design and so I chose MX::D as I often do, but the warning messages I was getting were killing me! I remembered the SO exchange and decided to look for that mod. Turns out it isn’t so much a mod(ification, read: patch) as an extra module, Method::Signatues::Modifiers, which overrides MX::M::S with its own magic when loaded. So I decided to give it a whirl. Here is a test:

use MooseX::Declare;
#use Method::Signatures::Modifiers;

class MyTest {

  method do_something ( Ref $thingy ) {
    print "Hello World";
  }

}

my $obj = MyTest->new();
$obj->do_something( 1 ); # called with incorrect type

Without M::S::M (commented out) the error you get is the totally unreadable:

Validation failed for 'Tuple[Tuple[Object,Ref],Dict[]]' with value "[ [ MyTest=HASH(0x20fa7d0), 1 ], {  } ], Internal Validation Error is: \n [+] Validation failed for 'Tuple[Object,Ref]' with value "[ MyTest{  }, 1 ]"\n  [+] Validation failed for 'Ref' with value 1" at /home/joel/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/MooseX/Method/Signatures/Meta/Method.pm line 435
MooseX::Method::Signatures::Meta::Method::validate('MooseX::Method::Signatures::Meta::Method=HASH(0x20faa28)', 'ARRAY(0x20dce30)') called at /home/joel/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/MooseX/Method/Signatures/Meta/Method.pm line 151
  MyTest::do_something('MyTest=HASH(0x20fa7d0)', 1) called at test.pl line 13

however WITH M::S::M (not commented) you get the far more pleasant and useful:

In call to MyTest::do_something(), the 'thingy' parameter ("1") is not of type Ref at test.pl line 13.

In the documentation, the authors (including Schwern, and Barefoot) note that M::S::M is not perfectly a drop-in replacement for MX::M::S, however the differences they note, I almost never use. So be aware, but don’t let that stop you from switching, you’ll be glad you did.

I haven’t done any compile timing tests, but Schwern seemed convinced that this would be improved too. Doesn’t matter, having useful error messages back is well worth the extra dependency and keystrokes.

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.