1 package DBIx::Class::UTF8Columns;
4 use base qw/DBIx::Class/;
8 # Perl 5.8.0 doesn't have utf8::is_utf8()
9 # Yes, 5.8.0 support for Unicode is suboptimal, but things like RHEL3 ship with it.
17 __PACKAGE__->mk_classdata( '_utf8_columns' );
21 DBIx::Class::UTF8Columns - Force UTF8 (Unicode) flag on columns
26 use base 'DBIx::Class::Core';
28 __PACKAGE__->load_components(qw/UTF8Columns/);
29 __PACKAGE__->utf8_columns(qw/name description/);
31 # then belows return strings with utf8 flag
33 $artist->get_column('description');
37 This module allows you to get columns data that have utf8 (Unicode) flag.
41 L<Template::Stash::ForceUTF8>, L<DBIx::Class::UUIDColumns>.
52 foreach my $col (@_) {
53 $self->throw_exception("column $col doesn't exist")
54 unless $self->has_column($col);
56 return $self->_utf8_columns({ map { $_ => 1 } @_ });
58 return $self->_utf8_columns;
62 =head1 EXTENDED METHODS
69 my ( $self, $column ) = @_;
70 my $value = $self->next::method($column);
72 my $cols = $self->_utf8_columns;
73 if ( $cols and defined $value and $cols->{$column} ) {
76 Encode::_utf8_on($value) unless Encode::is_utf8($value);
78 utf8::decode($value) unless utf8::is_utf8($value);
91 my %data = $self->next::method(@_);
93 foreach my $col (grep { defined $data{$_} } keys %{ $self->_utf8_columns || {} }) {
96 Encode::_utf8_on($data{$col}) unless Encode::is_utf8($data{$col});
98 utf8::decode($data{$col}) unless utf8::is_utf8($data{$col});
110 my ( $self, $column, $value ) = @_;
112 my $cols = $self->_utf8_columns;
113 if ( $cols and defined $value and $cols->{$column} ) {
115 if ($] <= 5.008000) {
116 Encode::_utf8_off($value) if Encode::is_utf8($value);
118 utf8::encode($value) if utf8::is_utf8($value);
122 $self->next::method( $column, $value );
127 Daisuke Murase <typester@cpan.org>
131 This program is free software; you can redistribute
132 it and/or modify it under the same terms as Perl itself.
134 The full text of the license can be found in the
135 LICENSE file included with this module.