#!/usr/bin/perl -w #use HTML::Template; use HTML::Template::Compiled; 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, PrintWarn => 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; #Codierung der Daten auf UTF-8 setzen: --> trotzdem werden diese nicht automatisch dekodiert (siehe Dump) $cgi->charset('utf-8'); #Werte aller Formularelemente in Form eines Hashes my %cgiHash = $cgi->Vars(); #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 ); } #Wiederauslesen des zuletzt eingefügten Eintrags aus der DB Tabelle 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(); ########################## F A Z I T ########################################## #Mit dem original HTML::Template klappt dieses Vorgehen allerdings nicht. # open the html template #my $template = HTML::Template->new( filename => 'test.tmpl', utf8 => 1 ); ###################################### Fazit ################################## # open the html template my $template = HTML::Template::Compiled->new( filename => 'test.tmpl', open_mode => ':utf8' ); #Füllen des Templates mit den Daten verschiedener Herkunft # #hier muss ich trotzdem manuell dekodieren $template->param( spalte => decode('utf-8',$cgiHash{'input'}) ); $template->param( spalte2 => '5 mal Euro: €€€€€' ); $template->param( spalte3 => $arr[0] ); $template->param( spalte4 => "Präfix€uro:" . $arr[0] ); #Ausgeben an den Server print( "Content-Type: text/html; charset=UTF-8\n\n", $template->output ); #Dump von Rohdaten Dump($cgiHash{'input'}); Dump($arr[0]); foreach($template->param()){ Dump($_); } Dump($template); __END__ SV = PV(0x85ed1b8) at 0x85ca900 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x862a8a8 "Das BU\342\202\254RO"\0 CUR = 11 LEN = 12 SV = PV(0x85de08c) at 0x8674c04 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x86720e0 "Das BU\342\202\254RO"\0 [UTF8 "Das BU\x{20ac}RO"] CUR = 11 LEN = 12 [Wed Aug 3 14:25:40 2011] form.cgi: You are using query() but have not specified that you want to use it (specify with use_query => 1) at /var/www/utftest/form.cgi line 111 SV = RV(0x817c3d0) at 0x84efe74 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x85ca8d0 SV = PVAV(0x85bc738) at 0x85ca8d0 REFCNT = 1 FLAGS = (OBJECT) IV = 0 NV = 0 STASH = 0x8154768 "HTML::Template::Compiled" ARRAY = 0x862b078 FILL = 30 MAX = 59 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = RV(0x817c3e8) at 0x85ca9d8 REFCNT = 1 FLAGS = (ROK) RV = 0x85ca840 SV = PVHV(0x8681a38) at 0x85ca840 REFCNT = 1 FLAGS = (SHAREKEYS,HASKFLAGS) UV = 4 NV = 0 ARRAY = 0x8667c58 (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 = PV(0x8665f54) at 0x85cabb8 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x8632668 "Pr\303\244fix\342\202\254uro:Das BU\342\202\254RO"\0 [UTF8 "Pr\x{e4}fix\x{20ac}uro:Das BU\x{20ac}RO"] CUR = 25 LEN = 28 Elt No. 1 SV = PVNV(0x85d08b0) at 0x85ca87c REFCNT = 1 FLAGS = (ROK) IV = 0 NV = 0 RV = 0x85ca870 SV = PVAV(0x85bc764) at 0x85ca870 REFCNT = 2 FLAGS = () IV = 0 NV = 0 ARRAY = 0x0 FILL = -1 MAX = -1 ARYLEN = 0x0 FLAGS = (REAL) PV = 0x85ca870 "" CUR = 0 LEN = 0 Elt No. 2 SV = PV(0x85ed254) at 0x85ca864 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x862ac90 "d41d8cd98f00b204e9800998ecf8427e"\0 CUR = 32 LEN = 36 Elt No. 3 SV = PV(0x85ed248) at 0x85ca8c4 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x862ab80 "test.tmpl"\0 CUR = 9 LEN = 12