Mechanical

Perl 6

Haven't posted in a while. In fact, much of my time has been spent with Perl 6, the upcoming version of Perl. And it's sort of been an accident.

A long time ago I stated the Perl 6 Cookbook project and recently I took another look at it. A few people have updated it, but not many. Unfortunately, I realized that the examples wouldn't run due to documentation formatting issue, so I started updating them. In trying to get them to work, I encountered the trimming blanks example.

$string = $string.trim;
$string = trim($string);
# or more concisely:
$string .= trim;

This was a snippet of code which removes whitespace at the beginning and end of strings. I fixed the formatting issues and tried to run the code, only to discover that no one had implemented the .trim method in Perl 6. What's worse, there weren't even any "spec" tests to verify the behavior once implemented. So to make a long story short, I wrote tests for this function and submitted a patch to implement .trim in Perl 6. It's written in PIR (a sort of "object oriented" assembly language which I had to teach myself) and looks like this:

.sub 'trim' :method :multi(_)
    .local string s
    .local int start, end, temp, len
    .local int is_whitespace
    s = self
    start = 0
    end = length s
    if end == 0 goto donetrailing
  trimleading:
    is_whitespace = is_cclass .CCLASS_WHITESPACE, s, start
    unless is_whitespace goto doneleading
    inc start
    goto trimleading
  doneleading:
    temp = end
  trimtrailing:
    dec temp
    is_whitespace = is_cclass .CCLASS_WHITESPACE, s, temp
    unless is_whitespace goto donetrailing
    end = temp
    goto trimtrailing
  donetrailing:
    len = end - start
    s = substr s, start, len
    .return(s)
.end

The patch has been accepted and now I've implemented my first core Perl 6 feature. So why weren't there any tests for this? Because it turns out that .trim was never in the Perl 6 specification. Larry Wall, however, has agreed to the basic idea and it's going in the spec. After a few other issues are hammered out, I'll be extending this (probably .trim_start and .trim_end) and the spec will be updated.

So the Perl 6 spec is being changed and I've added a new core feature to Perl 6 all because I didn't double-check the spec when I should have. Go figure.

Also, it turns out that the test system for Perl 6 needs a lot of work. I started updating it to make it easier to test my changes, but that exposed other bugs in Perl 6. Looks like I'm going to be busy ...

  • Current Mood: accomplished accomplished
  • Current Music: Skinny Puppy | Jahya
Extended Trim Functions
I suggest ltrim() and rtrim(). Otherwise, which end of the string would trim_start() effect if the string is written in Hebrew?

—Theory
Re: Extended Trim Functions

Actually, it's Hebrew which was the specific example I've used to avoid rtrim and ltrim. Consider the sentence "blessed be the name of his glorious kingdom, forever and ever" written in Hebrew (if you don't see Hebrew in the next bit, you don't have Hebrew fonts installed):

בָּרוּךְ שֵׁם כְּבוֹד מַלְכוּתוֹ לְעוֹלָם וָעֶד

Hebrew is an RTL (right-to-left) language. Due to how strings work internally in Parrot, rtrim would trim the end of that string, just as it would trim the end of this sentence. However, rtrim, short for "right trim" (just as it is for the other languages which implement it), would be trimming the left side of that string. People would justifiably be confused. The only way to avoid that would be for me to internally detect if it's an RTL string and reverse the string, apply the trim, and reverse it again. Also, if somehow the RTL logic gets convoluted, then one could easily trim the wrong end of the string. That's much easier to explain to people.

With .Net, they use TrimStart and TrimEnd (iirc) and this not only avoids the ambiguity, but simplifies the internal logic tremendously. .trim_start would always trim the start of the string, regardless of whether or not it's on the left or right side of the string.

I wouldn't want my Hebrew reading friends to be mad at me :)

Note: I don't know Hebrew. I had that quote wrong originally and had a couple of people correct me. Hope I didn't get it wrong again.

The only change I'd think worthy is being able to pass in character classes to trim (and its variants). That way it would be easy to remove characters that make sense:

qq(<"http://publius-ovidius.livejournal.com/">).trim(['"<>])

(Or whatever the Perl6 equivalent would be.)
If I recall correctly, Larry already vetoed a similar idea. He wants the common case covered because the less common variants are easy to handle.
Sounds like another opportunity to not pay attention to the spec. ;-)

If you're not allowed to pass in your own character class, then how would you do it?

[ $str ].map { s/^[chars]*//; s/[chars]*$//; $_ }
I don't really know you, but hear sideways of you as the "Perl Guru of Gurus" so I naturally assumed you had written the entire language yourself.

Thanks, by the way, for creating scalars. Those are very useful in my work. I appreciate that. ;)

But seriously, I work on a large software project myself (the software test bench for the Core i7 processor) so I'm well aware how making one change can quickly leads to "volunteering" to make other changes in seemingly unrelated areas as well.

And all you really wanted was to improve some documentation. Now here you are fixing test systems. Funny where life takes us.
Heh. Thanks for the kind words, S. While I'm very good with Perl, I'm not the "Guru of Gurus". I was once asked by some former colleagues how come I got to move to Europe and they didn't and I replied "many of you are much better programmers than I am, but nobody knows who you are."

Marketing, baby :)

I didn't create Perl or scalars, but if you work in a reasonably up-to-date Perl shop, I can almost guarantee you're running my code.

Software test bench for the Core i7 processor? Damn. That sounds hardcore!

Now back to hacking on Perl 6's testing infrastructure.
Learning Perl was the first task I was given when I got this job (10 years ago!), and it is still ubiquitous. Perl code is pervasive, and knowing how to read and write it is a requirement.

It used to be central to the test process a few years ago, though honestly I'm not sure I'm at liberty to say how. I can say that there were multiple aspects of my job which required Perl.

But now it is simply a language I use to write useful scripts that need a bit more power than CSH gives me. So it's importance has greatly diminished from the heyday. The bulk of my work is in other languages now.
I'd like to help!
I read your post on use perl and I was wondering if you could give some examples of tests that need help. I mean, a lot of them seem to just need a decision made, but I can't just make decisions. What do you think? Should I just join the perl6 mailing list and ask?
Re: I'd like to help!
Hi. I've been on holiday and haven't been able to respond to everything. However, I saw that you hit the Perl 6 language list and they seem to have sorted you out. Thanks for helping. That's awesome :)