Perl:如何在样板模块中注入 UTF-8 pragma?

Perl: How can I inject the UTF-8 pragma in a boilerplate module?

我有一个类似于 common::sense or Modern::Perl 的 Perl 样板模块。 这大致是对 Modern::Perl 的剽窃。它看起来像这样(为了使这个问题简洁而缩短):

package Prologue;

use strict;
use feature  ();
use utf8;

sub import {
    strict  ->import;
    feature ->import( ':5.20', 'signatures' );
    utf8    ->import;
}
1;

总而言之,一切正常。除了 UTF-8 pragma。在调用代码中手动添加use utf8;达到预期效果

那么如何将 UTF-8 编译指示注入调用代码?

适合我。

$ cat Prologue.pm
package Prologue;
require utf8;
sub import { utf8->import }
1;

$ cat a.pl
$_ = "é";
CORE::say(sprintf("%vX", $_));
use Prologue;
$_ = "é";
CORE::say(sprintf("%vX", $_));

$ perl a.pl
C3.A9
E9

patszim 自行回答)

这确实按预期工作。我的失败是 use 语句中的错字:use ProLogue; 的大写 "L" 而不是 use Prologue;。在我的不区分大小写的 Windows 系统上,这会导致 Perl 默默地不导入 Prologue 模块。

Windows 上的静默导入失败现在有一个 bug report

这不是一个直接的答案,而是一个给试图创建自己的样板模块的人的指南。

Import::Into module can import arbitrary modules into other packages. It has a very good explanation of what can go wrong and what to do about it: Why to use this module?我自己没有使用那个模块,而是将相应的技巧复制到我的样板模块中。