May 2005 Archives

Ruby on Rails on MySql

A couple of small problems. For the first one, a fix from LaughingMeme. When I tried to set up my first scaffold, I got a message complaining about

No such file or directory - /tmp/mysql.sock

I needed to edit config/database.yml to change the host to (or, equivalently, localhost.localdomain).

The second error was easier:

Access denied for user 'root'@'localhost.localdomain' (using password: NO)

I just had to specify my own username in database.yml instead of leaving the field empty. (I expected it to default to my username but for some reason it wanted to default to root.)

On the Theme of Unexpected Behaviour...

| No Comments

I was processing a file with UTF-8 text and finding that my output was coming out in ISO-8859-1 (a.k.a. latin1). I verified this first using od -c | less and then by running recode latin1..utf8. Either Perl, XML::Parser or my code was silently converting the text.

It turned out that Perl was responsible this time. Or at least, the fix was at the Perl level. Again, the answer is in the friendly man pages, specifically in perluniintro. There are three alternatives:

  1. open FH, ">:utf8", "file";
  2. binmode(STDOUT, ":utf8");
  3. use open ’:utf8’; # open as a pragma

That makes not one but two cases recently solution was in the man pages. It helps that these are new style man pages, with lots of tasty example code.

Least Helpful Error Message Ever

| No Comments

XML::Parser is useful for testing XML output, mainly because it's in the standard Perl distribution and it's stable. However, I ran into a problem with it yesterday that cost me some time.

Here's some (simplified) code

$parser = new XML::Parser(Style => 'Tree');

Those who know the module well will have spotted the error already. I was faced with this error message:

not well-formed (invalid token) at line 1, column 0, byte 0 at /usr/lib/perl5/[...]/XML/ line 187

I tried making my XML output smaller and smaller until it was just one self-closing element. Still I was getting the invalid token error.

Turned out I was calling the wrong method on the parser. A lot of Perl methods are pretty liberal about what they accept, perhaps too liberal at times. $parser->parse accepts a file handle or a string, but not a file name. My fault for not reading the documentation carefully, because it says, right at the top, $p1->parsefile('REC-xml-19980210.xml').

Perhaps not literally the least helpful error message ever. Certainly misleading for anyone unfortunate enough to run into it.