" /> Kazuho at Work: April 2008 Archives

« March 2008 | Main | May 2008 »

April 16, 2008

Embedded SQL in Perl

Today I wrote a tiny module Filter::SQL, a module that enables embedding SQL in perl.

    Filter::SQL - embedded SQL for perl

      use Filter::SQL;

      Filter::SQL->dbh(DBI->connect('dbi:...')) or die DBI->errstr;

      EXEC CREATE TABLE t (v int not null);;

      $v = 12345;
      INSERT INTO t (v) VALUES ($v);;
      foreach my $row (SELECT * FROM t;) {
          print "v: $row[0]\n";

      if (SELECT ROW COUNT(*) FROM t; == 1) {
          print "1 row in table\n";

After writing the module, people told me of similar modules like SQL::Preproc. Compared to SQL::Preproc, Filter::SQL does not provide conformance to Embedded-SQL, but may be easy to use for ordinally perl programmers. There are also modules like DBIx::Perlish that seems to tackle the same problem from a bit different direction. Anyway certainly TMTOWTDI, we have the right choose what you like, and I hope Filter::SQL would be a good choice for some of us.