It’s time for me to praise one of the useful utilities that makes my everyday work just a little bit more pleasant: rlwrap.

rlwrap “wraps” other interactive programs, providing readline functionality for programs that don’t have it built in. For example, last week I had to repeatedly use ‘imtest’ and ‘cyradm’ while doing some exploration about IMAP. Neither of these tools supports readline, so I normally would be able to press ‘up’ to recall my last command, use ^W or ^U to clear parts of my command, or use ^R to search my history. This would be very frustrating.

So, here comes rlwrap. You use it just like sudo, prefixing it to the command you want to run, thus

imtest -u sweeks -a vmadmin -t '' -m plain localhost

becomes

rlwrap imtest -u sweeks -a vmadmin -t '' -m plain localhost

rlwrap even remembers command history on a per-command basis, so you can recall your interactive commands from previous sessions. This greatly eases debugging. rlwrap is a very valuable part of my toolkit. If this interests you, you can also enable filename tab-completion, or even custom wordlists for tab-completion of command-specific commands.

I mentioned this to my co-workers, and I was surprised to hear that none of them had ever heard of it before. What tools do you use that you take for granted that everybody knows about?

On Saturday, I wrote up a possible API for Parrot compilers to support loading libraries written in other languages and discussed some of the details with Jonathan++ and Allison++. It’s not perfect, and is missing a few parts, but should be extensible enough to support whatever else we need in the future. I still need to formalize it a bit and add it to the Parrot docs and the example language shell.

On Sunday, I implemented it on Rakudo (Perl 6) and Cardinal (Ruby; very incomplete).

This morning, after confirming the spec with pmicahud++, I merged the changes into Rakudo trunk.

The syntax for specifying the source language for Perl 6 is:

use Foo:lang<cardinal>;

I couldn’t quite figure out what an appropriate way to do this in Ruby would be, so I just added a function to cardinal:

foreign_load('perl6','Foo/Bar')

If you have a better suggestion for what it should look like in Ruby, please let me know! I don’t actually know much Ruby at all, so my Ruby compiler is fairly limited.

I’ll be adding support for this to pynie (Python) soon, and other languages after that.

Here’s a simple example of using a Perl library from Ruby:

[sweeks@kweh ~]$ cat Foo.pm
module Foo {
    sub greet($name) is export {
        say "Hello, $name!"
    }
}
[sweeks@kweh ~]$ cat perl6.rb
foreign_load 'perl6', 'Foo'
['Ruby', 'Perl', 'World'].each { |name| greet name }
[sweeks@kweh ~]$ cardinal perl6.rb
Hello, Ruby!
Hello, Perl!
Hello, World!

Here’s a similar example of using a Ruby library from Perl:

[sweeks@kweh ~]$ cat Foo.rb
module Foo
    def greet(name)
        puts "hello, " + name
    end
    def apply_people(cb)
        people = ['Dave', 'Bryan', 'Stuart', 'Dax']
        people.each { |name| cb(name) }
    end
end
[sweeks@kweh ~]$ cat ruby.pl
use Foo:lang<cardinal>;
greet("person $_") for 1..5;
apply_people( { say "hello from perl, $^name" } )
[sweeks@kweh ~]$ perl6 ruby.pl
hello, person 1
hello, person 2
hello, person 3
hello, person 4
hello, person 5
hello from perl, Dave
hello from perl, Bryan
hello from perl, Stuart
hello from perl, Dax

Thanks go to my employer (Guru Labs) for their support in my work on Rakudo and Parrot.

Rakudo is just starting to get support for adding custom operators to the grammar from user-level code. You can’t specify the precedence yet, but you can run the traditional examples:

multi sub infix:<±>(Int $a, Int $b) { return $a + $b | $a - $b }
multi sub postfix:<!>(Int $a where { $_ > 0 }) { return [*] 1..$a }

my $x = 5! ± 2;
say "hi dood" if $x > 121;
say "hello again" if $x < 119;

I was playing around today with defining operators for mathematical set operations (∩ ∪ ∖ ⊂ ⊃ ⊆ ⊇ etc.) and then decided that I wanted a fancy syntax for defining sets, so I added support to rakudo for circumfix operator definition, and i now have this running on rakudo:

say "subset" if ⦃ 1, 3, 5 ⦄ ⊆ ⦃ 1, 2, 3, 4, 5 ⦄;

Blog speed-run.

| No Comments | No TrackBacks

On Sunday, before I left for my flight, I saw masak chatting on IRC about his blog speed-run, which I later found described here. He set a time limit to see how quickly he could implement a basic blog app on the tools we've been building.

At the airport later that day, I had 20 minutes to wait before boarding my flight, so I thought I'd try the same thing. Omgblog is what I got done before I needed to board. I'll try to leave it running for a while. The source is in the usual place.

I’ve still been working on the Web libraries for Perl 6.

In the past week, I’ve improved my Tags library to handle more common cases and be easier to use, I migrated HTTP::Daemon to use Parrot sockets instead of exec-ing socat, and I wrote a basic pastebin: http://github.com/masak/web/blob/c84c4c1892463459e406958db8f9232e7b7bf5d1/bin/kopipasta.pl.

You might be able to see a running instance here: http://pleasedieinafire.net:2080/. That host has poor connectivity lately, though.

I’ve also played a bit with some ideas related to dispatch. Here’s an example I got working on the plane home from Boston:

use LolDispatch;
use HTTP::Daemon;
sub item($request, $match) is handler(/^\/item\/(\d+)/) {
    say 'dispatched to item';
    say $match.perl;
}
my $request = HTTP::Request.new(
    req_url => HTTP::url.new(path => '/item/12345'),
    headers => HTTP::Headers.new( header_values => { 'Host' => 'localhost' }),
    req_method => 'GET',
);
dispatch($request);

Here’s the output of that example:

dispatched to item
Match.new(
 # WARNING: this is not working perl code
 # and for debugging purposes only
 ast  => "/item/12345",
 text => "/item/12345",
 from => 0,
 to   => 11,
 positional => [
  Match.new(
     ast  => "12345",
     text => "12345",
     from => 6,
     to   => 11,
    ),
 ],
)

I'm participating in the web framework for Perl 6 grant from The Perl Foundation. My first task has been working on a Tags library for (X)HTML generation.

I've got the start of a simple port of Template Declare implemented. It's not perfect, and I'm still working out the API we want, but it's functional, and nice. It's quite a bit nicer than CGI.pm-style html generation.

The philosophy of this library is that templates are code, and we should be able to deal with them as such. If your template is Perl, you can use all of the same tools you use for Perl. No need to learn a new language, and far fewer angle brackets. :)

Here's an example that runs under the current Tags.pm. Keep in mind, there's still a decent amount of API refactoring needed.

use Tags;
say show {
    html {
        head { title { 'Tags Demo' } }
        body {
            outs "hi";
            ul :id<numberlist> {
                outs "A list from one to ten:";
                for 1..10 {
                    li :class<number>, { $_ }
                }
            }
        }
    }
}

I recently finished digging through the cleanups necessary to allow Parrot languages to exist in their own separate namespaces. Before this when you tried to run code from, say, Ruby and Perl 6 in the same interpreter, they would both try to define a Hash class, for example, and step on each other's toes in a variety of ways. Now you can load as many languages as you want into the same interpreter.

I also added a hackish implementation of the :lang parameter to Perl 6's eval that loads up the appropriate compiler to use instead of the Perl 6 compiler. This means that Rakudo can now do things like this:

eval(q<VISIBLE "O HAI GUYZ">, :lang<lolcode>);
my $x = eval(q<10×5÷2>, :lang<APL>);
my $rubysub = eval(q<do |i| puts "ruby got " + i; return i + 10 end>, :lang<cardinal>);
my $schemesub = eval(q<(lambda (msg) (write "scheme got " msg "\n"))>, :lang<pheme>);
$schemesub($rubysub($x));

The output of that is:

O HAI GUYZ
25
ruby got 25
scheme got 35

The extra '25' is there because the APL spec says that any non-assignment results in a print as a side-effect.

I haven't added support for this to any of the other Parrot languages yet because I don't know what the API should look like for any other language.

I haven't added support for loading foreign libraries to Perl 6 yet because there are a couple of awkward semantic issues to work out, and I haven't added support for loading foreign libraries to any other language yet because I don't know what the API should look like.

This is where you come in, my opinionated Internet friends. If you have suggestions for the API for evaluating foreign code or loading foreign libraries in any of Ruby, Python, LOLCODE, PHP, or any other Parrot language, please speak up here or on the social news site of your choice.

Thanks go to my employer for sponsoring my work on Parrot. Most of what I've done wouldn't be possible without their support.

Perl 6 - Given/When

| 1 Comment | No TrackBacks

Today I fixed the majority of the issues with Rakudo's 'when' blocks. When blocks do smartmatching against the current topic. For example, this:

when 'foo' { ... }

could also be written approximately like this:

if $_ ~~ 'foo' { ... }

The biggest difference is that 'when' blocks automatically break out to the innermost scope that has $_ as one of its formal parameters (either explicit or implicit), so:

for @numbers {
    when 1..5 { say 'low' }
    when 6..10 { say 'high' }
}

Could also be written as:

for @numbers {
    if $_ ~~ 1..5 { say 'low'; break }
    if $_ ~~ 6..10 { say 'high'; break }
}

(Which currently doesn't work in Rakudo, as we don't have control exceptions on loops handled properly yet.)

this lets us use the 'given' block, which just sets the topic for the block, to implement an idiomatic switch statement in Perl 6:

given $foo {
    when 'A' { say 'got A' }
    when 'B' { say 'got B' }
    when 'C' { say 'got C' }
    when '1' { say 'got 1' }
    when '2' { say 'got 2' }
    when '3' { say 'got 3' }
    when /foo/ { say 'matched a regex' }
    when 1..100 { say 'matched a range' }
    when 'etc' { say 'you know... like this.' }
    default { say 'When all else fails...' }
}

There's a handler for CONTINUE exceptions around every 'when' block to continue on through the block, so if you want to fall through, you just say:

given $num {
    when '3' { say 'got 3'; continue }
    when 1..100 { say 'got from 1 to 100' }
}

Non-fatal exceptions

| No Comments | No TrackBacks

Parrot's exceptions have a 'severity' attribute to indicate, of course, how severe the exception is. The current severities are: (taken from runtime/parrot/include/except_severity.pasm

EXCEPT_NORMAL   
EXCEPT_WARNING  
EXCEPT_ERROR    
EXCEPT_SEVERE   
EXCEPT_FATAL    
EXCEPT_DOOMED   
EXCEPT_EXIT

For a long time now, the spec has said that non-fatal exceptions shouldn't cause termination of the program, but should result in the message being printed and normal execution resuming. This hasn't been the case until today. It was a pretty simple change, but should make some things much nicer in the future.

Here's a simple program demonstrating this behaviour:

.include 'include/except_severity.pasm'
.sub main :main
    say 'before the warning'
    $P0 = new 'Exception'
    $P0['severity'] = .EXCEPT_WARNING
    $P0['message'] = "\tOMG something is kinda wrong"
    throw $P0
    say 'after the warning'
.end

And here's the output:

[sweeks@kweh parrot]$ ./parrot et.pir
before the warning
    OMG something is kinda wrong
after the warning

I've also updated Perl 6's warn() to use a non-fatal exception, meaning that these can now be caught.

Currying in Perl 6

| No Comments | No TrackBacks

There is still quite a lot of low-hanging fruit in Rakudo.

I checked on IRC today during a break in the class I'm teaching to see jhorwitz asking for currying in Perl 6, which is spelled like:

my $curried = &function.assuming(...);

A few minutes later, I committed an implementation of it. Here is the currying patch for Rakudo. It's about the same size as the last patch I mentioned here. Here are the tests we're now passing:

sub tester(:$a, :$b, :$c) {
"a$a b$b c$c";
}

my $u = &tester.assuming(b => 'x');
is $u(a => 'w', c => 'y'), 'aw bx cy', 'currying one named param';

my $w = &tester.assuming(b => 'b');
my $v = $w.assuming(c => 'c');
is $v(a => 'x'), 'ax bb cc', 'can curry on an already curried sub';
is $w(a => 'x', c => 'd'), 'ax bb cd', '... and the old one still works';

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • Anthony: Are there any difference between Movable type blogs and Joomla read more
  • Nathaniel Christen: I think it's great that Perl6 has advanced to the read more
  • renormalist: Really nice, indeed! For an occasional Parrot follower like me, read more
  • renormalist: Nice. Can you paste the source here to recreate this read more
  • ab5tract: I too have been waiting for this moment since the read more
  • Charles: This is the moment I've been waiting for since Parrot read more
  • Derek: The idiomatic way of doing this in ruby is to read more
  • Steve Jorgensen: Regarding the appropriate methods for loading, Ruby normally uses "require" read more
  • mark: Now actually that is brilliant. No matter if people dislike read more
  • Stephen Weeks: :P read more

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en