fork download
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use utf8; # allows 'ñ' to appear in the source code
  6.  
  7. use Encode;
  8.  
  9. my $word = "Español"; # the 'ñ' is permitted because of the 'use utf8' pragma
  10.  
  11. # Convert the string to its UTF-8 equivalent.
  12. my $utf8_word = Encode::encode("UTF-8", $word);
  13.  
  14. # Use 'utf8::decode' to convert the string back to internal form.
  15. my $word_again_via_utf8 = $utf8_word;
  16. utf8::decode($word_again_via_utf8); # converts in-place
  17.  
  18. # Use 'Encode::decode' to convert the string back to internal form.
  19. my $word_again_via_Encode = Encode::decode("UTF-8", $utf8_word);
  20.  
  21. # Do the two conversion methods produce the same result?
  22. # Prints 'Yes'.
  23. print $word_again_via_utf8 eq $word_again_via_Encode ? "Yes\n" : "No\n";
  24.  
  25. # Do we get back the original internal string after converting both ways?
  26. # Prints 'Yes'.
  27. print $word eq $word_again_via_Encode ? "Yes\n" : "No\n";
  28.  
Success #stdin #stdout 0.02s 4900KB
stdin
Standard input is empty
stdout
Yes
Yes