In ASCII, a character always maps to a single byte.
In UTF-8, a character may map to multiple bytes.
For Perl to know whether the data it receives from an external source (which could be a string, or binary data such as an image) as a string of bytes or as a UTF-8 string, it uses the internal UTF8 flag.
Nothing external to Perl (eg the console or the database) knows about this flag, so we need to transform all input/output data into a form that each program understands, which we do using Encode.
To convert an input string of bytes which represents a UTF-8 string, into Perl's internal string format, we DECODE the byes
tofrom UTF-8, using Encode::decode() or Encode::decode_utf8()To convert a Perl string into a string of bytes representing a UTF-8 for other programs to understand, we ENCODE the string using Encode::encode() or Encode::encode_utf8()
An example round trip
The steps below demonstrate how to accept UTF-8 strings from outside, store them in MySQL, retrieve them from the database, and re-output them
Manual, or with Automatic with PerlIO layers MySQL::dbd > v 4 --------------- ---------------- --------- --------- ----- +----- ¦ UTF-8 ¦ ---decode_utf8()->> ¦ Perl ¦ ---encode_utf8()->> ¦ UTF +-8 ¦ ¦ Console ¦ <<-encode_utf8()--- ¦ strings ¦ <<-decode_utf8()--- ¦ MySQ +L DB ¦ --------- --------- ----- +-----
Create a table in MySQL which uses the UTF-8 character set:
This step ensures that all UTF-8 aware programs that interact with this database know to treat the stored data as UTF-8
CREATE TABLE test_db.test ( string VARCHAR(50) ) CHARACTER SET utf +8;Get a UTF-8 string:
This step accepts a string of bytes representing a UTF-8 string, and converts them into Perl's internal string format.
-
From a UTF-8 console:
use Encode qw( decode_utf8 ); my $string = <>; my $utf8_string = decode_utf8($string);
-
or, from an ISO-8859-1 console:
use Encode qw( decode ); my $iso_8859_string = <>; my $utf8_string = decode('ISO-8859-1',$iso_8859_string);
- or from within a Perl script:
use utf8; # Tells Perl that the script itself is written i +n UTF-8 my $utf8_string = "UTF-8 string with special chars: ñ æ ô";
-
From a UTF-8 console:
Open a UTF-8 enabled database connection:
This step connects to the database, and tells DBD::mysql to auto-convert to/from UTF-8.
IMPORTANT: This requires a version of DBD::mysql greater than version 4
use DBI(); my $dbh = DBI->connect ('dbi:mysql:test_db', $username, $password, {mysql_enable_utf8 => 1} );Write to and read from the DB:
$dbh->do('INSERT INTO test_db.test VALUES(?)', $utf8_string); $dbh->do('SELECT string FROM test_db.test LIMIT 1'); my $new_string = $dbh->fetchrow_arrayref->[0];Display the retrieved string:
The output data needs to be converted from Perl's internal format into a string of bytes that the console will understand.
- on a UTF-8 console:
use Encode qw( encode_utf8 ); print Encode::encode_utf8($new_string); OR # Add an auto-encoding layer binmode (STDIN,':utf8'); print $new_string;
-
or, on an ISO-8859-1 console:
use Encode qw( encode ); print Encode::encode('ISO-8859-1', $new_string);
- on a UTF-8 console:
UPDATE - Added readmore tags. Added diagram illustrating round trip
UPDATE - Corrected a type: TO utf8, not from utf8
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A UTF8 round trip with MySQL
by Joost (Canon) on Jun 13, 2007 at 02:22 UTC | |
by clinton (Priest) on Jun 13, 2007 at 08:26 UTC | |
by Joost (Canon) on Jun 13, 2007 at 10:41 UTC | |
by clinton (Priest) on Jun 13, 2007 at 11:01 UTC | |
by Joost (Canon) on Jun 13, 2007 at 11:28 UTC | |
| |
by Anonymous Monk on Jul 25, 2013 at 13:34 UTC | |
Re: A UTF8 round trip with MySQL
by Juerd (Abbot) on Jun 13, 2007 at 19:49 UTC | |
by clinton (Priest) on Jun 13, 2007 at 20:13 UTC | |
by Juerd (Abbot) on Jun 13, 2007 at 20:45 UTC | |
by Anonymous Monk on Jul 30, 2013 at 13:28 UTC | |
by Joost (Canon) on Jun 13, 2007 at 20:34 UTC | |
by Juerd (Abbot) on Jun 13, 2007 at 20:52 UTC | |
by Joost (Canon) on Jun 13, 2007 at 21:01 UTC | |
| |
by mje (Curate) on Mar 31, 2009 at 10:24 UTC | |
Re: A UTF8 round trip with MySQL
by Burak (Chaplain) on Jun 27, 2007 at 22:42 UTC | |
by clinton (Priest) on Jun 28, 2007 at 07:53 UTC | |
by Burak (Chaplain) on Jun 28, 2007 at 21:20 UTC | |
by Joost (Canon) on Jun 28, 2007 at 18:01 UTC | |
by Burak (Chaplain) on Jun 28, 2007 at 21:26 UTC | |
by Joost (Canon) on Jun 28, 2007 at 21:33 UTC | |
by Anonymous Monk on Jul 30, 2013 at 13:30 UTC | |
by Anonymous Monk on Mar 30, 2009 at 18:49 UTC |