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
  • 37,241
  • 25
  • 195
  • 267
sid_com
  • 24,137
  • 26
  • 96
  • 187

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
  • 367,544
  • 15
  • 269
  • 518
0

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

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378