Sunday, July 19, 2009

Perl 6 At Work

So, the Perl 6 script I talked about in my last two posts is now every bit as functional as the Perl 5 script it is descended from. And while I certainly recognize there is more to do to improve this script (both in better Perl 6 code and improved user interface), I can't begin to say how excited I am by this script. It really feels like a proper Perl 6 script at this point, and it strikes me as clearly superior to the Perl 5 version.

What do I like about it? The GetMatches sub is elegant in the way it takes a Regex and allows me to replace five lines of fairly hacky Perl 5 code with a single elegant line of Perl 6. And the given/when construct is a much cleaner way of handling command line arguments. (Admittedly this also gained over the Perl 5 version by eliminating dead code for ways I thought I would use the original script when I first wrote it.) Overall, the code has gotten significantly more element than the Perl 5 version, despite the fact that Perl 6 isn't finished yet.

Oh yes, what does this do? It extracts entities from a (Part 21 encoded) STEP file, including all the sub-entities that entity depends on. This is invaluable for debugging STEP support.

Improvements? In terms of Perl 6 usage, I'd love to get rid of the loop at the end that only does say. I guess I could use map for that, but just as I didn't like the thought of using map and ignoring its inputs, I don't like the thought of using map with side-effects.

I also wonder if the given statement is even needed. Can when be used with $_ from a for loop? And I look at the split loop and wonder if that could be more elegantly written in terms of my GetMatches function.

In terms of general code, I think there would be benefits from switching the entity references to straight numbers instead of numbers proceeded by a '#'. I'd certainly like some usage information. The code would be more reliable if it actually parsed the STEP data with some rules -- right now it's assuming that no one will ever use a semi-colon in a string in one of these files, and while that's never really caused me trouble in the past, it is a rash assumption. And I'd like to have support for tracing each entity's usage up to the top level to get its STEP context, which can be very important for imported. (Right now I do that sort of thing by hand after letting the script do the dirty work.)

Updated: Oh yes, for/when works every bit as well as given/when!

Updated again: One thing I'd really like to do is set up some tests for this -- if nothing else, it would make improving the script easier. Does anyone have hints on testing in Perl 6?

No comments:

Post a Comment