#!/usr/bin/perl -w use HTML::Template; use CGI::Simple; use DBI; use Devel::Peek; use CGI::Carp qw(fatalsToBrowser carpout); open(LOG, ">/var/www/utftest/log.txt"); carpout(LOG); use Encode qw( encode decode ); use utf8; binmode(STDIN, ":encoding(utf8)"); #Einem bereits geöffneten Kanal kann nachträglich auch ein Konverter zugewiesen werden_ #binmode(STDOUT, ":encoding(UTF-8)"); my $dbh = DBI->connect( 'DBI:mysql:database=testdb' . ';host=localhost', 'root', 'womex', { RaiseError => 1, Print_warn => 1, Warn => 1, mysql_enable_utf8 => 1} ) or die "Can't connect to database!"; #cgi Objekt für Zugriff auf per GET oder POST erhaltene Formulavariablen my $cgi = new CGI::Simple; #Werte aller Formularelemente in Form eines Hashes my %cgiHash = $cgi->Vars(); # open the html template my $template = HTML::Template->new( filename => 'test.tmpl', utf8 => 1 ); $template->param( spalte => $cgiHash{'input'} ); $template->param( spalte2 => encode('utf-8','5 mal Euro: €€€€€') ); #SQl Statement, welches die Daten der Pflichfelder aus dem Formular speichern läßt. my $sth = $dbh->prepare( qq{ INSERT INTO utftbl SET spalte = ? }); #Durchführen des inserts in die Tabelle mit den Pflichtdaten unless ( $sth->execute( $cgiHash{'input'} ) ) { warn sprintf( "[Error]: Reason: [%s].", $dbh->errstr ); } my ($return_value) = $dbh->last_insert_id( undef, undef, 'utftbl', 'id' ); $sth = $dbh->prepare( qq{ SELECT spalte from utftbl where id=$return_value }); #Durchführen des selects in die Tabelle mit den Pflichtdaten unless ( $sth->execute() ) { warn sprintf( "[Error]: Reason: [%s].", $dbh->errstr ); } my @arr = $sth->fetchrow_array(); #Wenn Datenbank String als utf-8 kodierter String eingelesen wurde und #wir nichts an diesem verändert haben, dann können wird diesen direkt ohne #Dekodierung/Enkodierung direkt rausschreiben, falls die Ausgabe auch in utf-8 deklariert ist. $template->param( spalte3 => $arr[0] ); $template->param( spalte4 => encode('utf-8',"Präfix€uro:") . $arr[0] ); print( "Content-Type: text/html; charset=UTF-8\n\n", $template->output ); Dump($cgiHash{'input'}); Dump($arr[0]); Dump($template); __END__ SV = PV(0x84b8930) at 0x8488d28 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x84f4aa8 "Das BU\303\242\302\202\302\254RO"\0 [UTF8 "Das BU\x{e2}\x{82}\x{ac}RO"] CUR = 14 LEN = 16 SV = PV(0x84ca9d0) at 0x8532288 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x851c1f0 "Das BU\303\242\302\202\302\254RO"\0 [UTF8 "Das BU\x{e2}\x{82}\x{ac}RO"] CUR = 14 LEN = 16 SV = RV(0x8180df0) at 0x8450788 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x81cbd78 SV = PVHV(0x8158820) at 0x81cbd78 REFCNT = 1 FLAGS = (PADBUSY,PADMY,OBJECT,SHAREKEYS) IV = 4 NV = 0 STASH = 0x8154744 "HTML::Template" ARRAY = 0x84f4b08 (0:4, 1:4) hash quality = 137.5% KEYS = 4 FILL = 4 MAX = 7 RITER = -1 EITER = 0x0 Elt "param_map" HASH = 0xfaf3dd80 SV = RV(0x8180d24) at 0x852a7f0 REFCNT = 1 FLAGS = (ROK) RV = 0x852a7a8 SV = PVHV(0x85510b0) at 0x852a7a8 REFCNT = 1 FLAGS = (SHAREKEYS,HASKFLAGS) UV = 4 NV = 0 ARRAY = 0x8517b40 (0:5, 1:2, 2:1) hash quality = 91.7% KEYS = 4 FILL = 3 MAX = 7 RITER = -1 EITER = 0x0 Elt "spalte4" [UTF8 "spalte4"] HASH = 0xcf2c47e3 SV = RV(0x8180e48) at 0x8532078 REFCNT = 1 FLAGS = (ROK) RV = 0x8532048 Elt "parse_stack" HASH = 0x4c50ad1 SV = RV(0x8180d1c) at 0x852a808 REFCNT = 1 FLAGS = (ROK) RV = 0x8531b44 SV = PVAV(0x8487eb4) at 0x8531b44 REFCNT = 1 FLAGS = () IV = 0 NV = 0 ARRAY = 0x8361280 FILL = 8 MAX = 11 ARYLEN = 0x852a700 FLAGS = (REAL) Elt No. 0 SV = RV(0x8180e24) at 0x852a5f8 REFCNT = 1 FLAGS = (ROK) RV = 0x821092c Elt No. 1 SV = RV(0x8180e2c) at 0x8531d18 REFCNT = 1 FLAGS = (ROK) RV = 0x8256718 Elt No. 2 SV = RV(0x8180e34) at 0x8531d48 REFCNT = 1 FLAGS = (ROK) RV = 0x8220084 Elt No. 3 SV = RV(0x8180e38) at 0x8531e08 REFCNT = 1 FLAGS = (ROK) RV = 0x8531d00 Elt "options" HASH = 0xa12ff3a2 SV = RV(0x8180c50) at 0x8488d1c REFCNT = 1 FLAGS = (ROK) RV = 0x8488e24 SV = PVHV(0x84d8568) at 0x8488e24 REFCNT = 1 FLAGS = (SHAREKEYS,HASKFLAGS) UV = 33 NV = 0 ARRAY = 0x851a8c0 (0:37, 1:22, 2:4, 3:1) hash quality = 105.3% KEYS = 33 FILL = 27 MAX = 63 RITER = -1 EITER = 0x0 Elt "cache_debug" HASH = 0x3cb23b45 SV = IV(0x8480070) at 0x848e8a8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 0