Leser: 19
2010-06-09T19:03:52 pktm[...]
utf8::is_utf8() schweigt sich beharrlich aus, i.d.S. dass ich keinen Rückgabewert erhalte (nicht einmal undef).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/Perl/bin/perl
use strict;
use warnings;
use Data::Dumper qw/Dumper/;
use utf8;
use Encode qw/decode_utf8 encode_utf8 is_utf8 decode/;
use FindBin qw/$Bin/;
use CGI qw/fatalsToBrowser/;
use CGI::Carp qw/fatalsToBrowser/;
use FileHandle;
use Log::Log4perl qw(:easy);
use Devel::Peek;
#Log::Log4perl->easy_init( { level => INFO, file => ">> $Bin/log/instance_run.log" } );
use open ':encoding(UTF-8)';
use open ':std';
my $q = CGI->new();
my $keyword = $q->param('keyword');
$keyword = decode_utf8( $keyword ) unless is_utf8( $keyword );
die("Missing keyword") unless $keyword;
print $q->header(-charset => 'utf-8');
my $keyword2 = $q->param('keyword');
print $keyword;
print "<br />";
print $keyword2;
print "<br />";
print "ist utf8? -> " . Dumper(utf8::is_utf8($keyword2));
print "<br />";
die($keyword);
Quote�bung
Übung
ist utf8? -> $VAR1 = '';
Software error:
�bung at C:/Apache/cgi-bin/netmed/sc/tools/ajax_gscrape.cgi line 39.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<form method="..." action="..." accept-encoding="UTF-8">
...
use Encode qw(decode);
... decode('UTF-8', $cgi->param('irgendwas'), Encode::FB_CROAK); # diesen Vorgang gerne auch als Subroutine/Wrapper, wie schon angesprochen
__END__
...
=head1 DIAGNOSTICS
Dies with faulty encoding.
=head1 CONFIGURATION AND ENVIRONMENT
Form data must be encoded in C<UTF-8>.
1 2 3 4 5 6 7 8 9
package myCGI; use base 'CGI'; use Encode; sub param { my $self=shift; return Encode::decode('UTF-8', $self->SUPER::param(@_), Encode::FB_CROAK); }
1 2 3 4 5 6 7 8 9
{ use Encode; no warnings 'redefine'; my $param = \*CGI::param; sub CGI::param { return Encode::decode('UTF-8', $param->(@_), Encode::FB_CROAK); } }
CGI->encoding('utf-8');
1
2
3
# dekodieren automatisch:
$cgi->charset('utf-8'); $cgi->param('thing')
charset('utf-8'); param('thing');