To follow up having looked at perldoc http://perldoc.perl.org/utf8.html ....

I have just tried...
-----------------

        # encode UTF8
        use utf8;
        utf8::encode($xls);

        # output header
        $c->response->content_type('application/vnd.ms-excel');
        $c->response->content_length(length($xls));
        $c->response->header(Content_Disposition => 
'attachment;filename=NBCS_Export.csv');

        # create an IO::File for Catalyst
        use IO::File;
        my $iof = IO::File->new;

        $iof->open(\$xls, "r");
        $iof->binmode(":encoding(UTF-8)");

        # output XLS data
        $c->response->body($iof);

        # close file
        undef $iof;

---------------------------------------

And still get exactly the same error...

[error] Caught exception in engine "Wide character in syswrite at 
C:/Perl/lib/IO/Handle.pm line 474."

Do I need to use something else to encode to utf8?

Thanks,
Craig.

-----Original Message-----
From: Craig Chant [mailto:cr...@homeloanpartnership.com]
Sent: 02 July 2013 10:38
To: The elegant MVC web framework
Subject: RE: [Catalyst] CSV / UTF-8 / Unicode

Hi Lars,

Thanks for the reply....

>> The problem is rather that Catalyst::Response::body wants octets

Sorry, you've lost me, I thought response body wanted an IO::File Handle which 
is what I have given it?

I was also under the impression that $c->response->body( 'Page not found' ); 
was acceptable and that is also a char string not octet?

I seem to be missing something here, please can you elaborate.

>> encode the content again for output as UTF-8

Isn't that what $iof->binmode(":encoding(UTF-8)");  does?

I thought all strings in perl were Unicode ? or at least that's what the 
Unicode::Encoding plugin is for isnt' it?

I have in my app ....

use Catalyst qw/
    Unicode::Encoding
/;

What's the purpose of this Catgalyst plugin if it isn't making my app Unicode?

How do I encode a string to unicode if it isn't already?

>         # close file
>         undef $iof;
>> That's not necessary. When the reference falls out of scope, the destructor 
>> that closes the file is called automatically.

Does it do any harm? Is there an issue with cleaning up your own object 
references?

I thought it was good practice to clean up your own references , even if auto 
garbage collection *might* do it for you? It's a habit I have from VBA / 
VB.NET, am I doing something bad?

>> PS: IO::File does not throw exceptions when the file could not be read.
>> Instead I recommend to use the `open` built-in in conjuction

Please note there is no file to be read , this is utilising Perl 5+'s ability 
to use a string as a file handle.

There will always be a string, even if it is empty so the exported XLS will be 
a blank spread sheet.

I think all I need to do is understand how I encode the string before giving it 
to IO::File

Your help is appreciated.

Regards,
Craig.



-----Original Message-----
From: Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 [mailto:da...@cpan.org]
Sent: 02 July 2013 10:06
To: catalyst@lists.scsys.co.uk
Subject: Re: [Catalyst] CSV / UTF-8 / Unicode

> I seem to be having major problems getting the application devel
> server (my_app_server.pl) to accept an IO::File Handle as CSV and
> correctly encode as UTF-8.
No, the code does exactly what you said. The problem is rather that 
Catalyst::Response::body wants octets, not characters. Either pass the handle 
through in raw mode, or encode the content again for output as UTF-8. If the 
input file is not under your control, I recommend going through a manual 
decoding/encoding step instead of IO layers, and make decoding errors fatal 
(see FB_CROAK in <http://p3rl.org/Encode>) in order to catch malformed encoding 
before it's delivered to the client.
One can't be too careful.

>         # close file
>         undef $iof;
That's not necessary. When the reference falls out of scope, the destructor 
that closes the file is called automatically.

PS: IO::File does not throw exceptions when the file could not be read.
Instead I recommend to use the `open` built-in in conjuction with 
<http://p3rl.org/autodie> for better error checking.
This Email and any attachments contain confidential information and is intended 
solely for the individual to whom it is addressed. If this Email has been 
misdirected, please notify the author as soon as possible. If you are not the 
intended recipient you must not disclose, distribute, copy, print or rely on 
any of the information contained, and all copies must be deleted immediately. 
Whilst we take reasonable steps to try to identify any software viruses, any 
attachments to this e-mail may nevertheless contain viruses, which our 
anti-virus software has failed to identify. You should therefore carry out your 
own anti-virus checks before opening any documents. HomeLoan Partnership will 
not accept any liability for damage caused by computer viruses emanating from 
any attachment or other document supplied with this e-mail. HomeLoan 
Partnership reserves the right to monitor and archive all e-mail communications 
through its network. No representative or employee of HomeLoan Partnership has 
the authority to enter into any contract on behalf of HomeLoan Partnership by 
email. HomeLoan Partnership is a trading name of H L Partnership Limited, 
registered in England and Wales with Registration Number 5011722. Registered 
office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is 
authorised and regulated by the Financial Conduct Authority.
This Email and any attachments contain confidential information and is intended 
solely for the individual to whom it is addressed. If this Email has been 
misdirected, please notify the author as soon as possible. If you are not the 
intended recipient you must not disclose, distribute, copy, print or rely on 
any of the information contained, and all copies must be deleted immediately. 
Whilst we take reasonable steps to try to identify any software viruses, any 
attachments to this e-mail may nevertheless contain viruses, which our 
anti-virus software has failed to identify. You should therefore carry out your 
own anti-virus checks before opening any documents. HomeLoan Partnership will 
not accept any liability for damage caused by computer viruses emanating from 
any attachment or other document supplied with this e-mail. HomeLoan 
Partnership reserves the right to monitor and archive all e-mail communications 
through its network. No representative or employee of HomeLoan Partnership has 
the authority to enter into any contract on behalf of HomeLoan Partnership by 
email. HomeLoan Partnership is a trading name of H L Partnership Limited, 
registered in England and Wales with Registration Number 5011722. Registered 
office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is 
authorised and regulated by the Financial Conduct Authority.
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to