【问题标题】:Perl: How can I inject the UTF-8 pragma in a boilerplate module?Perl:如何在样板模块中注入 UTF-8 编译指示?
【发布时间】:2015-09-23 16:39:42
【问题描述】:

我有一个类似于common::senseModern::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 杂注。在调用代码中手动添加use utf8;即可达到预期效果。

那么如何将 UTF-8 pragma 注入到调用代码中呢?

【问题讨论】:

  • 你想完成什么?默认开启utf8;并不像听起来那么简单。
  • 我想要use utf8;,所以我可以在变量名中使用非ASCII字符(在我的例子中是德语变音符号)。我承认,我没有完全理解the world of unicode in Perl
  • 这可能与阅读相关:stackoverflow.com/questions/6162484/…
  • 真丢脸...我在use ProLogue; 中有错字。我在windows上,不区分大小写,没有报错。这个问题最好删掉吗?
  • @patszim 它实际上无法删除,因为它有一个赞成的答案。如果将来其他人遇到类似问题,我会将您的最后评论作为答案。

标签: perl utf-8 perl-module


【解决方案1】:

为我工作。

$ 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

【讨论】:

    【解决方案2】:

    patszim 自行回答)

    As pointed out by ikegami 这确实按预期工作。我的失败是use 语句中的拼写错误:use ProLogue; 用大写“L”代替use Prologue;。在我不区分大小写的 Windows 系统上,这会导致 Perl 默默地不导入 Prologue 模块。

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

    【讨论】:

      【解决方案3】:

      这不是一个直接的答案,而是为试图创建自己的样板模块的人们提供一个指针。

      Import::Into 模块可以将任意模块导入到其他包中。它很好地解释了可能出现的问题以及如何处理:Why to use this module? 我自己没有使用该模块,而是将相应的技巧复制到了我的样板模块中。

      【讨论】:

        猜你喜欢
        • 2021-02-12
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        • 2011-02-01
        相关资源
        最近更新 更多