State of Dancer on Perl 6

Bailador is growing better and bigger and starts to resemble a real tool more and more. Let’s see what new features it has gained recently.

Remember the first example in perldoc Dancer? It’s not really different in Bailador:

use Bailador;
get '/hello/:name' => sub ($name) {
    return "Why, hello there $name"
}
baile;

Aside of being a little Spanished, what did it give us? We have subroutine signatures in Perl 6 so we can pass the :name parameter to the sub; there’s no need to use param() now: it’s gone.

You don’t need to pass everything using GET of course. post keyword is also supported.

post '/' => sub {
    return request.params.perl
}

The above will print something like ("foo" => "bar").hash, if fed with appropriate request.

any() is a reserved keyword in Perl 6, and while you can use it, it means a completely different thing. Instead of any('get', 'post') you can just do it like this:

get post '/' => sub {
    if request.is_get {
        return "I am GET"
    } else {
        return request.params.perl
    }
}

post, as well as get return their arguments, so you can chain them like in the example above. It also shows the joy of request object, which you can use to inspect the request being processed. It’s not as cool as Dancer::Request, but it does the job, being quite small and simple.

What else do we have? Let’s show off a bit and write a simple-simple pastebin webapp.

use Bailador;

unless 'data'.IO ~~ :d {
    mkdir 'data'
}

get '/' => sub {
    template 'index.tt'
}

post '/new_paste' => sub {
    my $t  = time;
    my $c = request.params<content>;
    unless $c {
        return "No empty pastes please";
    }
    my $fh = open "data/$t", :w;
    $fh.print: $c;
    $fh.close;
    return "New paste available at paste/$t";
}

get /paste\/(.+)/ => sub ($tag) {
    content_type 'text/plain';
    if "data/$tag".IO.f {
        return slurp "data/$tag"
    }
    status 404;
    return "Paste does not exist";
}

baile;

Holy cow, what’s that! Let’s go there piece by piece. First, we’ll create a data directory if it doesn’t already exist. No black magic here, let’s proceed. What’s next? Templates! Here we just load index.tt, not passing any parameters, but that works too and some example apps use that in their example templates.

The handler of new_paste uses our well-known request object again, and creates a new file for a paste, identified by the current time.

The last get block uses some nifty features, so let’s take a look. It uses regexes, and you can see that they also cooperate with subroutine parameters without black magic. We then set a content_type as we’ll do in Dancer, and send status 404 if no paste have been found. Easy peasy? I suppose so. That’s it, it works like a charm.

Thus we’ve covered all the features in Bailador as for now. I don’t think it’s that poor, as for about 100 lines of code.

What’s next? What’s missing? You tell me. Or you contribute; the code is dead simple and implementing stuff like before(), after(), before_template() etc should be a matter of 3-5 lines, I think. Feel encouraged to look into the code and hack on it. If you have any questions, suggestions or criticism, don’t hesitate to tell, or poke me on #perl @ Freenode. Have fun!


MuEvent: AnyEvent lookalike for Perl 6

Trying to struggle with Select in Parrot, I accidentally discovered that its Socket has a .poll method. What a trivial, yet satisfying way to have some simple non-blocking IO. Thus, MuEvent was born.

Why MuEvent? Well, in Perl 6, Mu can do much less than Any. MuEvent, as expected, can do much less than AnyEvent, but it’s trying to keep the interface similar.

You’re welcome to read the code, and criticise it all the way. Keep in mind that I can no idea how should I properly write an event loop, so bonus points if you tell me what could have been done better. I don’t expect MuEvent to be an ultimate solution for event-driven programming in Perl 6, but I hope it will encourage people to play around. Have an appropriate amount of fun!


How to use your Kindle as a notebook

So amazon has released those new, shiny Kindles for low prices. Does that make my Kindle Keyboard (that’s how they call it now anyway) obsolete now? Not on my watch. So what’s a good thing about a physical keyboard, on a device you don’t really type on? Turns out it can be useful if you can find a reasonable use for it.

I used to carry around a notebook of a size of a post-it sticker in my back pocket. I’m nowhere near the idea of carrying my Kindle in a back pocket, I’ve had it broken already, thank you, but can it be as good as a notebook as a good, ol’ piece of paper? First of all, we’ll need a decent app for it. But those are not available for customers outside of the US. Crap.

But there’s this feature for adding inline notes in a book you read, right? So if I could carry around an empty book just for putting notes on it… that could work. So I assembled one, and all the inline notes appear on the “My Clippings” entry just fine. Works fine now, let’s see how it will work in the field, over time.

So, what do you use a keyboard on your Kindle for?


What is Production Ready?

“When will Perl 6 be production ready?” – they ask from time to time. I know the feeling, there was a time I wanted to know too, and after a year working on Rakudo, I can truly say,

I have no freaking idea!

I’d really like to tell you, seriously. If you ask #perl6, they will start tricking you into thinking that it’s ready enough and they’re actually using it, right? Tricky bastards. But, what do you actually ask for? What is this mighty Production Ready?

I dedicated some thinking to this today. What makes something Production Ready? I can think of two possibilities

  1. The creators declare it Production Ready
  2. People start using it in Production Environment

The first one is a bit tricky to achieve when it comes to Perl 6. As we know, Perl 6 is a language. How can language be Production Ready? Think, think. Is there another example of something which is rather a spec than an end-user product, and is either not declared as finished, or the spec freeze date is ridiculously far in the future? Right, it’s HTML5. Spec is a draft, it’s nowhere near finished, and neither of the implementation implement all of it. So what makes HTML5 production-ready? I don’t think it’s declared ready by its creators. It’s that people didn’t bother with official opinions and started actually solving problems with it. Took the existing implementations and made use of it. Therefore, we can safely assume that by “Production Ready Perl 6” we really mean “A Perl 6 Compiler I can use to get the job done”. So what are the current compilers lacking for the majority of people?

Yes, I’m asking you. You don’t really know, do you? You didn’t even try them? It’s just that people don’t use them too often, so they’re probably crap, right? Ok, there’s some logic in that.

There is a possibility that Perl 6 is already capable of solving your problems. You should try it. But! Enough of the advertising business, I’m wondering here.

“So what is your Production Ready?”, you may ask. What do I expect from Perl 6 before it will be Production Ready for me? It’s not, I’m not gonna lie. It’s solving my problems, it pays my bills, but it lacks this Something that will make it Purely Awesome. In my opinion, there are two major things we’re missing:

  1. Speed. Not all things I write need to be blazingly fast, but what is the point of amazingly expressive language, if the bottleneck of the development process is program runtime?
  2. Access to Perl 5 modules from CPAN. Yes, I know of modules.perl6.org fairly well, believe me. Still, it will take ages, if not infinity to make it as awesome as CPAN is. Blizkost is a bridge between Perl 5 and Parrot and it’s capable of running simple stuff already.

That’s it. I can live without most of the things. But what I’m really looking for, is a better Perl 5. It needs CPAN, and it needs to be less slow that it is. I’m not looking for a C performance. I could live with Perl 5 performance here probably.

That’s what I’m missing. And what is Your Production Ready?


S26 in HTML

On http://perlcabal.org/syn/, Synopsis 26 is the only one without the HTML page. That’s of course due to the lack of Pod (Pod6) to HTML converter. Today there has been a breakthrough in this embarrasing situation :)
My Pod parser integrated into Rakudo is capable of parsing S26 completely, so this morning I wrote a Pod::To::HTML module for it. Parsing and generating HTML output from S26 takes about 4 minutes on my machine, but the outcoming document is not that bad. You can see some still NYI features, like the lack of formatting codes and correctly interpreting semantic blocks. The first one is a part of my GSoC work, the second one is not but I’ll probably do it anyway, just for the sake of having prettier S26 :)

My HTML-fu is a bit Less Than Awesome, so if anyone knows how to make it any better (and it won’t be cheating as Pod::To::HTML is not a part of my GSoC work), I’m willing to hand out commit bits to anyone interested.

Maybe it’s finally time to read this one. Have the appropriate amount of fun.


GSoC – The podparser branch has landed

Just a few minutes ago, with a total count of 28 files changed, 1627 insertions and 41 deletions, the podparser branch in which I’ve been doing my GSoC work has been merged into nom, the current development branch of Rakudo and the soon-to-be master. So, what do we get?

  • Pod is now parsed, not ignored. The whole parse tree is available in runtime in the $=POD variable
  • A basic support for documenting classes and subroutines with #= comments, and the .WHY method on each object returning the attached documentation
  • A –doc switch, a not-really-replacement for perldoc
  • A simple Pod::To::Text output generator
  • and more, of course :)
So, how does it look like in action?
$=POD
All the Pod we have written in our code is now available in the $=POD variable. Example?
=begin pod
Some documentation
=end pod

say $=POD[0].content[0].content;
Results in

Some documentation

That’s not very useful per se, so how about this one:

=begin pod

=head1 NAME

A basic pod document

=head2 Running Perl programs

To run a Perl program from the Unix command line:

    perl progname.pl

=head2 Things on my desk

There are the following things on my desk right now:

=item A cup of tea
=item A couple of pens
=item A stereo
=item A couple of books
=item A laptop, obviously

=end pod

DOC INIT {
    use Pod::To::Text;
    say pod2text($=POD);
    exit;
}

say "I'm just a simple program";

Now what’s the DOC INIT thing? Let’s see. How about we run the above program:

$ perl6 foo.pl
I'm just a simple program

No suprises. Let’s introduce the –doc switch then:

$ perl6 --doc foo.pl
NAME

A basic pod document

  Running Perl programs

To run a Perl program from the Unix command line:

    perl progname.pl

  Things on my desk

There are the following things on my desk right now:

 * A cup of tea
 * A couple of pens
 * A stereo
 * A couple of books
 * A laptop, obviously

That’s right. The DOC blocks are executed only when the –doc command line option has been given. At the moment you have to write them yourself, but maybe even in the nearest few days there will be a default DOC INIT block doing What You mean all by itself. There we go, a perldoc-alike built-in :)

WHY

That’s probably the biggest killer feature in the whole project. Although it’s not yet fully implemented in Rakudo (suprise segfaults here and there, don’t worry, they’re not permanent :)), it looks pretty much like this:

#= our zebra class
class Zebra {
...
}

say Zebra.WHY # prints: "our zebra class"

Yes yes, a documentation inspection in runtime. See the potential?

# what was that sub again?
&sort.WHY.say # get the documentation for the sort() builtin

That opens a way for lots of awesome userspace tools too.

So, what’s still not quite there?

  • .WHY (some cases)
  • Formatting Codes (C<code>, B<bold> etc)
  • DOC use (that can be written as DOC INIT { use … } for now)
  • Default DOC INIT (as mentioned above)
  • A Pod::Parser as a standalone module
With that done, my GSoC project will be finished, and I’ll hopefully have time to hit some things I was planning to poke in some spare time, for example:
  • HTML output generator
  • Extending the parser so Synopse 26 can be fully parsed
  • Other fancy userspace tools; I don’t want to spoil the suprise :)

What can I say? Pull it, compile it, play with it, report bugs and have the appopriate amount of fun :)


Pictures

I (and a dear friend of mine actually) took a plenty of nice pictures. I’m rarely satisfied with my photography, but that could make a nice, non-nerdy post. I was supposed to write them from time to time after all :)

They could use some cropping, but it’s late.

PS. WordPress seems to do some harm to my pictures, probably due to site width, so here are some links to photos above and more:


GSoC: First (easily) visible results

Yeah, of course I’m doing stuff for almost 2 months now. But all this was boooring. The goal was not for the parser outside the compiler, but the parser in the compiler itself, exposing Pod to runtime. After some funny and some frustrating moments (like self-duplicating bacon: worse than it sounds!) it finally happened: a $POD variable (should be $=POD one day), is visible in the code and contains sensemaking results. I also started moving tests to Rakudo tree, so ‘make test’ can be proud of the shiny new capabilites too. Exciting times. I feel excited and proud, one of the main goals is there, as it should be, not in some remote repo as a separate module. From now on, everything should be easier :)


The summary of the Community Bonding Period

Students get to know mentors, read documentation, get up to speed to begin working on their projects.

Or so says the GSoC timeline. That ends today, almost one month since the accepted students were announced.  I didn’t do much community bonding per se. I’ve been quite active in the Perl 6 community for almost a year now. I already knew Moritz and Carl (the latter one even in person), my mentors, so that part can be ticked as “done” as well. So what did I do, instead of  lying in the shade and drinking a fine tea? The Read Documentation part, but not only. I’ve done lots of discussions with my mentors, and received an excellent feedback. Almost every discussion I was afraid I may forgot about was scrupulously logged in the gsocmess repo. But not only the irc logs got there. I started scratching out how the Pod nodes of the example Pod documents will look like. I used the parts of S26, as they’re probably the best examples of Pod 6 out there. I didn’t spend the whole time parsing example documents myself though. Over the last two weekends I started the actual coding: what was to be done for week #1 and even #2. After some serious headscratching and a few wtf moments I ended up with a parser capable of parsing delimited, paragraph, and abbreviated blocks of Pod. 129 lines of code, 337 lines of tests. I also started moving the grammar rules into rakudo; I created a gsoc/podparser branch and started migrating the code there. It doesn’t bring any features, but it opens a way to fill in the action methods and actually produce the Pod node tree in parse time. At the moment of writing this, the branch fails on some of the spectests, which I plan fixing soon.

How will I fill the time that I got working in advance? Well, most of it will be lost^Wwasted^Wspent on the University work. My semester is still on, the exams finish on June the 30th, so it’s quite possible I won’t do any GSoC-related work in the upcoming weeks. On the other hand, I can’t imagine Physics or Signal Theory being more attractive than GSoC, so I’ll probably end up ruining my exam session and coding instead. But time will tell. Hopefully I’d be able to end up with the exams passed, and the GSoC on schedule.

TL;DR: Instead of community bonding, I started the actual coding and bit off most of week #1 and #2 of my schedule.


Zebras in the Perl 6 herd

Well, the #perl6 herd mostly. Where did they come from? Well, it all started with this tadzik guy one day on the #perl irc channel.

tadzik  | good evening zebras!
colomon | zebras?
tadzik  | oh, I just felt a need for a funny greeting
tadzik  | how are things?

Zebras had to wait to become noticed until the morning next day:

masak   | <tadzik> good evening zebras!
masak   | I for one hope this will become an instant classic :)
jnthn   | It may, but it's not black and white.

And it did became, actually. Since that day, zebras have been mentioned 230 on #perl6, my irc logs say. In a various forms, in a various contexts. But the main idea remained. -Ofun, all the way.

(Gee, that sounds so official)

Zebra is also a codename of a Secret Project of mine, a bit GSoC related. But that’s another story, and it’s not coming very soon anyway.


Follow

Get every new post delivered to your Inbox.