HTML::...Unicode(3pm)
π HTML::...Unicode β the perl doc you didn't know you needed π
okay bestie, gather round, because we're talking about HTML::...Unicode (that's the Unicode.pod page, if we're being official) and honestly? it's giving "short doc, huge glow-up". no cap.
β¨ VERSION β¨
this is version 2.07, periodt.
πΈ DESCRIPTION πΈ
so like. it's about working with unicode. that's the whole vibe.
for a real live practical example, go look at the Catalyst application hanging out in the examples/unicode directory in this distribution. it's there, it's cute, go visit it π¦
π ASSUMPTIONS π
okay so, for this whole tutorial we're assuming all encodings are UTF-8. lowkey you can mix different encodings from different sources, it's not that deep, but that's fully beyond the scope of this tutorial. not our problem today π
for simplicity we're also assuming:
- you're using Catalyst for your web-framework,
- DBIx::Class for your database ORM,
- TT for your templating system,
- and YAML format
HTML::FormFuconfiguration files, with YAML::XS installed.
that said β the principles we're covering should translate to whatever technologies you chose to work with. it's the vibes that transfer π
π BASICS π
short and sweet, main character energy only:
you must decode all data going into your program, and encode all data coming from your program.
that's it. that's the whole thing. tattoo it.
if you wanna skip the yapping and just see what you need to do, jump straight to "CHANGES REQUIRED". iykyk π«Ά
π INPUT π
π Input parameters from the browser
if you're using Catalyst, then Catalyst::Plugin::Unicode will decode all input parameters sent from the browser to your application β peep "Catalyst Configuration".
if you're using some other framework, or honestly if you just need to decode the input parameters yourself, go take a look at HTML::FormFu::Filter::Encode. it's right there β¨
π¦ Data from the database
if you're using DBIx::Class, then DBIx::Class::UTF8Columns is likely the best options (yes, "options", we're keeping it, it's iconic) β because it will decode all input retrieved from the database. see "DBIx::Class Configuration".
in other cases (i.e. plain DBI), you still need to decode the string data coming from the database. this varies depending on the database server. for MySQL, for instance, you can use the mysql_enable_utf8 attribute: see the DBD::mysql documentation for details.
π Your template files
set TT to decode all template files β see "TT Configuration".
π§ HTML::FormFu's own template files
set HTML::FormFu to decode all template files β see "HTML::FormFu Template Configuration".
β HTML::FormFu form configuration files
if you're using YAML config files, your files will automatically be decoded by load_config_file|HTML::FormFu/load_config_file and load_config_filestem|HTML::FormFu/load_config_filestem. automatic. slay.
if you have Config::General config files, your files will automatically be decoded by load_config_file|HTML::FormFu/load_config_file and load_config_filestem|HTML::FormFu/load_config_filestem, which automatically sets Config::General's -UTF8 setting.
πΈ Your perl source code
any perl source files which contain Unicode characters must use the utf8 module. no exceptions bestie, that's the rule.
π₯ OUTPUT π₯
π Data saved to the database
with DBIx::Class, DBIx::Class::UTF8Columns will encode all data sent to the database β see "DBIx::Class Configuration".
π HTML sent to the browser
with Catalyst, Catalyst::Plugin::Unicode will encode all output sent from your application to the browser β see "Catalyst Configuration".
in other circumstances you need to be sure to output your Unicode (decoded) strings in UTF-8. to do this you can encode your output before it's sent to the browser with something like:
use utf8;
if ( $output && utf8::is_utf8($output) ){
utf8::encode( $output ); # Encodes in-place
}
another option is to set the binmode for STDOUT:
bindmode STDOUT, ':utf8';
β οΈ but be sure to do this only when sending UTF-8 data: if you're serving images, PFD files, etc,binmodeshould remain set to:raw. do NOT mess this up, bestie, we're begging π
π CHANGES REQUIRED π
π Catalyst Configuration
add Catalyst::Plugin::Unicode to the list of Catalyst plugins:
use Catalyst qw( ConfigLoader Static::Simple Unicode );
π DBIx::Class Configuration
add DBIx::Class::UTF8Columns to the list of components loaded, for each table that has columns storing unicode:
__PACKAGE__->load_components( qw( UTF8Columns HTML::FormFu PK::Auto Core ) );
pass each column name that will store unicode to utf8_columns():
__PACKAGE__->utf8_columns( qw( lastname firstname ) );
π¦ TT Configuration
tell TT to decode all template files, by adding the following to your application config in MyApp.pm:
package MyApp;
use parent 'Catalyst';
use Catalyst qw( ConfigLoader );
MyApp->config({
'View::TT' => {
ENCODING => 'UTF-8',
},
});
1;
π§ HTML::FormFu Template Configuration
make HTML::FormFu tell TT to decode all template files, by adding the following to your myapp.yml Catalyst configuration file:
package MyApp;
use parent 'Catalyst';
use Catalyst qw( ConfigLoader );
MyApp->config({
'Controller::HTML::FormFu' => {
constructor => {
tt_args => {
ENCODING => 'UTF-8',
},
},
},
});
1;
these above 2 examples should be combined, like so:
package MyApp;
use parent 'Catalyst';
use Catalyst qw( ConfigLoader );
MyApp->config({
'Controller::HTML::FormFu' => {
constructor => {
tt_args => {
ENCODING => 'UTF-8',
},
},
},
'View::TT' => {
ENCODING => 'UTF-8',
},
});
1;
π AUTHORS π
Carl Franks cfranks@cpan.org
Michele Beltrame arthas@cpan.org (contributions) π«Ά
β COPYRIGHT β
this document is free, you can redistribute it and/or modify it under the same terms as Perl itself. iconic behaviour honestly.
π AUTHOR π
Carl Franks <cpan@fireartist.com>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2018 by Carl Franks.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie π this one shipped with perl v5.28.1 and was dated 2019-01-12 β so yes, it's vintage, but it ATE and left no crumbs πΈβ¨ take care of your encodings, hydrate, and go decode something π¦π
π¦ where this page comes from
- debian bookworm: libhtml-formfu-perl 2.07000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libhtml-formfu-perl 2.07000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-formfu-perl 2.07000-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms