fREWdiculous!
7 May
“I estimate that Rakudo starts up nearly 40% faster now than it did when I started on Sunday night. We can get it faster yet.” –chromatic
6 May
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!
6 May
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 |
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!
5 May
I’ve mentioned this in at least one previous post before, but it bears repeating.
First off, here’s some context for the varied information I am about to throw out at you, dear reader. I keep in touch with both the Ruby and Rails worlds because I think they have some really good ideas. Recently there was a conference session about CouchDB. I read the slides and I was impressed. CouchDB is cool stuff! The method of presenting the information was a little weird, but I didn’t think he went too far. Little did I know that he actually showed a lot more in the actual session. (read: actual porn.) It was a big deal and a lot of people have strong feelings about what went down.
Update: See Giles’ comment; I can’t hear sarcasm on the internet; mea culpa.
I’ve mentioned Giles Bowkett before. He’s an amazing coder and often he’s a good guy, but sometimes he just has some strange ideas. Or at least that’s how I feel. Here’s a highlight from the previous link:
DHH is a god. He’s not just another programmer, whose code we should analyze, learn from, or improve. He is our leader. If DHH likes REST, we all like REST. If DHH refuses to apologize for anything ever, we all refuse to apologize for anything ever. This is the Rails Way and we must honor it. In fact, I would go to say that if we see DHH refusing to apologize for being rude when he’s right, we should go a step further and refuse to apologize for being a dick when we’re wrong.
After all, if you criticize somebody important in the Rails world, you don’t get an apology from them. You get banned from conferences. You should know who’s important, and you should kiss their ass, no matter what kind of scum they might be. If you think things should be different, then you should fuck off and go write Python like Zed Shaw.
That’s an interesting thought. I can understand loyalty. In the past year I’ve developed a lot of loyalty to the Perl world, and even specific people. I would say that I have a similar amount of loyalty to Larry Wall. But I would also say that I always have reservations to my loyalty to people. People are never perfect and I will never blindly listen to what they say. Especially when it comes to non-technical issues like ethics.
Here are a couple points of view that certain women have come to (I found these on _why’s blog).
Audrey Eschright: Ruby (and Rails in particular) loves the rock star image. You see it in job posts, how people talk about their work, and the way Rubyists rant on their blogs. It’s macho, it can be offputting to both genders, and it makes it easy in this kind of situation to say, “what’s your problem? I’m just busy being awesome”. It’s also a significant barrier to adoption for people who aren’t already a part of this culture, and don’t find it appealing.
Victoria Wang: DHH’s attitude seems to say that the more we lower ourselves to the most base level of marketing scum in the name of entertainment, the better, even if at the end of the day there are no more women, or anyone worth knowing, in the room. It kind of makes me want to never touch Rails code again.
But the Rails world isn’t always this way, and I am confused that certain people have come to the conclusion they have. Remember Giles’ thoughts before, about following DHH to the end of the earth, or at least to sexism? Giles recently had a post about how he wanted rails to be a welcoming community to homosexuals. How can you be open to people of different sexual orientations, but not to people of a different sex?
But then of course they have people like raganwald. If I could magically become someone, it would be raganwald + _why. Brilliant, hilarious, and good. Raganwald posted a few days ago with some thoughts that I think apply to the situation going down right now. I highly recommend reading it. But if you can’t I’ll apply it for you: the Rails community is not bad. Their various coders are quite smart and often good, but these recent events are not ok, and defending what happened is even worse.
And then on the other hand we have the Perl world. We certainly aren’t perfect. I think that often Perl programmers are stuck in the past and not willing to change things for the better. I can feel that changing though…
Recently Ovid has been posting a lot about Roles and apparently there was recently quite the debacle on use.perl.org. I didn’t read it because reading comments on use.perl.org is just too painful. But what I am getting at is that things were said that hurt other people. But unlike the current Rails situation, Ovid apologized.
You don’t have to have a huge ego to be awesome.
4 May
Ok, you guys asked for it. I have updated WebCritic to be a lot leaner and meaner. Get the new version at the same great place.
It now runs entirely in it’s own lightweight server. No apache or mod_perl needed. It now uses CGI::Application::Server. It no longer uses CGI::Application::Dispatch, as my end goal no longer requires it. CAS fills the gap that CAD did for me. I also removed all of the lines that needed perl 5.10 because I wasn’t actually using any perl 5.10 features. If I ever do use perl 5.10 features though I will leave it as an exercise to the user to backport the code. Because it’s a user run server I no longer run a separate critic server. This makes the whole system a lot easier to run as a user. It defaults to criticizing the directory that it is run from and port 5053.
I plan on making a real page for it soon and putting it on CPAN in less than a month. Enjoy!
30 Apr
We should have all heard of Moose by now as a great way to do OO with Perl. While I was working on WebCritic I decided that it would be a good idea to hook my OO stuff up Moose style. I figure that even if I were to just write code and then disappear I might as well write 2009 code instead of 1999 code so that if it ever gets copied it will bless the copier instead of curse them.
One reason that Moose is great is that it makes things that you do all the time very succinct and obvious. For example, before:
1 2 3 4 5 6 7 8 |
after:
1 2 3 4 5 | has directory => ( is => 'rw', isa => 'Str', required => 1, ); |
That’s not a lot shorter, but it is more clear. Plus we get some extra error checking for free.
There’s also some cool options that allow you to write cleaner code. Before:
1 2 3 4 5 6 7 8 9 10 11 12 |
After:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
The boilerplate in the first version goes away in the new version. Much nicer.
And lastly, check this out, before:
1 2 3 4 5 6 7 8 9 10 11 |
after:
1 | # nothing! |
That’s right! All of that goes away and gets created automatically by Moose. The error checking is done automatically. Instantiation is done automatically. And heck, it will even work with a hash or hashref as the instatiation data!
29 Apr
When I was writing WebCritic I decided that the code was small and simple enough that it would be a great candidate for me to figure out how to set up automated testing for the whole stack (except for the javascript.) This is something that I’ve wanted to do at work for a long time but I feel bad spending time figuring out stuff like this on the customer’s dollar. I already had Perl Testing: A Developer’s Notebook and I figured I’d use it to get a start. Very helpful!
Perl has some great testing modules out there, but the problem is finding which ones. We all know about Test::Simple, Test::More, and Test::Most. There are tons more though that can really help with testing. The book helped me find a lot of them.
But anyway, I decided that before I release my code, because I was doing some OO work, I might as well use Moose to do the OO parts (more on that later.) When I was doing the porting it was very helpful to have the tests there to tell me if I had done things correctly. This is one of the main reasons I have heard people use for creating a test suite. It is not necessarily that your code will magically be better, but that you can change code with abandon and know if you introduce new bugs. Of course this implies good test coverage, but I am not really at that point yet. I am just to the point where I have decided that testing is awesome!
28 Apr
I like to continually move towards perfection in my code. perlcritic is a tool based on the book Perl Best Practices by Damian Conway. It’s basically lint for perl.
perlcritic is fine as it is if you spend all day on the console, but I usually spend my whole day in Firefox and vim. The only use for my console is checking in source and using irssi. There are a few other things I use the console for, but the point remains, I spend more time in Firefox than I do in the shell. The same is true of my coworkers: we are a windows shop; without installing cygwin using the console is fairly painful. And if it’s a hassle for me there is no way I can convince my coworkers to use it.
So I developed WebCritic. At some point I’ll make a real website for it, but for now this will have to work. I have other things I want to work on more, but I am using this daily at work so I presume that people might use it
. Anyway, it depends on:
1 2 3 4 5 | IO::All Perl::Critic CGI::Application CGI::Application::Dispatch Moose |
After checking it out from github you’ll want to start the critic server. It’s just a tiny server that keeps criticisms around for files that haven’t changed. With a fairly small codebase that took the load time down by a factor of 10. It also means that you don’t have to give the world access to your code. Just have the user owning the code run the server and your real webserver will talk with the mini server.
Now as to how to set up the real webserver…
I’ve been using Apache for the whole thing, so it’s been fairly simple getting it all going. If you want help getting it going with IIS I can help. Just leave a comment. Anyway, the config that I use for apache is in the devdocs dir in the repo. Just Include it in your main apache conf file. In ubuntu you only have to make a symlink to it in /etc/apache/sites-enabled.
You also need to start my mini server. I haven’t set up any script to autostart it as I prefer to start it as a user. Basically all you need to do is cd into the repo dir and run
1 | perl bin/server.pl <directory-to-monitor> |
The first time that it checks through the code it will take a while (mine takes 15 seconds,) but after that it should be pretty quick.
After getting all this going you should just have to go to http://localhost:5000/critic and you’ll get a nice listing of all of the criticisms from perlcritic. You can put a .perlcriticrc file in the dir that the server monitors to customize perlcritic. Note that all the columns are sortable and there are more columns that are hidden by default. Most importantly, the javascript will automatically query the webserver for data every 30 seconds, but if you want to know now press Alt+R while the page has focus and the page should reload the data (but not the page itself!)
Enjoy!
23 Apr
Ok that article is really long. Here’s the breakdown. Basically Matthew S Trout, the main guy who makes DBIx::Class, made a similar observation as other people. More specifically, Perl people don’t blog enough. With that in mind Mr. Trout gives some compelling reasons to start! So read the article.
Also: lots of things are coming out lately! Ubuntu 9.04 just got pushed to my laptop, and my phone is gonna get updated from 1.1 to 1.5 (read: on screen keyboard!) in less than two weeks! Woo! WOO!
9 Feb
I watched this keynote from frozen perl this weekend and it was pretty great. There are plenty things to take from this presentation, but the thing I want to mention comes from slides 66-77. Consider that mandatory reading to understand this blog post.
Now read this, this, and almost any of these.
Caveat Lector: All of those links may be outliers. I am certainly not reading a statistically valid sample of The Webternet; so maybe just consider this some random observations from this random dude.
Also: All of the above linked people are smarter and more motivated than I am. I would not criticize their intelligence in the least and their technical skills has my respect for sure. What have I done? A job? Helped move some books to people that needed them at school? Some stuff at a hospital? Compared to Rails, Mongrel, and Archeo- whatever. Compared to those guys I am nothing. Keep that in mind.
I just think it is important that in the perl world we have keynotes where people say: “Be Excellent to Each Other.” People say that perl is dead. I disagree. You should see how many blog posts there are out there saying, “no, perl is not dead!” But I would much rather perl be dead than perl be a bunch of jerks.
Basically this boils down to the difference between me and those other programmers. I think that our priorities should be:
Even if you aren’t a Christian I still think that exactly that priority list matters: Code is useless without users. Maybe they think they are exceptions because they are the users. Fine. I just know that if I see Giles, Zed, or DHH in real life I will be like, “Wow, what a great coder! Probably don’t want to actually hang out with them for more than 10 minutes though…”
Maybe I’m wrong though. Maybe the internet is just not a place for being nice. I just know that I am going to try to do my best to “Be Excellent to Each Other.”