Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck with UTF-8 using API in perl

Tags:

perl

Kinda new to Perl. I am using Perl web API to get the data. The error is "application/xml; charset=UTF-8". I used 'use utf8' but doesn't seem to work. The line it gets stuck looks something like this

my @candidates = $c->bookmarks_for(start => 1, tag =>'pubmed');

Can you please help me.

Thank you, Sammed

like image 775
smandape Avatar asked Dec 21 '22 13:12

smandape


1 Answers

The

use utf8;

is only (and only) for one thing. When you will use in your source code utf8 characters. For example:

my $utf8str = "αΩ";

For any other thins, for example:

my $data = MyModule::get_some_data();

you should use

my $utf8data = Encode::decode_utf8($data);

In the case, when your get_some_data returning octets (bytes).

For example, when reading text files, you can tell perl utf8 conversion at IO level with,

open($fd, "<", $filename);
$fd->binmode(:utf8); #for marking data as utf8, or
$fd->binmode(:encoding(utf8)); #for marking *and* checking data for utf8 validity too.

Or you can use the open pragma for telling perl using utf8 as default at IO layer

use open(:std :utf8);

Here is no short answer. You really should read:

  • http://perldoc.perl.org/utf8.html
  • http://perldoc.perl.org/Encode.html
  • http://perldoc.perl.org/perlunicode.html
  • http://perldoc.perl.org/perlunifaq.html
  • http://perldoc.perl.org/perluniintro.html
  • http://perldoc.perl.org/perlunitut.html

Perl is extremely strong in utf8 processing, but you must know, hot to use it properly - unfortunately, here is no shorter way as RTFM...

And note: here are differences in utf8 processing in perl <5.6, 5.6, 5.8, 5.12, 5.14...

like image 171
jm666 Avatar answered Dec 29 '22 15:12

jm666