2006.09.11

The Benefits of Smart Linking

Today's learning experience with Perl 6 was Smart Links. Smart Links refer to a special syntax used in the test suite which connect a particular part of a test script with a related point the Perl 6 specification.

Smart links are especially valuable for a language in development. They help answer these important questions:

  • Has this feature been tested? If you don't see references to tests from the spec, there's a possibility the feature is not only untested, but unimplemented!
  • Are there duplicates tests for this feature? If you see more than one test script linked in to the same point in the spec, it's possible there are duplicate tests, and the tests suite can be simplified.
  • Is there a spec for this feature? On the other hand, if you find a test without a smart link, it may simply not have one yet, or the feature may be unspec'ed. It was discovered that the basic function "say" did not have a formal definition through this kind of review. 

However, to the receive the benefits of smart linking, the smart links must actually exist. Today, 263 test scripts have at least one smart link, but 349 do not.

To help with this, you mostly just need some motivation to help, and commit access to the pugs tree. Ask for it on #perl6 if you don't have it.

I posted some specific tips on getting started with smart linking on the new Smart Linking wiki page

2006.09.10

Help write Perl 6 in Perl 6

Today I discovered to my surprise that part of the core of Perl 6 is written in Perl6. This is Prelude.pm.  Having this part of the code in Perl6 means it is easier for Perl 6 users to hack on. It also means this functionality is available to all Perl 6 implementations, not just Pugs.

For effiency, Prelude.pm is compiled into a YAML format file, and later read back into Haskell with less effort.

There are still some functions which are currently implemented in Pugs which would be good candidates to include in Prelude.pm. These are listed on the Preludification Candidates page, although I can't vouch for the freshness of the list. This is your chance to write some of Perl 6 in Perl 6!

(There's also a Prelude/PIR.pm which is written in a Perl 6, and I'm not sure how it's related. If you know, please add a comment to the top of PIR.pm to explain the difference. )

2006.09.09

Getting Started with Perl 6 modules

Humberto Massa asked about how to get stated hacking CGI.pm for Perl 6. To answer his question, I've enhanced some of the Perl 6 wiki pages. The download page is cleaner now and I've added a new page on getting started with Perl 6 module projects. Mostly you need to know to use util/prove6 to run your tests with, instead of prove.

If you have further questions use the mailing lists or IRC to get in touch with the team.

Happy Hacking!
 

2006.09.08

CGI::App to Perl 6: hacking on CGI.pm-p6

Today's work on porting CGI::Application to Perl 6 was hacking on CGI.pm for Perl 6, which we depend on. (You can of course choose any sufficiently compatible alternative). While several people have thankfully worked on it in the past, no else is maintaining at the moment for Perl 6, and it remains incomplete.

Today I noticed that while it had escapeHTML and unescapeHTML methods, they were both untested and broken. They were fairly easy to fix,  and I learned more about Perl 6 regular expressions in the process.  I added this related entry to Differences.pod:

    Was:    $str =~ s/(a)/$1/e;
    Now:    $str ~~ s:P5/(a)/{$0}/;

A substitition had been ported from CGI.pm-p5, but was now failing because $1 needed to be replaced with $0. Take note!

Next, I noticed that charset support was incomplete, and cookie handling is not yet implemented. You can see how this is turning into a detour...

The CGI.pm porting is fairly easy if anyone wants to dive in and lend a hand.

 

2006.09.06

Three Reasons to Port to Perl 6 now

Porting CGI::Application to Perl 6 has been a great experience.I offer these three reasons for others considering porting a module to Perl 6.

Continue reading "Three Reasons to Port to Perl 6 now" »

2006.09.04

CGI::App to Perl 6: A plugin system in one line of code.

Audrey Tang has the implemented the initial working functionality in a powerful new Perl 6 feature, calling sets of methods. Here's how we'll be using it in CGI::Application for Perl 6.

In Perl 5, we used 65 lines of code in a "call_hook" function that took care of running all the plugged-in code at a particular point in the execution process. It would look through up through the inheritance try and execute perhaps multiple bits of plugging functionality at each point. The is one of the only places in CGI::Application where we used an an external module to help, making use of Class::ISA to determine the correct inheritance tree.

In Perl 6, we can replace all that busy work with a one line.

Continue reading "CGI::App to Perl 6: A plugin system in one line of code." »

2006.09.03

Perl 6, a multi-lingual language

You can inspect a complex data structure in Perl 6 just by adding the.perl method:

say %h.perl

Perl 5 can't even speak Perl 5 like that without some help from a module:

use Data::Dumper; print Dumper (%h) ;

Pugs not only speaks Perl 6 easily, it's also fluent in Perl5 and YAML.

Continue reading "Perl 6, a multi-lingual language" »

2006.09.02

CGI::App to Perl 6: Another improvement for references

Earlier I wrote about how references are gone in Perl6. However, subroutine signature Perl6 expose just opposite-- that really everything is a reference.

We use modify-by-reference in CGI::Application to prevent copying large HTML documents into the "postrun" routine.

This is a technique that should be used sparingly, as returning explicit values creates for clearer code flow.

Perl6 helps promotes good programming practices in two ways in this regard. 1. You have explicitly turn on modify-by-reference for a subroutine argument, and 2. This explicitness makes it clear that this trick is being employeed. Let's a take a look at the syntax:

  Was: sub foo {...};        foo(\$bar)   Now: sub foo ($bar is rw); foo($bar)

Perl6 is always passing references to subroutines in the background, they are just read-only by default.  While in Perl5 you might have a dig around to see if modification by reference was happening in a subroutine, in Perl6 you can look no further than the signature.

If you want a copy of the value passed, Perl6 covers that too:

  sub foo ($bar is copy);

Aspects of this may seem formal compared to the looseness of Perl5. So far I've generally liked these changes, as compactness and clarity often come with them.

CGI::App to Perl 6: param() is now 90% shorter

Lot's of OO modules have a param() method: CGI::Application,  HTML::Template, CGI::Session... and they are mostly all about the same. That is, they are a glorifed hash wrapper.

Yet, in Perl5 it's easy for the code for them to be verbose, especiall if you want to accept both a hash /and/ a hashref, and provide some error checking on the input.

The param() method of CGI::App in Perl5 was 33 lines (including some whitespace). The Perl6 translation was 3 lines! This is possible with subroutine signatures and multi-method dispatch. Further, Perl6 tries somewhat successfully to hid e the difference between a hash and hashref, which eliminates another case to check for.

For constrast, here's the two pieces of code:

Continue reading "CGI::App to Perl 6: param() is now 90% shorter" »

CGI::App to Perl 6: Reference handling not needed here

I'm still wrapping my head around what means that references have been replaced with Capture objects in Perl6.

However, I was pleased to see it means this common idiom is no longer necessary. Often I have a variable that may contain a single scalar value or an arrayref of values.

Perl5:

my @new = (ref $old eq 'ARRAY' ) ? @$old : ($old);

But Perl6 Does The Right Thing, Easily

my @new = @$old;

Perl5 would complain if you tried to deference $old as an array when it wasn't an arrayref.

Now I have look through the CGI::Application code and see all the places I can shave off a few bytes with this refactor...

Here's another case in Perl6 where reference handling is not needed, while Perl5 requires it:

# Perl6 will properly assign @a to the 'k' key,
# without using reference notation.
my @a = ( 1,2 );
my %h = (
    k => @a,
    j => 'm',
);