Lukas
Lukas

Reputation: 61

perl database data transfer, csv, tsv

am writting little script in perl for a client of mine, which will extract certain data from database and sends an email with this data to client via attachment. Script itself is not an issue, the problem is the file. I thought that standard .csv file will be fine and script is doing what its supposed to do with it without any issues, but clients further software that is processing data needs .tsv file without any delimiters like double quotes. Trouble is that database data contains special characters that they need, like text with new lines and double quotes in them. Once script runs it creates .tsv but special characters mess it up so they need to be escaped somehow, unfortunately i cannot use double quotes for this like in .csv file. Is there any better solution for this?

Thanks.

Upvotes: 2

Views: 892

Answers (1)

creaktive
creaktive

Reputation: 5220

Use Text::CSV configured to use tabs as delimiters:

#!/usr/bin/env perl
use strict;
use warnings qw(all);
use Text::CSV;

my $tsv = Text::CSV->new({
    #quote_char      => undef,
    #escape_char     => undef,
    sep_char        => "\t",
    eol             => "\n",
    quote_space     => 0,
    quote_null      => 0,
});

my @line = ("column1\t", 12345, scalar localtime);
$tsv->print(\*STDOUT, \@line);

Upvotes: 2

Related Questions