3455d4cc1aecb3a7d37b416b122f9fbf5dcf2181
[migration-tools.git] / dbf-to-tsv.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use XBase;  # or could use DBI and DBD::XBase;
7 use Data::Dumper;
8 use Getopt::Long;
9 use Encode;
10
11 my $in = '';
12 my $out = '';
13 GetOptions('in=s' => \$in, 'out=s' => \$out);
14
15 open OUT, ">$out" or die $!;
16
17 my $table = new XBase $in or die XBase->errstr;
18
19 # get list of field names
20 my @names = $table->field_names;
21
22 # dump PATRONID, SURNAME, FIRSTNAME
23 print OUT join ("\t", @names) . "\n";
24
25 sub clean {
26   if ( $_ ) { 
27     s/\\/\\\\/g;
28     s/\n/\\n/g; 
29     s/\r/\\r/g; 
30     s/\t/\\t/g; 
31     Encode::encode("utf8", $_) 
32   } else { ''; } # to avoid 'Use of uninitialized value in join'
33 }
34
35 my $i = 0;
36 for (0 .. $table->last_record) {
37     $i++;
38     my ($deleted, @row) = $table->get_record($_);
39     @row = map (&clean, @row); 
40     print OUT join("\t", @row) . "\n" unless $deleted;
41
42 }
43
44 print STDERR "$i records exported to $out.\n";