4

Can I omit the use 'utf8'-pragma when I am already using use encoding 'utf8'?

#!/usr/bin/env perl
use warnings;
use 5.012;
use Encode qw(is_utf8);

use encoding 'utf8';


my %hash = ( '☺' => "☺", '\x{263a}' => "\x{263a}", 'ä' => "ä", 'a' => "a" );
for my $key ( sort keys %hash ) {
    say "UTF8 flag is turned on in the STRING $key" if is_utf8( $hash{$key} );
    say "UTF8 flag is NOT turned on in the STRING $key" if not is_utf8( $hash{$key} );
}
Cœur
  • 34,719
  • 24
  • 185
  • 251
sid_com
  • 22,779
  • 24
  • 93
  • 180

2 Answers2

5

use encoding is official discouraged. The module is deprecated because it causes very weird behaviour. Instead, you should use the following:

use utf8;                             # Source code is UTF-8
use open ':std', ':encoding(UTF-8)';  # STDIN,STDOUT,STDERR are UTF-8.
ikegami
  • 343,984
  • 15
  • 249
  • 495
0

Yes, but make sure you are familiar with the CAVEATS in the perldoc.

Eugene Yarmash
  • 131,677
  • 37
  • 301
  • 358