Archive for March, 2005

traversing directory trees in perl

Thursday, March 24th, 2005

I spent a whole day trying to traverse a directory tree using Perl, and grab all the directories that met certain criteria, and put them in an array. It sounds relatively simple, but it isn’t, and I got pretty frustrated. Finally I went to Google and typed in “perl traverse directory tree”. The first link that came up took me here. Lo and behold there is already a Perl module that will do it. The link has the info, but I still had to figure out how to use it for my own purposes. Here is some sample code that puts it into practice:

use File::Find;

sub traverse {
$options{‘wanted’} = &wanted; # this option specifies a subroutine to process the results.
$options{‘no_chdir’} = 1; # this option tells the module to not change the current default directory.
# there are many more options described in the link above.
find(%options, @accounts); # this command traverses the directories listed in the @accounts array.
# now you have a list of all the traversed directories, do what you want
foreach $dir (@directories) {
&doDirectory ($dir);

sub wanted {
local($file) = $File::Find::name;
if ( $file =~ /^\\./ ) { return; } # get rid of files or directories that begin with a period.
if ( (-d $File::Find::name) ) { # just grab directories, not files.
push @directories, $File::Find::name; # put them in an array.

That’s all there is to it.

bug in Sqlite

Friday, March 18th, 2005

I highly recommend the marvelous, free, relational database engine Sqlite. It is not only free of a commercial license, it is also free of the restrictions imposed by the Free Software Foundation GNU GPL license, i.e., you can do whatever you want with it, incorporate it into your application, whether your app is free, or, dare I say it, costs money. However there is a bug, which I discovered recently. The ISNULL() function doesn’t work the way it is supposed to, or the way you would expect it to. If you have a field in your database file that may be NULL, and you want to do a select statement that substitutes some string (‘blah’) in the case where the field is NULL, you would expect to be able to do something like this:

SELECT field1,field2,ISNULL(‘blah’,field3) FROM tablename;

But that won’t work, because of the bug. Instead say this:

SELECT field1,field2,(CASE WHEN field3 ISNULL THEN ‘blah’ ELSE field3 END) FROM tablename;

And it will work exactly the way you expect it to. If field3 is NULL it will substitute the string ‘blah’. If field 3 is not NULL, it will select the contents of field3.