lucianoes

Encode UF8 from Perl to PostgreSQL

Nov 10th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.74 KB | None | 0 0
  1. #Perl has two UTF-8 encodings, utf8 which is Perl's liberal version and UTF-8 which is
  2. #a strict #interpretation, aka utf-8-strict. The liberal version allows for encoded
  3. #characters outside the UTF-8 character set, however you can run into problems when
  4. #interoperating with #applications that expect utf-8-strict, such as PostgreSQL.
  5. #Perl function  to #strictify utf8 to UTF-8 using the Encode core module:
  6. #http://www.dev411.com/blog/2006/09/29/perl-strictify-utf8-to-UTF-8
  7.  
  8. use Encode;
  9.  
  10. sub strictify_utf8 {
  11.     my $data = shift;
  12.     if (Encode::is_utf8($data) && !Encode::is_utf8($data,1)) {
  13.         Encode::_utf8_off($data);
  14.         Encode::from_to($data, 'utf8', 'UTF-8');
  15.         Encode::_utf8_on($data);
  16.     }
  17.     return $data;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment