fREWdiculous!
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!
6 Responses for "PerlCritic for Web Developers"
My thoughts:
- Will it be CPANized?
- Any chance to slim down the architecture and
simply have a Catalyst controller instead of
yet another tiny server to take care of?
If one wants a standalone server, then the
catalystic one could be used.
CGI::Application and not HTTP::Engine? Why?
This sounds very similar to perlcritic.com. Have you had a look at that yet?
You should probably mention somewhere that perl 5.10 is required for this. I just tried running this, and my poor old 5.8.8 installation didn’t get past the `use feature ’5.10′` line.
Thanks for the write-up. I’m just starting with Moose and I like real code like this to help me wrap my head around it. Just one question, Why did you set reader and writer attributes for directory? Was that just a personal preference over using the default?
@Robert: Hopefully this will be put on CPAN yes, but I figured that it would be better to get it out there first. I don’t know when though. Also, Catalyst is way too big, as far as I can tell, to use for this. If you want a standalone server CGI::Application::Server will do the trick. I may look into that at some point.
@Jonathan: I use CGI::Application at $work and I used it at school. It works really well and it’s very lightweight.
@j1n3l0: not really though; perlcritic.com requires you to actively submit your source. This runs locally and monitors all files matching a certain pattern (.pm, .pl, .t, .plx) and gives criticisms on all of them. The idea here is to make it ridiculously easy to get criticisms on your source once everything is set up.
@beppu: that’s probably not even required. I just type the use feature line whenever I make a new perl file. I don’t think this uses any of 5.10. Comment out those lines and see if it still works.
@derby: Damian Conway recommends using set/get methods in Perl Best Practices (page 340.) I will make another post on just Moose later on so stay tuned
Leave a reply