How can I use Unicode characters when I write to Perl's format?
email, format, perl, unicode
Solution
I have never had the desire to learn about formats. This is a bad answer because I am unable to offer any insight into your problem and/or potential solutions, but others have already done that. I am going to offer two suggestions for replacements.
The first one, `Perl6::Form` ought to be useful as a better `format` although I had never used it until I put together this example today. On the other hand, I have used `Text::Table` and it is very useful for creating tables in plain text (most of the time, I just generate HTML, but email is still one of those places where plain text is plainly better).
`Perl6::Form` example:
#!/usr/bin/perl
use strict;
use warnings;
use Perl6::Form;
my @data = (
['127.0.0.1', 'Johnny Smithey', 'JLNSJIV', 14, 5],
['127.0.0.2', 'Ömer Seyfettin Şınas', 'OSS3', 25, 5],
);
for my $data_ref ( @data ) {
print format_data($data_ref);
}
sub format_data {
my ($data) = @_;
return form
'{<<<<<<<<<<<<<<<} {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<} ' .
'{<<<<<<<<<<} {<<<<<<<<<<<<<<} {>>}',
@$data;
}
`Text::Table` example:
#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;
my %common_options = (
align => 'left',
title_align => 'center',
);
my $sep = \' ';
my $table = Text::Table->new(
{
title => 'IP Address',
sample => '<' x 15,
%common_options,
},
$sep,
{
title => 'Full Name',
sample => '<' x 34,
%common_options,
},
$sep,
{
title => 'RID',
sample => '<' x 10,
%common_options,
},
$sep,
{
title => 'Since',
sample => '<' x 14,
%common_options,
},
$sep,
{
title => 'Times',
sample => '>' x 2,
align => 'right',
title_align => 'center'
},
);
$table->rule('');
$table->load(
['127.0.0.1', 'Johnny Smith-Jones', 'JLNSJIV', '20090814010203', 5],
['127.0.0.2', 'Ömer Seyfettin Şınas', 'OSS3', '20071211101112', 3],
['192.168.172.144', 'Jane Doe', 'JD156', '20080101010101', 1],
);
print $table->table;
Problem
Basically I have a database where I get `$lastname`, `$firstname`, `$rid`, `$since`, `$times` and `$ip` from. Using a Perl script, I format the data to send it via e-mail. Since the `$lastname` and `$firstname` can contain special chars (for instance ä, ü, ß, é,...) I first decode the strings. ``` my $fullname = decode("utf8", $lastname) . ', ' . decode("utf8", $firstname); my $send = swrite(<<'END', $ip, $fullname, $rid, $since, $times); @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<<<<< @>>END ``` Without `decode`, the special chars are garbage (ä becomes À) and the rest is OK. With `decode`, everything is fine except the lines with name containing special chars have a couple of `<` too many. Why is that? And how do I remove them? Edit: `swrite` is from `perldoc perlform` ``` sub swrite { my $format = shift; $^A = ''; formline($format, @_); return $^A; } ``` Edit2: The problem is not the terminal nor STDOUT. I use: ``` use Mail::Sender; use vars qw($sender); #... $sender->MailMsg({to => $mailto, cc=> "", bcc => "", subject => "subject", msg => $send}); ``` And the characters are badly shown when receiving the email. Edit 3: The data I get is already scrambled. I get 'À' instead of 'ä' and that's why my format fails, because the number of chars decreases when using decode.