I just pushed out another update for the LWP::Simple module for Perl 6. This time, the main work was:
- refactoring the code and adding unit tests for the URL parsing (that might even grow into a Perl 6 URI module
- adding partial basic auth support. To be complete and working, it needs to base64 encode the user/password pair. Not implemented yet. I'll see if I get around to it, or if someone has done it already.
- adding a
getstore()
method, that writes on to disk the downloaded content. Unfortunately that needs to strip the HTTP headers and undestand chunked transfers
for it to be remotely useful.
It was nice to see the module grow in both functionality, code and unit tests coverage. I had to workaround a couple of problems I couldn't understand. I was extremely lazy and I didn't even look up Synopses, so I assume it's my fault. However.
The first is the use of .match()
and ~~
to match against a regular expression. I found that the following code:
my $hostname = 'cosimo:eelst@faveclub.eelst.com';
if $hostname.match('^cosimo') {
# Doesn't enter here
}
doesn't trigger a match. However, this other here:
my $hostname = 'cosimo:eelst@faveclub.eelst.com';
if $hostname ~~ /^cosimo/ {
# Does match
}
And, in the same way, something similarly surprising. The following code correctly matches:
my $hostname = 'cosimo:eelst@faveclub.eelst.com';
if $hostname ~~ /^ .+ : .+ @ .+ $/ {
say '(user:pass@host) matches';
} else {
say '(user:pass@host) does not match';
}
but adding captures makes the same exact regex fail:
my $hostname = 'cosimo:eelst@faveclub.eelst.com';
if $hostname ~~ /^ (.+) : (.+) @ (.+) $/ {
say '(with captures) matches';
} else {
say '(with captures) does not match';
}
There are also very nice things about programming in Perl 6 that are slowly sucking me in this fantastic language. This is part of a test script for LWP::Simple:
#
# Test the parse_url() method
#
use v6;
use Test;
use LWP:: Simple;
my @test = (
{ User-Agent => 'Opera/9.80 (WinNT; 6.0) Version/10.60' },
"User-Agent: Opera/9.80 (WinNT; 6.0) Version/10.60rn",
{ Connection => 'close' },
"Connection: closern",
);
for @test -> %headers, $expected_str {
my $hdr_str = LWP:: Simple.stringify_headers(%headers);
is($hdr_str, $expected_str, 'OK - ' ~ $hdr_str);
}
Note how in the for
statement we can "extract" the hash and string from the @test
array with:
for @test -> %headers, $expected_string {
# Loop body
}
It's not a big deal, other languages have it, but Perl 6 is filled with this small niceties that make the resulting code still feel like Perl, but also, don't know exactly, more robust perhaps?
So, to conclude:
- Anyone with a Perl 6 implementation of MIME::Base64 ? Speak up before I create a monster :)
- Anyone cares enough to take on the chunked transfer encoding support?