Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 2.02 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Question about the check-argument from the encode-function
  2. #!/usr/bin/env perl
  3. use warnings;
  4. use 5.012;
  5. use Encode qw(encode);
  6. my $s = 'a';
  7.  
  8. for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
  9.     my $encoded = encode( $encoding, $s );
  10.     my $c = unpack '(B8)*', $encoded;
  11.     printf "%-12s:t%8sn", $encoding, $c;
  12. }
  13.  
  14. say "-------------------";
  15.  
  16. for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
  17.     my $encoded = encode( $encoding, $s, Encode::FB_WARN );
  18.     my $c = unpack '(B8)*', $encoded;
  19.     printf "%-12s:t%8sn", $encoding, $c;
  20. }
  21.  
  22.  
  23. # iso-8859-1  :   01100001
  24. # iso-8859-15 :   01100001
  25. # cp1252      :   01100001
  26. # cp850       :   01100001
  27. # -------------------
  28. # iso-8859-1  :   01100001
  29. # Use of uninitialized value $c in printf at ./perl1.pl line 20.
  30. # iso-8859-15 :          
  31. # Use of uninitialized value $c in printf at ./perl1.pl line 20.
  32. # cp1252      :          
  33. # Use of uninitialized value $c in printf at ./perl1.pl line 20.
  34. # cp850       :
  35.        
  36. *CHECK* = Encode::FB_QUIET
  37.   If *CHECK* is set to Encode::FB_QUIET, (en|de)code will immediately
  38.   return the portion of the data that has been processed so far when an
  39.   error occurs. The data argument will be overwritten with everything
  40.   after that point (that is, the unprocessed part of data). This is
  41.   handy when you have to call decode repeatedly in the case where your
  42.   source data may contain partial multi-byte character sequences, (i.e.
  43.   you are reading with a fixed-width buffer). Here is a sample code that
  44.   does exactly this:
  45.  
  46.     my $buffer = ''; my $string = '';
  47.     while(read $fh, $buffer, 256, length($buffer)){
  48.       $string .= decode($encoding, $buffer, Encode::FB_QUIET);
  49.       # $buffer now contains the unprocessed partial character
  50.     }
  51.  
  52. *CHECK* = Encode::FB_WARN
  53.   This is the same as above, except that it warns on error. Handy when
  54.   you are debugging the mode above.
  55.        
  56. perl -MEncode -Mutf8 -E '$s="a"; encode("utf-8", $s, Encode::FB_WARN); say $s'
  57.        
  58. my $encoded = encode( $encoding, $s, Encode::FB_WARN | Encode::LEAVE_SRC);