Dom2's Journal Dom2's use Perl Journal en-us use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners. 2012-01-25T02:07:22+00:00 pudge Technology hourly 1 1970-01-01T00:00+00:00 Dom2's Journal All Journals RSS <p>Is there a way to get a full text feed of <em>all</em> 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.</p><p>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...</p> Dom2 2006-09-18T19:51:04+00:00 journal Reference Counts <p>I've been looking at writing stuff using <a href="">XML::Genx</a>. 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: <a href="">T_STDIO</a>. That takes a perl filehandle and gives you a FILE* which you can pass into all the usual C functions.</p><p>The trouble is that the typemap doesn't increase the refcount on the perl filehandle. I don't think that it <em>should</em>, because it doesn't know when or where you are likely to need to decrement the refcount.</p><p>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).</p><p>Anyway, I'm now faced with two choices.</p><ol> <li>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 <code>EndDocument()</code>. And I have nowhere to store the information that I need to do that because my object is a blessed scalar reference.</li><li>Alternatively, I deprecate <code>StartDocFile()</code> and make everything use <code>StartDocSender()</code> which makes it easier to wrap things up in.</li></ol><p>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 <code>EndDocument()</code> and decrement it appropriately there. I assume that will work, I have no idea what point Perl runs its GC at.</p><p>Four years of pure Perl have really softened me up when it comes to C.<nobr> <wbr></nobr>:-)</p> Dom2 2004-12-08T00:57:01+00:00 journal XML::Genx <p>After seeing the article on <a href="">Extending Ruby with C</a>, I thought "I wonder how easy that would be in Perl?" The article is mostly about providing access to the <a href="">Genx</a> library. So, I spent a little while playing with it and came up <a href="">XML::Genx</a>. I don't know whether Perl <em>needed</em> another XML writing library; <a href="">XML::SAX::Writer</a> is pretty good. But Genx has some really useful features.</p><ul> <li>Doesn't require SAX.<nobr> <wbr></nobr>:-)</li><li>Will not output non-well-formed XML.</li><li>Always outputs UTF-8.</li><li>Ouputs <a href="">canonical XML</a>, which makes for much easier testing.</li></ul><p>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.</p><p>On the whole though, the most useful surprise has been <a href="">CPAN Testers</a>. 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 <a href="">windows</a>.</p> Dom2 2004-12-02T08:57:23+00:00 journal Haskell <p>As a fine example of the kind of <a href="">Yak Shaving</a> that I tend to indulge in, I've studied a lot of <a href="">Haskell</a> over the last few days. It's a really rather pleasant functional language. I picked up a <a href="">book</a> for it a while ago, the first time I thought about looking at Relax NG, when I noticed that <a href="">James Clark</a> had written <a href="">examples</a> of the validation algorithm in it.</p><p>So naturally, the book has sat on my shelves for the best part of a year with a bookmark about 1.5 chapters in.</p><p>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).</p><p>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:</p><blockquote><div><p> <tt>qSort<nobr> <wbr></nobr>:: [Int] -&gt; [Int]<br>qSort [] = []<br>qSort (x:xs) = qSort [y | y &lt;- xs, y &lt;= x] ++ [x] ++ qSort [y | y &lt;- xs, y &gt; x]<br>&nbsp; &nbsp; </tt></p></div> </blockquote><p>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.</p><blockquote><div><p> <tt>sub qSort {<br>&nbsp; &nbsp; return unless @_;<br>&nbsp; &nbsp; my ($x, @xs) = @_;<br>&nbsp; &nbsp; return (<br>&nbsp; &nbsp; &nbsp; &nbsp; qsort( grep { $_ &lt;= $x } @xs ),<br>&nbsp; &nbsp; &nbsp; &nbsp; $x,<br>&nbsp; &nbsp; &nbsp; &nbsp; qsort( grep { $_ &gt; $x } @xs ),<br>&nbsp; &nbsp; );<br>}</tt></p></div> </blockquote> Dom2 2004-03-23T19:45:05+00:00 journal 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.<nobr> <wbr></nobr>:-) <p>Anyway, I've recently decided to have a look at <a href="">Relax NG</a> 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 <a href="/~Matts">Perl</a>/<a href="/~darobin">XML</a> luminaries around too. </p><p>More to follow soon...</p> Dom2 2004-03-23T19:23:52+00:00 journal