<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Stephen Weeks</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.gurulabs.com/stephen/atom.xml" />
    <id>tag:blogs.gurulabs.com,2009-02-24:/stephen/13</id>
    <updated>2009-05-26T07:56:37Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.32-en</generator>

<entry>
    <title>Useful Utilities - rlwrap</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/05/useful-utilities---rlwrap.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.264</id>

    <published>2009-05-26T07:45:51Z</published>
    <updated>2009-05-26T07:56:37Z</updated>

    <summary>It&#8217;s time for me to praise one of the useful utilities that makes my everyday work just a little bit more pleasant: rlwrap. rlwrap &#8220;wraps&#8221; other interactive programs, providing readline functionality for programs that don&#8217;t have it built in. For...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>It&#8217;s time for me to praise one of the useful utilities that makes my everyday work just a little bit more pleasant: rlwrap.</p>

<p>rlwrap &#8220;wraps&#8221; other interactive programs, providing readline functionality for programs that don&#8217;t have it built in.  For example, last week I had to repeatedly use &#8216;imtest&#8217; and &#8216;cyradm&#8217; while doing some exploration about IMAP.  Neither of these tools supports readline, so I normally would be able to press &#8216;up&#8217; 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.</p>

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

<pre><code>imtest -u sweeks -a vmadmin -t '' -m plain localhost
</code></pre>

<p>becomes</p>

<pre><code>rlwrap imtest -u sweeks -a vmadmin -t '' -m plain localhost
</code></pre>

<p>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.</p>

<p>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?</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Cross-language library loading on Parrot</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/05/cross-language-library-loading.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.263</id>

    <published>2009-05-18T16:52:16Z</published>
    <updated>2009-05-19T03:14:31Z</updated>

    <summary>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&#8217;s not perfect, and is missing a few parts, but should...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Perl6" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrot" label="parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrot" label="Parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl6" label="Perl 6" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>On Saturday, I wrote up <a href="http://nopaste.snit.ch/16580">a possible API for Parrot compilers to support loading libraries written in other languages</a> and discussed some of the details with Jonathan++ and Allison++.  It&#8217;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.</p>

<p>On Sunday, I implemented it on <a href="http://github.com/rakudo/rakudo">Rakudo</a> (Perl 6) and <a href="http://github.com/cardinal/cardinal">Cardinal</a> (Ruby; very incomplete).</p>

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

<p>The syntax for specifying the source language for Perl 6 is:</p>

<pre><code>use Foo:lang&lt;cardinal&gt;;
</code></pre>

<p>I couldn&#8217;t quite figure out what an appropriate way to do this in Ruby would be, so I just added a function to cardinal:</p>

<pre><code>foreign_load('perl6','Foo/Bar')
</code></pre>

<p>If you have a better suggestion for what it should look like in Ruby, please let me know!  I don&#8217;t actually know much Ruby at all, so my Ruby compiler is fairly limited.</p>

<p>I&#8217;ll be adding support for this to <a href="http://code.google.com/p/pynie">pynie</a> (Python) soon, and other languages after that.</p>

<p>Here&#8217;s a simple example of using a Perl library from Ruby:</p>

<pre><code>[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!
</code></pre>

<p>Here&#8217;s a similar example of using a Ruby library from Perl:</p>

<pre><code>[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&lt;cardinal&gt;;
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
</code></pre>

<p>Thanks go to <a href="http://gurulabs.com/">my employer (Guru Labs)</a> for their support in my work on Rakudo and Parrot.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>New Toys in Perl 6: custom ops.</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/05/new-toys-in-perl-6-custom-ops.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.262</id>

    <published>2009-05-15T04:49:22Z</published>
    <updated>2009-05-15T05:29:13Z</updated>

    <summary><![CDATA[Rakudo is just starting to get support for adding custom operators to the grammar from user-level code. You can&#8217;t specify the precedence yet, but you can run the traditional examples: multi sub infix:&lt;±&gt;(Int $a, Int $b) { return $a +...]]></summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Perl6" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrot" label="Parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl6" label="Perl 6" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>Rakudo is just starting to get support for adding custom operators to the grammar from user-level code.  You can&#8217;t specify the precedence yet, but you can run the traditional examples:</p>

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

my $x = 5! ± 2;
say "hi dood" if $x &gt; 121;
say "hello again" if $x &lt; 119;
</code></pre>

<p>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:</p>

<pre><code>say "subset" if ⦃ 1, 3, 5 ⦄ ⊆ ⦃ 1, 2, 3, 4, 5 ⦄;
</code></pre>
]]>
        

    </content>
</entry>

<entry>
    <title>Blog speed-run.</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/05/blog-speed-run.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.261</id>

    <published>2009-05-13T03:07:25Z</published>
    <updated>2009-05-13T03:21:16Z</updated>

    <summary>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...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Grant" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Perl6" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="grant" label="Grant" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl6" label="Perl 6" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="web" label="Web" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>On Sunday, before I left for my flight, I saw masak chatting on IRC about his blog speed-run, which I later found <a href="http://use.perl.org/~masak/journal/38946">described here</a>.  He set a time limit to see how quickly he could implement a basic blog app on the tools we've been building.</p>

<p>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.  <a href="http://pleasedieinafire.net:2080/">Omgblog</a> is what I got done before I needed to board.  I'll try to leave it running for a while.  The source is in <a href="http://github.com/masak/web/blob/d42596a457ebe15115a7ea07ae96bdc9034231a7/bin/omgblog.pl">the usual place</a>.</p>]]>
        
    </content>
</entry>

<entry>
    <title>More Web Grant Updates - Pastebin</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/04/more-web-grant-updates---paste.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.260</id>

    <published>2009-04-19T21:26:57Z</published>
    <updated>2009-04-20T00:00:06Z</updated>

    <summary>I&#8217;ve still been working on the Web libraries for Perl 6. In the past week, I&#8217;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...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Grant" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Perl6" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>I&#8217;ve still been working on the Web libraries for Perl 6.</p>

<p>In the past week, I&#8217;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: <a href="http://github.com/masak/web/blob/c84c4c1892463459e406958db8f9232e7b7bf5d1/bin/kopipasta.pl">http://github.com/masak/web/blob/c84c4c1892463459e406958db8f9232e7b7bf5d1/bin/kopipasta.pl</a>.</p>

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

<p>I&#8217;ve also played a bit with some ideas related to dispatch.  Here&#8217;s an example I got working on the plane home from Boston:</p>

<pre><code>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 =&gt; HTTP::url.new(path =&gt; '/item/12345'),
    headers =&gt; HTTP::Headers.new( header_values =&gt; { 'Host' =&gt; 'localhost' }),
    req_method =&gt; 'GET',
);
dispatch($request);
</code></pre>

<p>Here&#8217;s the output of that example:</p>

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

    </content>
</entry>

<entry>
    <title>Tags.pm for the Perl 6 Web project</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/03/tagspm-for-the-perl-6-web-proj.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//13.257</id>

    <published>2009-03-22T00:56:29Z</published>
    <updated>2009-03-22T01:13:15Z</updated>

    <summary>I&apos;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&apos;ve got the start of a simple port of Template Declare implemented. It&apos;s...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Grant" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Perl6" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>I'm participating in the <a href="http://www.perlfoundation.org/ilya_carl_and_stephen_a_lightweight_web_framework_for_perl_6">web framework for Perl 6 grant</a> from The Perl Foundation.  My first task has been working on a Tags library for (X)HTML generation.</p>

<p>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.</p>

<p>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. :)</p>

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

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

    </content>
</entry>

<entry>
    <title>Parrot Speaks Your Language</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2009/01/parrot-speaks-your-language.html" />
    <id>tag:blogs.gurulabs.com,2009:/stephen//12.267</id>

    <published>2009-01-06T04:12:37Z</published>
    <updated>2009-01-06T04:48:38Z</updated>

    <summary>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...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrot" label="parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>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.</p>

<p>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:</p>

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

<p>The output of that is:</p>

<pre><code>O HAI GUYZ
25
ruby got 25
scheme got 35
</code></pre>

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

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>Thanks go to <a href="http://gurulabs.com/">my employer</a> for sponsoring my work on Parrot.  Most of what I've done wouldn't be possible without their support.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Perl 6 - Given/When</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2008/12/perl-6-givenwhen.html" />
    <id>tag:blogs.gurulabs.com,2008:/stephen//12.266</id>

    <published>2008-12-16T14:32:07Z</published>
    <updated>2008-12-16T15:17:25Z</updated>

    <summary>Today I fixed the majority of the issues with Rakudo&apos;s &apos;when&apos; blocks. When blocks do smartmatching against the current topic. For example, this: when &apos;foo&apos; { ... } could also be written approximately like this: if $_ ~~ &apos;foo&apos; {...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrot" label="Parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="perl6" label="Perl 6" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>Today I fixed the majority of the issues with Rakudo's 'when' blocks.  When blocks do smartmatching against the current topic.  For example, this:</p>

<pre><code>when 'foo' { ... }
</code></pre>

<p>could also be written approximately like this:</p>

<pre><code>if $_ ~~ 'foo' { ... }
</code></pre>

<p>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:</p>

<pre><code>for @numbers {
    when 1..5 { say 'low' }
    when 6..10 { say 'high' }
}
</code></pre>

<p>Could also be written as:</p>

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

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

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

<pre><code>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...' }
}
</code></pre>

<p>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:</p>

<pre><code>given $num {
    when '3' { say 'got 3'; continue }
    when 1..100 { say 'got from 1 to 100' }
}
</code></pre>
]]>
        

    </content>
</entry>

<entry>
    <title>Non-fatal exceptions</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2008/12/nonfatal-exceptions.html" />
    <id>tag:blogs.gurulabs.com,2008:/stephen//12.264</id>

    <published>2008-12-15T14:03:28Z</published>
    <updated>2008-12-16T14:37:07Z</updated>

    <summary>Parrot&apos;s exceptions have a &apos;severity&apos; 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...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="exceptions" label="exceptions" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrot" label="Parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>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</p>

<pre><code>EXCEPT_NORMAL   
EXCEPT_WARNING  
EXCEPT_ERROR    
EXCEPT_SEVERE   
EXCEPT_FATAL    
EXCEPT_DOOMED   
EXCEPT_EXIT
</code></pre>

<p>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 <a href="http://www.parrotvm.org/svn/parrot/revision?rev=33912">pretty simple change</a>, but should make some things much nicer in the future.</p>

<p>Here's a simple program demonstrating this behaviour:</p>

<pre><code>.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
</code></pre>

<p>And here's the output:</p>

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

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

    </content>
</entry>

<entry>
    <title>Currying in Perl 6</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2008/12/currying-in-perl-6.html" />
    <id>tag:blogs.gurulabs.com,2008:/stephen//12.263</id>

    <published>2008-12-12T03:25:01Z</published>
    <updated>2008-12-12T03:30:57Z</updated>

    <summary>There is still quite a lot of low-hanging fruit in Rakudo. I checked on IRC today during a break in the class I&apos;m teaching to see jhorwitz asking for currying in Perl 6, which is spelled like: my $curried =...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrot" label="Parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>There is still quite a lot of low-hanging fruit in Rakudo.</p>

<p>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:</p>

<p>    my $curried = &function.assuming(...);</p>

<p>A few minutes later, I committed an implementation of it.  Here is the <a href="http://www.parrotvm.org/svn/parrot/revision?rev=33804">currying patch for Rakudo</a>.  It's about the same size as the last patch I mentioned here.  Here are the tests we're now passing:</p>

<p>    sub tester(:$a, :$b, :$c) {<br />
        "a$a b$b c$c";<br />
    }<br />
    <br />
    my $u = &tester.assuming(b => 'x');<br />
    is $u(a => 'w', c => 'y'), 'aw bx cy', 'currying one named param';<br />
    <br />
    my $w = &tester.assuming(b => 'b');<br />
    my $v =  $w.assuming(c => 'c');<br />
    is $v(a => 'x'), 'ax bb cc', 'can curry on an already curried sub';<br />
    is $w(a => 'x', c => 'd'), 'ax bb cd', '... and the old one still works';</p>]]>
        
    </content>
</entry>

<entry>
    <title>Exception Handlers in HLLs</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2008/12/exception-handlers-in-hlls.html" />
    <id>tag:blogs.gurulabs.com,2008:/stephen//12.262</id>

    <published>2008-12-11T12:21:10Z</published>
    <updated>2008-12-11T14:07:10Z</updated>

    <summary>I just added support to the Parrot Compiler Toolkit for generating exception handlers. The only language I&apos;ve patched to use this so far is Perl 6. For example, this works: do { say &quot;foo&quot;; die &quot;here is a failure&quot;; say...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="exceptions" label="exceptions" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="parrot" label="parrot" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>I just added support to the Parrot Compiler Toolkit for generating exception handlers.  The only language I've patched to use this so far is Perl 6.</p>

<p>For example, this works:</p>

<pre><code>do {
    say "foo";
    die "here is a failure";
    say "baz";
    CATCH {
        say "bar" if $_&lt;message&gt; ~~ /failure/;
        $_&lt;resume&gt;();
    }
}
</code></pre>

<p>And produces the output:</p>

<pre><code>[sweeks@kweh]$ perl6 ehtest.pl
foo
bar
baz
[sweeks@kewh]$
</code></pre>

<p>Any Parrot language can now add support for exception handlers by adding a PAST::Control node to the 'handlers' attribute of a PAST::Stmts or PAST::Block node.</p>

<p>Here's the <a href="http://www.parrotvm.org/svn/parrot/revision?rev=33783">patch to Rakudo</a>.  Pretty small.</p>

<p>After another minor supporting change to Parrot, we'll have CONTROL blocks, and soon after we'll start supporting all of the control exceptions (next/last/redo) in all the appropriate loops.</p>

<p>Contact me on IRC if you want help or need anything else to support this in your language.</p>

<p>Thanks to my employer, <a href="http://gurulabs.com">Guru Labs</a> for supporting all my work on Parrot.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Exceptions in Parrot</title>
    <link rel="alternate" type="text/html" href="http://blogs.gurulabs.com/stephen/2008/11/exceptions-in-parrot.html" />
    <id>tag:blogs.gurulabs.com,2008:/stephen//12.258</id>

    <published>2008-11-17T22:01:53Z</published>
    <updated>2008-11-18T05:02:26Z</updated>

    <summary>So, let&#8217;s talk about exceptions in Parrot. All of the code in this blog entry is in PIR (Parrot Intermediate Representation, Parrot&#8217;s equivalent of ASM). Throwing an exception from Parrot is pretty straightforward: .sub main :main $P0 = new &apos;Exception&apos;...</summary>
    <author>
        <name>Stephen Weeks</name>
        <uri>http://blogs.gurulabs.com/stephen</uri>
    </author>
    
        <category term="Parrot" scheme="http://www.sixapart.com/ns/types#category" />
    
    <category term="parrottutorial" label="parrot tutorial" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en-US" xml:base="http://blogs.gurulabs.com/stephen/">
        <![CDATA[<p>So, let&#8217;s talk about exceptions in Parrot.</p>

<p>All of the code in this blog entry is in PIR (Parrot Intermediate Representation, Parrot&#8217;s equivalent of ASM).</p>

<p>Throwing an exception from Parrot is pretty straightforward:</p>

<pre><code>.sub main :main
    $P0 = new 'Exception'
    throw $P0
.end
</code></pre>

<p>If you want a message on the exception, a severity, etc, it&#8217;s like this:</p>

<pre><code>.sub main :main
    $P0 = new 'Exception'
    $P0 = "I couldn't foo. :("
    $P0['severity'] = .EXCEPT_ERROR
    throw $P0
.end
</code></pre>

<p>Catching an exception isn&#8217;t much harder:</p>

<pre><code>.sub main :main
    push_eh handler
    function_that_could_fail()
    pop_eh
    .return (1)
  handler:
    .get_results ($P0)
    say "Couldn't foo because some function failed"
    .return (0)
.end
</code></pre>

<p>That function just returns failure if an exception was thrown between &#8216;push_eh&#8217; and &#8216;pop_eh&#8217;.  Pretty straightforward.</p>

<p>If you wanted to actually recover from the exception and resume continuation, we can do this too.  Parrot adds a continuation to the exception object that you can invoke to resume execution at the next opcode after you threw the exception:</p>

<pre><code>.sub main :main
    push_eh handler
    maybe_fail()
    pop_eh
    .return (1)
  handler:
    .local pmc exception, continuation
    .get_results (exception)
    cleanup_and_recover()
    continuation = exception['resume']
    continuation()
.end
</code></pre>

<p>There&#8217;s a potential problem here with what happens when something in the exception handler, such as the &#8216;cleanup<em>and</em>recover&#8217; function we imagined here, throws an exception.  When this exceptions system was first implemented, the handler would just catch the exception again, the handler would misbehave again, infinite loop.  Bad.</p>

<p>The solution that has been in place for quite a while is to mark the exception handler as &#8220;already used&#8221; and then to have exception handlers refuse to handle anything if they had already been used.  This doesn&#8217;t play nicely with resumable exceptions.  For example, this code:</p>

<pre><code>.sub main :main
    .local pmc e
    push_eh handler
    e = new 'Exception'
    e = "First Exception"
    throw e
    e = new 'Exception'
    e = "Second Exception"
    throw e
    pop_eh
    say "Everything was fine."
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught exception with message: "
    say ex
    co = ex['resume']
    co()
.end
</code></pre>

<p>Currently produces this output:</p>

<pre><code>[sweeks@kweh parrot]$ ./parrot exception-demo.pir
Caught exception with message: First Exception
Second Exception
current instr.: 'main' pc 16 (exception-demo.pir:9)
</code></pre>

<p>Parrot can&#8217;t find a valid handler for the exception, so it prints the message and the file/line-number it was thrown on and dies.</p>

<p>The currently-used workaround for this is to make an actual ExceptionHandler object and manually mark the exception handler as unused before invoking the resume continuation.  This is ugly.</p>

<pre><code>.sub main :main
    .local pmc eh, e
    eh = new 'ExceptionHandler'
    set_addr eh, handler
    push_eh eh
    e = new 'Exception'
    e = "First Exception"
    throw e
    e = new 'Exception'
    e = "Second Exception"
    throw e
    say "Everything was fine."
    pop_eh
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught exception with message: "
    say ex
    eh = 0
    co = ex['resume']
    co()
.end
</code></pre>

<p>This works, though:</p>

<pre><code>[sweeks@kweh parrot]$ ./parrot exception-demo.pir
Caught exception with message: First Exception
Caught exception with message: Second Exception
Everything was fine.
</code></pre>

<p>To deal with this ugliness, I added the ability to set a filter for exception types or severities on exception handlers a while back.  That works like the following:</p>

<pre><code>.include 'include/except_severity.pasm'
.sub main :main
    .local pmc eh, e
    eh = new 'ExceptionHandler'
    set_addr eh, handler
    eh.'min_severity'(.EXCEPT_NORMAL)
    eh.'max_severity'(.EXCEPT_WARNING)
    push_eh eh
    e = new 'Exception'
    e = "First Exception"
    e['severity'] = .EXCEPT_NORMAL
    throw e
    e = new 'Exception'
    e = "Second Exception"
    e['severity'] = .EXCEPT_WARNING
    throw e
    say "Everything was fine."
    pop_eh
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught a warning: "
    say ex
    co = ex['resume']
    co()
.end
</code></pre>

<p>That example will only work on parrot r32783 or newer, due to a small bug I just fixed.</p>

<p>So, this &#8216;disable exception handler&#8217; behavior is very simple to remove.  We&#8217;ve left it in so long because there are still a few tests that rely on the old behavior.  We&#8217;re looking to remove it soon, though.</p>

<p>That&#8217;s about it for the basics of using exceptions in Parrot.  Soon I&#8217;ll post about the bug in Parrot that made resuming to another subroutine not work.</p>
]]>
        

    </content>
</entry>

</feed>

