Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Help for "Cannot decode string with wide characters..." and

by PerlBroker (Acolyte)
on Apr 08, 2012 at 10:32 UTC ( #963988=perlquestion: print w/replies, xml ) Need Help??

PerlBroker has asked for the wisdom of the Perl Monks concerning the following question:

I have this error, and although, once this script worked, I cannot myself figure out what to do:
Cannot decode string with wide characters at /usr/lib/perl/5.10/Encode line 174.
My input is UTF-8 from the HTML form, and the script is roughly this one:
#!/usr/bin/perl use strict; use warnings; use utf8; use CGI qw(-utf8); use Encode; use encoding 'utf8'; use CGI::Carp qw(croak fatalsToBrowser); # etc... $| = 1; binmode(STDOUT, ":utf8"); binmode(STDIN, ":utf8"); my $q = new CGI; croak("It works yet..."); my $referer_url = "@{[ $q->url ]}"; croak("Cannot decode string... error before this line.");
In general, if a value from the HTML textform contains one UTF-8 character, I get that error. Any help is appreciated.

Replies are listed 'Best First'.
Re: Help for "Cannot decode string with wide characters..." and
by moritz (Cardinal) on Apr 08, 2012 at 16:09 UTC
    When you use utf8;, then your string literals are text strings. But this
    my $referer_url = "@{[ $q->url ]}";

    tries to interpolate an URL, and that seems to be a byte string. Simply mixing those two is a very bad idea.

    I don't see why you do it in this complicated way anyway. What's wrong with

    my $url = $q->url;

    or maybe

    my $url = decode_utf8 $q->url;

    depending on how you want to use it.

Re: Help for "Cannot decode string with wide characters..." and
by Anonymous Monk on Apr 08, 2012 at 11:04 UTC

    upgrade Encode CGI Encoding   cpanp -i Encode CGI Encoding

      After upgrade of Encode and CGI, it started working, at least there is no error. But now I get these characters in messages: ��
        Maybe something like this:
        #!/usr/bin/perl -l BEGIN { $| = 1;} use strict; use warnings; use utf8; use Encode; use CGI qw/:standard -utf8/; use CGI::Carp qw/fatalsToBrowser set_message/; $CGI::PARAM_UTF8 = 1; BEGIN { sub handle_errors { my $msg = shift; print "<h1>There's a problem</h1>"; print "<p>Cannot decode string: $msg</p>"; } set_message(\&handle_errors); } my $q = CGI->new; binmode STDOUT, ":encoding(UTF-8)"; my $referer_url = "@{[ $q->url ];}"; print $referer_url;
Re: Help for "Cannot decode string with wide characters..." and
by ikegami (Pope) on Apr 10, 2012 at 00:34 UTC

    Get rid of

    binmode(STDIN, ":utf8");

    Your corrupting the data stream passed to CGI. The proper way to decode the parameters is using

    use CGI qw(-utf8);

    which you already use. Speaking of doing the same thing twice, you did *that* twice too! First you tell Perl the source code is UTF-8

    use utf8;

    then you tell it again using a buggy version of the first method.

    use encoding 'utf8';

    The encoding is UTF-8, by the way. utf8 is something else.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963988]
Approved by lidden
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-07-06 02:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found