Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text::CSV::Unicode not processing CSV string?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm having major problems with Perl and wide characters, specifically the GBP (£) pound sign.

If I create my own CSV string and output it to the browser, when I open the CSV file, there is a funny char displayed with the pound sign (£).

So I am trying to create a Unicode CSV string using the Text::CSV::Unicode module, only it won't parse my CSV string.

The line it says it doesn't like is...
"Axxxx xxxx xxxx Ltd","Alan Smith","Carl Jones","Legal & General","30/07/2009","524.89","22/07/2011","306.09","24/08/2011","Protection","Reinstatement £306.09
13/12/11 C/B £306.09"

which is found using
Code:
$csv->error_input

I can only assume it doesn't like the carriage return in the last encapsulated column value, but it should as I have created the object using
Code:
my $csv = Text::CSV::Unicode->new([{ binary => 1 }]);

And the CPAN docs say
new $csv = Text::CSV::Unicode->new( [{ binary => 1 }] );

This function may be called as a class or an object method. It returns a reference to a newly created Text::CSV::Unicode object. binary => 0 allows the same ASCII input as Text::CSV and all other input, while binary => 1 allows for all Unicode characters in the input (including \r and \n),

So there should be no problem with either \r or \n in any of the fields in my input array?

Any ideas how I fix this pound sign problem or get the Text::CSV::Unicode to accept my input?

Thanks,
1DMF


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Well I don't know if it's a typo in the docs or if the square brackets are meant to imply 'optional' as there is another example that uses..
Code:
 $csv = Text::CSV::Unicode->new( { binary => 1 } );

Which works, only when the XLS file is opened, there is still a funny character in front of the pound sign!

So Text::CSV::Unicode has made no difference?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
It's OK, found the culprit.

It was the IO::File output of the string that was corrupting the data!

Code:
# create an IO::File for Catalyst
    use IO::File;
    my $iof = IO::File->new;
    $iof->open(\$xls, "r");
    [b][COLOR=#EF2929]$iof->binmode(":encoding(UTF-8)"); [/color][/b]

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

Once I added the binmode with a UTF-8 encoding layer, it now outputs pound signs correctly :)

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top