DVK
DVK

Reputation: 129403

Why dosn't "use utf-8;" work when used inside a Perl module?

We had a bug in a library, that was caused by one of the inputs being Unicode.

It was fixed by adding use utf8; to the script using that library.

However, adding use utf8; to the library itself (so ALL scripts using that library would be fixed) had no effect.

Why? Can this be addressed?

Upvotes: 0

Views: 101

Answers (2)

ikegami
ikegami

Reputation: 385789

use utf8; tells Perl that the current file is encoded using UTF-8.

You have a script that's encoded using UTF-8, so you had to add use utf8; to the script. (Without it, you might think you have my $x = "é";, but you're telling Perl my $x = "é";.)

Adding it to a module makes no sense if it's the script that's encoded using UTF-8. The directive must be added to each file (script or module) that's encoded using UTF-8. (If you pass the bad $x to a module, and the module produces junk because of that, it's still the script that needs to be fixed.)

Upvotes: 3

GMB
GMB

Reputation: 222462

From the documentation:

The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope.

In other words, this pragma applies to the current package only. You need to put it in every package whose source code might contain Unicode characters. If your input comes from somewhere else, then you need to ensure that it is properly decoded: the pragma will have no effect on that.

PS: I understand that you meant use utf8, not use utf-8 (the latter is not a valid pragma).

Upvotes: 3

Related Questions