Hi! Please consider following me on twitter: @hanekomu.

Debugging principle: Look under the lamp post

In a blog post, Mark-Jason Dominus talks about how a good place to start debugging is to look under the lamp post. The idea behind that principle is: If you lose your wallet in a dark street start by looking under the lamp post. The wallet might not be there, but you will not waste much time on the search.

For example, if your program accumulates results in an $output variable and something goes wrong, start your search by finding all occurrences of that variable in your code. Don't make elaborate assumptions about who does what in the code. This may sound obvious but sometimes we don't follow that advice. So start looking under the lamp post.

Write a comment | Bookmark and Share

posted at: 15:25 | path: /misc | permalink | 0 comments | 0 trackbacks

World clock in Perl, functional programming approach

I talk to Perl mongers in different time zones, so I wrote a small program to tell me what time it is in those time zones. Lately I find myself writing programs in a more functional programming-oriented style, that is, avoiding intermediate variables and using grep() and map() a lot. Since Perl is a procedural language at heart, this is possible only to a certain extent. The world clock program is simple enough so I could write it in a purely functional style:

#!/usr/bin/env perl

use warnings;
use strict;
use DateTime;
use Text::Table;

print Text::Table->new(qw/Timezone Time/)->load(
    map { [ $_->time_zone->name, $_->strftime("%F %T %Z") ] }
    map { DateTime->now->set_time_zone($_) }
    qw{
        Europe/London
        Europe/Vienna
        America/Los_Angeles
        Asia/Tokyo
    }
);

The result looks like this:

Europe/London       2008-05-31 10:02:09 BST 
Europe/Vienna       2008-05-31 11:02:09 CEST
America/Los_Angeles 2008-05-31 02:02:09 PDT 
Asia/Tokyo          2008-05-31 18:02:09 JST 

Write a comment | Bookmark and Share

posted at: 10:38 | path: /dev | permalink | 0 comments | 0 trackbacks

SQL-style Perl

Some syntacting sugar to make grep() and map() look more like SQL:

#!/usr/bin/env perl
use warnings;
use strict;

my @students = (
    { id => 1234, marks => 20 },
    { id => 4711, marks => 85 },
    { id => 3000, marks => 86 },
    { id => 9999, marks => 99 },
);

sub SELECT (&@) { my $code = shift; map  { $code->() } @_ }
sub WHERE  (&@) { my $code = shift; grep { $code->() } @_ }
sub FROM   (@)  { @_ }

sub id    () { $_->{id}    }
sub marks () { $_->{marks} }

my @cleverlies = SELECT { id } WHERE { marks > 85 } FROM @students;

Write a comment | Bookmark and Share

posted at: 18:59 | path: /dev | permalink | 0 comments | 0 trackbacks

First Moosifications

After hearing nothingmuch's Moose talk at YAPC::Asia I've now switched two of my CPAN distributions to Moose: Games::Go::Coordinate and Games::Go::Rank

Mooooooooooooooose!

Tags: .

Write a comment | Bookmark and Share

posted at: 20:48 | path: /dev | permalink | 0 comments | 0 trackbacks

Hello, world!

Irasshaimase!

Following schwern's "Perl is unDead" talk at YAPC::Asia in which he talked, among other things, about increasing Perl's visibility to search engines, I've started this new blog about Perl topics. There's not much to see yet and I'm not a prolific blogger, but hopefully the site will grow...

The blog is rather basic in design; this is intentional. I also promise not to have 900kB of JavaScript on each page.

Write a comment | Bookmark and Share

posted at: 20:19 | path: /misc | permalink | 0 comments | 0 trackbacks

use 箆; or: Distributions with Kanji on CPAN

During the YAPC::Asia hackathon day 2, we discovered a kanji that we could use for Moose.pm: 箆. It even looks a bit like some thing with antlers. Here is the JEDict definition:

箆 [へら: HERA] spatula
箆鹿 [へらじか: HERAJIKA] (uk) moose, elk

So a moose is an animal with a spatula on its head? Larry said that 箆 could also mean "comb", so a moose is an animal with a comb on its head. But I digress.

I've made a simple proof-of-concept distribution. The main module's code basically is:

use utf8;
package 箆;
1;

perl Makefile.PL, make, make test and make dist all worked well. It produced a tarball: 箆.tar.gz. So far, so good.

Then I've tried to upload the tarball to cpan:

$ cpan-upload-http 箆.tar.gz

That also gave no error message. So I waited for the emails from PAUSE. Here is the first one:

> Subject: Notification from PAUSE
>
> MARCEL (Marcel Grünauer == hanekomu (跳ね込む)) visited the PAUSE and
> requested an upload into his/her directory. The request used the
> following parameters:
>
> pause99_add_uri_upload [箆-0.01.tar.gz]
> SUBMIT_pause99_add_uri_httpupload [ Upload this file from my disk ]
> pause99_add_uri_httpupload [-0.01.tar.gz]
>
> The request is now entered into the database where the PAUSE daemon will
> pick it up as soon as possible (usually 1-2 minutes).
>
> During upload you can watch the logfile in
> https://pause.perl.org/pause/authenquery?ACTION=tail_logfile&pause99_tail_logfil e_1=5000.
>
> You'll be notified as soon as the upload has succeeded, and if the
> uploaded package contains modules, you'll get another notification from
> the indexer a little later (usually within 1 hour).
>
>
> Thanks for your contribution,
> --
> The PAUSE

Due to the good fortune of having kanji and kana in my CPAN display name (跳ね込む), I was sure that the email had the right encoding. But the tarball filename it returned was wrong: "箆-0.01.tar.gz". Well, let's see. Maybe it's just a problem with generating the email. Wait and see... Here is the second email:

> Subject: CPAN Upload: M/MA/MARCEL/-0.01.tar.gz
>
> The uploaded file
>
> -0.01.tar.gz
>
> has entered CPAN as
>
> file: $CPAN/authors/id/M/MA/MARCEL/-0.01.tar.gz
> size: 24392 bytes
> md5: 39ebd0b5ab4bd9acdb2a80c1c4a733dd
>
> No action is required on your part
> Request entered by: MARCEL (Marcel Grünauer == hanekomu (跳ね込む)))
> Request entered on: Sun, 18 May 2008 11:00:42 GMT
> Request completed: Sun, 18 May 2008 11:00:52 GMT
>
> Thanks,
> --
> paused, v996

Hm, doesn't look good either. Well, wait for the indexer report... Here it is:

> Subject: Failed: PAUSE indexer report MARCEL/-0.01.tar.gz
>
> The following report has been written by the PAUSE namespace indexer.
> Please contact modules@perl.org if there are any open questions.
> Id: mldistwatch 1001 2008-05-15 05:34:01Z k
>
> User: MARCEL (Marcel Gruenauer == hanekomu)
> Distribution file: -0.01.tar.gz
> Number of files: 21
> *.pm files: 14
> README: 箆-0.01/README
> META.yml: 箆-0.01/META.yml
> Timestamp of file: Sun May 18 11:00:51 2008 UTC
> Time of this run: Sun May 18 11:02:23 2008 UTC
>
> No package statements could be
> found in the distro (maybe a script or
> documentation distribution?)
>
> __END__

Ah, so it could actually untar it because it found the README and META.yml files. But it seems it uses the wrong regex to find the package statements. Maybe it needs to read the file as utf8...

Just to be sure it wasn't a problem with cpan-upload-http, I also uploaded the file from the PAUSE web interface directly. Here is the response:

> The Perl Authors Upload Server
>
> Upload a file to CPAN
> Add a file for MARCEL
> File successfully copied to '/home/ftp/incoming/-0.01.tar.gz'
> Your filename has been altered as it contained characters besides the class [A-Za-z0-9_\-\.\@\+].
> DEBUG: your filename['箆-0.01.tar.gz'] corrected filename['-0.01.tar.gz'].

So this doesn't work either.

The distribution actually does show up on my search.cpan.org page as -0.01. So it might be installable if you download manually, but it won't be installable via the CPAN shell. And I guess I've now 0wned the root namespace on CPAN...

Dear CPAN maintainers, please fix it.

Write a comment | Bookmark and Share

posted at: 15:16 | path: /dev | permalink | 0 comments | 0 trackbacks