I rewrote my blog software in Perl

In the last few weeks I have rewritten my blog software in Perl. The old version used Ruby. I wrote that version, because I wanted to try out Ruby. The software is about as old as the blog itself. The new software is written Perl. I use many new modules in Perl. For example Moo, Path::Tiny and Carton. Moo is an OO library for Perl. It allows me to write OO code in Perl without any boilerplate....

September 2, 2013

Simple queue in Redis with Perl

Sometimes you need to have a asynchronous worker written in Perl. The small script here takes a job from the queue and executes the code. It only takes a few lines of Perl. use Redis; my $redis = Redis->new(encoding => undef); my $queue_name = 'q'; my $timeout = 10; for (;;) { my ($queue, $job_id) = $redis->blpop(join(':', $queue_name, 'queue'), $timeout); if ($job_id) { my %data = $redis->hgetall($job_id); # do something with data....

September 2, 2013

Dist Milla and Pinto

Today I was working on a private module that I use to write web applications. A few weeks ago I found out about Milla. Milla is a plugin bundle for Dist::Zilla, which makes it easy to create Perl modules. When you’re ready Milla automatically releases your new module to PAUSE. This is really useful for public modules, but not that useful for private modules. Pinto is like CPAN in a way....

July 23, 2013

Pretty colors for charts using Chart

Monday I was creating a graph for my webshop software. I use the Chart module, but the colors and the graph of the default settings aren’t really pretty. So I tried to create a better looking chart. I think I succeeded at least a bit. Here is the code that I used to get this result: my $c = Chart::Composite->new(1024, 250); $c->set(composite_info => [ [ 'Lines', [1] ], [ 'Points', [2] ], ]); $c->set(precision => 0); $c->set(skip_x_ticks => 7); $c->set(max_y_ticks => 11); $c->set(min_y_ticks => 2); $c->set(legend => 'none'); $c->set(grey_background => 'false'); $c->set(y_grid_lines => 'true'); $c->set(colors => { 'background' => [255,255,255], 'misc' => [200,200,200], 'x_grid_lines' => [220,220,220], 'y_grid_lines' => [220,220,220], 'dataset0'=>[150,180,250], 'dataset1'=>[120,140,240], }); my $label = 'x'; my $val = 10; $c->add_pt($label, $val, $val); You should call add_pt for each point you want to show....

August 25, 2011

Log parsing speed

Now that we have two programs that parse log files, we can start to take a look at how many lines the program parses per second. First we have to make the two programs as similar as possible. In pseudocode it looks likes this. Load all modules Take the start time using Time::HiRes Put the code of the program here Set line_count = 0 Using stdin: loop through all lines Parse the line Set line_count++ Find the time difference Divide and line_count / time as n lines/s In Perl this looks like:...

April 13, 2011

Write your own Accesslog parser in Perl

Writing an apache access log parser isn’t that hard. Below is a parser that does just that. It creates Data::Dumper output of all the lines. No warranty. use Data::Dumper; use Parse::RecDescent; $Parse::RecDescent::skip = ''; my $grammar = q{ line: ip ws '-' ws user ws datetime ws request ws status ws responsesize ws referrer ws useragent "\n" { $return = { ip => $item[1], user => $item[5], datetime => $item[7], method => $item[9]->{method}, url => $item[9]->{url}, protocol => $item[9]->{protocol}, status => $item[11], size => $item[13], referrer => $item[15], useragent => $item[17], } } user: '-' | /\w+/ request: '"' method ws url ws protocol '"' { $return = { method => $item[2], url => $item[4], protocol => $item[6] } } datetime: '[' date ':' time ws timezone ']' { $return = $item[2] ....

April 12, 2011

Apache accesslog reporting tools

Today I tried to create a report of some basic statistics about Abacus downloads. Normally I would use grep, awk and a few other commandline tools to find a rough estimate of these numbers. However this time I needed a bit more information than these tools could give me. A problem in need of a solution. My first question was: how many people have downloaded Abacus? The answer is grep '/abacus/files/Abacus' | grep -v '<localip>' \ | grep -v 'somebots' | awk '{print $1}' | sort | uniq | wc -l The pattern here is the following....

April 12, 2011

Create sub directories in Perl

Sometimes you need to create a large tree of subdirectories. But why? Two examples that I think of are structured directories for weblogs, e.g. /[year]/[month]/[day]/, or the automatic backing up of files e.g. invoices/[company]/[year]. Before you begin, you know that using a split and chdir, or some other combination of built-ins, will just make big mess. A call to mkdir -p could also work, but let’s use the available modules this time....

April 7, 2011

Middleware in Plack

I’ve been using Plack for a few days now, and I have to say, I really like it. I rewrote the base of my webshop platform to use it. The change wasn’t really difficult and the code is a lot cleaner now. Especially with the Middleware structure I was able to remove a lot of code from the main application. Middleware classes are classes that are called between the webserver and the main application....

September 10, 2010

Perl 5.12 has been released

The latest version of Perl, version 5.12 has been released. There are many changes and bugfixes and I would recommend you check the release notes for the whole list. Implicit strictures when you use an version number greater than 5.12.0 The yada, yada operator Y2038 compliance New package naming for including the version Updated to the latest Unicode standard

April 13, 2010