thpfft has asked for the wisdom of the Perl Monks concerning the following question:
quick question: my new Geo::Postcode - name currently subject of debate - comes with sample data in the form of a SQLite file. I can't just distribute the data file, because of binary incompatibilities (endianness, I suppose), so the data is distributed in a csv file and copied into SQLite during installation.
My question: what's the best way to do that, and at what stage?
At the moment I've got the data-writing code in Makefile.PL, wrapped in an eval so that if it fails the process will continue, Makemaker will notice the missing module and CPAN.pm will go off and install the requirements.
Doesn't really work, though. Having gone away to install the requirements, CPAN will not run the Makefile.PL again, so the data file never gets written.
And anyway, as David Cantrell has pointed out, the correct place to write the database file would be between make and make test. I have no idea how to do that. I could just hide it in the first test, of course. but do people always run make test? And anyway it would be underhanded and wrong. Bah.
any help - or good examples to copy - would be much appreciated.
thanks,
will
here's the current Makefile.PL, by the way:
#!/usr/bin/perl -w # -*- perl +-*- use strict; use lib qw( ./lib ); use ExtUtils::MakeMaker; $|++; my $csvdata = './useful/postcodes.csv'; my $datafile = './lib/Geo/Postcode/postcodes.db'; my $tablename = 'postcodes'; create_sqlite_file() unless -e $datafile; WriteMakefile( NAME => 'Geo::Postcode', VERSION_FROM => 'lib/Geo/Postcode.pm', PREREQ_PM => { 'DBD::SQLite' => 0 }, ($] >= 5.005 ? (ABSTRACT_FROM => 'lib/Geo/Postcode.pm', AUTHOR => 'william ro +ss <wross@cpan.org>') : () ), clean => { 'FILES' => $datafile }, ); sub create_sqlite_file { print "Creating sample data file.\n"; my $dbh; eval { use DBI; $dbh = DBI->connect("dbi:SQLite:dbname=$datafile","",""); }; if ($@) { print "Connection failed. Is DBD::SQLite installed? Makemaker +will tell us:\n"; return; } open( INPUT, $csvdata) || die("can't open file $csvdata: $!"); my @cols = split(',',<INPUT>); my $columns = join(', ', map { "$_ varchar(255)" } grep { $_ ne 'p +ostcode' } @cols); $dbh->do("create table $tablename (postcode varchar(12) primary ke +y, $columns);"); my $counter; my $insert = "INSERT INTO $tablename( " . join(',',@cols) . " ) va +lues ( " . join(',', map { '?' } @cols) . ")"; my $sth = $dbh->prepare($insert); while (<INPUT>) { chomp; my @data = split(/,/); $sth->execute( @data ); $counter++; print "." unless $counter % 40; } $sth->finish; $dbh->disconnect; print "done.\n$counter points imported into sample data set.\n\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: cpan module with binary data: when to build?
by Zaxo (Archbishop) on Sep 02, 2004 at 16:01 UTC | |
by thpfft (Chaplain) on Sep 02, 2004 at 20:45 UTC | |
|
Re: cpan module with binary data: when to build?
by Smylers (Pilgrim) on Sep 02, 2004 at 15:59 UTC | |
|
Re: cpan module with binary data: when to build?
by DrHyde (Prior) on Sep 02, 2004 at 15:16 UTC |