Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of Dom2 (2981)

Monday September 18, 2006
02:51 PM

All Journals RSS

Is there a way to get a full text feed of all the use.perl journals? The feed on offer for the main journals page is somewhat limited -- it only has the titles and doesn't link through to the individual articles.

I only ask because up until now, I'd used "friends journals" to keep track of everybody. But I've just bumped into a limit -- only 200 friends are allowed. And it would be far more sensible to just read all new journals...

Tuesday December 07, 2004
07:57 PM

Reference Counts

I've been looking at writing stuff using XML::Genx. Unfortunately, I soon came across a rather nasty little bug. There's an API which lets you pass in a filehandle and it gets all output sent to it. XS has a typemap to help you along with this: T_STDIO. That takes a perl filehandle and gives you a FILE* which you can pass into all the usual C functions.

The trouble is that the typemap doesn't increase the refcount on the perl filehandle. I don't think that it should, because it doesn't know when or where you are likely to need to decrement the refcount.

The net effect is that the filehandle is only valid as long as it's open in Perl. Otherwise it'll get GC'd (and consequently closed) when it goes out of scope. So you have to keep the original filehandle lying around. Surprisingly, I'd managed to do this in all the tests that I had written... Adding a test for this situation provoked a segfault (under Linux, but not FreeBSD for some reason).

Anyway, I'm now faced with two choices.

  1. Rewrite the typemap so that it increments the refcount of the filehandle. The trouble with that is that I then have to decrement that refcount when I get to EndDocument(). And I have nowhere to store the information that I need to do that because my object is a blessed scalar reference.
  2. Alternatively, I deprecate StartDocFile() and make everything use StartDocSender() which makes it easier to wrap things up in.

I don't want to deprecate the interface; it's a useful thing to have. But it seems that the alternative is to create a static HV (not visible to Perl) with a key of "$self" and a value of the filehandle SV (or is that really GV?). Then I can check for that in EndDocument() and decrement it appropriately there. I assume that will work, I have no idea what point Perl runs its GC at.

Four years of pure Perl have really softened me up when it comes to C. :-)

Thursday December 02, 2004
03:57 AM

XML::Genx

After seeing the article on Extending Ruby with C, I thought "I wonder how easy that would be in Perl?" The article is mostly about providing access to the Genx library. So, I spent a little while playing with it and came up XML::Genx. I don't know whether Perl needed another XML writing library; XML::SAX::Writer is pretty good. But Genx has some really useful features.

  • Doesn't require SAX. :-)
  • Will not output non-well-formed XML.
  • Always outputs UTF-8.
  • Ouputs canonical XML, which makes for much easier testing.

Anyway, it's been an interesting exercise. My C skills needed bringing out of storage, although I made few really daft mistakes. XS is still an "interesting" tool to work with. Coding at that level certainly makes you appreciate how high level Perl is.

On the whole though, the most useful surprise has been CPAN Testers. That's a really good way to find out that the world isn't just as FreeBSD faced as it looks like to me. Now all I need to do is figure out how to get a development environment working under windows.

Tuesday March 23, 2004
02:45 PM

Haskell

As a fine example of the kind of Yak Shaving that I tend to indulge in, I've studied a lot of Haskell over the last few days. It's a really rather pleasant functional language. I picked up a book for it a while ago, the first time I thought about looking at Relax NG, when I noticed that James Clark had written examples of the validation algorithm in it.

So naturally, the book has sat on my shelves for the best part of a year with a bookmark about 1.5 chapters in.

It's only now in the last week or so that I've made a concerted effort to pick it up again and go through it. I'm nearly halfway through and enjoying it thoroughly. I've always enjoyed programming in the functional style, even in Perl, but Haskell feels so much nicer (within its realm).

What really caught my eye, though, was the definition of quicksort. I've read descriptions of the algorithm a few times (I'm not a trained Computer Scientist and have no formal training), but never really understood it. The implementation in the book just blew me away with its simplicity:

qSort :: [Int] -> [Int]
qSort [] = []
qSort (x:xs) = qSort [y | y <- xs, y <= x] ++ [x] ++ qSort [y | y <- xs, y > x]
   

It's not the fastest implementation, it's not the most general. But I find it an incredibly clear demonstration of the algorithm. This is my rough translation in Perl.

sub qSort {
    return unless @_;
    my ($x, @xs) = @_;
    return (
        qsort( grep { $_ <= $x } @xs ),
        $x,
        qsort( grep { $_ > $x } @xs ),
    );
}

02:23 PM

And so it begins...

I've been signed up to the very lovely use.perl for quite some time, but never bothered writing much before, aside from the odd comment here and there. I've been blogging for some time at work, mostly as an aide-memoire (I'm the only person who reads it). Combined with commit mails, it's essential for filling in the timesheet. :-)

Anyway, I've recently decided to have a look at Relax NG as a project. I do lots of XML stuff at work and it seemed like an interesting tool to play with. The hope (and it is quite possibly a vain one given my track record) is to produce a validator in Perl. I am going to use the journal as a place to talk about how I go about this. Well, that's the idea anyway. Hopefully, talking about it in public will give me enough embarassement to actually get around to implementing it instead of getting sidetracked into some terribly interesting yak shaving, which is what usually happens to me. Of course, it's nice to have lots of Perl/XML luminaries around too.

More to follow soon...