如何使用现代 perl 和 utf8 默认设置“use My::defaults”?

2024-01-05

我想为我自己的“默认使用”制作一个模块,例如:

use My::perldefs;

包含以下内容(主要基于基督的 https://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129 post.)

use 5.014;
use strict;
use features qw(switch say state);

no warnings;
use warnings qw(FATAL closed threads internal debugging pack substr malloc
                unopened portable prototype inplace io pipe unpack regexp
                deprecated exiting glob digit printf utf8 layer
                reserved parenthesis taint closure semicolon);
no warnings qw(exec newline);

use utf8;
use open qw(:std :utf8);
use charnames qw(:full);
use feature qw(unicode_strings);
use Encode qw(encode decode);
use Unicode::Normalize qw(NFD NFC);
use Carp qw(carp croak confess cluck);
use autodie;

很简单,想要实现一个use My::perldefs为了实现

  • 完整且正确的 utf8 支持,并且
  • 所有现代 Perl 功能均已打开。

基于最近的问题 https://stackoverflow.com/q/6494138/734304好的起点是 uni::perl。它几乎可以做我想做的所有事情,只需要添加:

use feature qw(unicode_strings);
use charnames qw(:full);
use Encode qw(encode decode);
use Unicode::Normalize qw(NFD NFC);
use autodie;

我将奖励那些用上述 5 行扩展 uni::perl (插入下面)的人,使用有效且正确的方法。

请帮助为 utf8 和现代 Perl 使用制作一个好的样板。谢谢。


Bellow 是 uni::perl 的副本。

package My::perldefs;

use 5.014;
BEGIN {
    ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
    $^H |= 0x00000602;
}
m{
use strict;
use warnings;
}x;
use mro ();

BEGIN {
    for my $sub (qw(carp croak confess)) {
        no strict 'refs';
        *$sub = sub {
            my $caller = caller;
            local *__ANON__ = $caller .'::'. $sub;
            require Carp;
            *{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
            goto &{ 'Carp::'.$sub };
        };
    }
}

sub import {
    my $me = shift;
    my $caller = caller;
    ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";

    $^H |=
          0x00000602 # strict
        | 0x00800000 # utf8
    ;

    # use feature
    $^H{feature_switch} =
    $^H{feature_say}    =
    $^H{feature_state}  = 1;

    # use mro 'c3';
    mro::set_mro($caller, 'c3');

    #use open (:utf8 :std);
    ${^OPEN} = ":utf8\0:utf8";
    binmode(STDIN,   ":utf8");
    binmode(STDOUT,  ":utf8");
    binmode(STDERR,  ":utf8");

    for my $sub (qw(carp croak confess)) {
        no strict 'refs';
        *{ $caller .'::'. $sub } = \&$sub;
    }
    while (@_) {
        my $feature = shift;
        if ($feature =~ s/^://) {
            my $package = $me. '::'. $feature;
            eval "require $package; 1" or croak( "$@" );
            $package->load( $caller );
        }
    }
}

1;

Ps:

All of the above is (C): Mons Anderson, C<< <mons at cpan.org> >>

use feature qw(unicode_strings)简单,$^H{feature_unicode}只需要设置即可。其他模块也不是太难,只需使用require并显式调用必要的模块函数(例如Encode and Unicode::Normalize定义一个export方法通过Exporter以调用包作为参数)。棘手的是autodie,它确实严格按照caller并且通常会将其功能注入到My::perldefs包裹。我认为这里唯一好的解决方案(除了重新实现模块之外)My::perldefs)正在使用goto- 这允许调用所需的方法而不改变caller,因此这些方法被注入到正确的命名空间中。这是我最终得到的:

package My::perldefs;

use 5.014;
BEGIN {
    ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
    $^H |= 0x00000602;
}
m{
use strict;
use warnings;
}x;
use mro ();

BEGIN {
    for my $sub (qw(carp croak confess)) {
        no strict 'refs';
        *$sub = sub {
            my $caller = caller;
            local *__ANON__ = $caller .'::'. $sub;
            require Carp;
            *{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
            goto &{ 'Carp::'.$sub };
        };
    }
}

sub import {
    my $me = shift;
    my $caller = caller;
    ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";

    $^H |=
          0x00000602 # strict
        | 0x00800000 # utf8
    ;

    # use feature
    $^H{feature_switch} =
    $^H{feature_say}    =
    $^H{feature_state}  =
    $^H{feature_unicode}= 1;

    # use mro 'c3';
    mro::set_mro($caller, 'c3');

    #use open (:utf8 :std);
    ${^OPEN} = ":utf8\0:utf8";
    binmode(STDIN,   ":utf8");
    binmode(STDOUT,  ":utf8");
    binmode(STDERR,  ":utf8");

    #use charnames qw(:full)
    require charnames;
    charnames->import(":full");

    #use Encode qw(encode decode)
    require Encode;
    Encode->export($caller, "encode", "decode");

    #use Unicode::Normalize qw(NFC NFD)
    require Unicode::Normalize;
    Unicode::Normalize->export($caller, "NFC", "NFD");

    for my $sub (qw(carp croak confess)) {
        no strict 'refs';
        *{ $caller .'::'. $sub } = \&$sub;
    }
    while (@_) {
        my $feature = shift;
        if ($feature =~ s/^://) {
            my $package = $me. '::'. $feature;
            eval "require $package; 1" or croak( "$@" );
            $package->load( $caller );
        }
    }

    #use autodie qw(:default)
    #goto needs to be used here to make sure that caller doesn't change
    require autodie;
    @_ = ("autodie", ":default");
    goto &autodie::import;
}

1;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用现代 perl 和 utf8 默认设置“use My::defaults”? 的相关文章

  • perl生成字符串来匹配正则表达式

    我尝试找到一种方法来生成与正则表达式匹配的字符串 例如以下正则表达式 A Z 6 6 A Z2 9 A NP Z0 9 A Z0 9 3 3 0 1 我尝试过 Cpan 上的一些 perl 模块不起作用 gt 字符串 随机 gt 正则表达式
  • json_encode() 非 utf-8 字符串?

    所以我有一个字符串数组 并且所有字符串都使用系统默认值ANSI编码并从 SQL 数据库中提取 因此有 256 种不同的可能的字符字节值 单字节编码 有什么方法可以让我得到json encode 工作并显示这些字符而不必使用utf8 enco
  • UTF8 vs. UTF16 vs. char* vs. 什么?谁来给我解释一下这个烂摊子!

    我已经设法忽略所有这些多字节字符的东西 但现在我需要做一些 UI 工作 我知道我在这方面的无知将会赶上我 谁能用几段或更少的内容解释我需要知道什么 以便我可以本地化我的应用程序 我应该使用什么类型 我同时使用 Net 和 C C 并且我需要
  • Python UTF-8转换问题

    在我的数据库中 我存储了一些 UTF 8 字符 例如 名称 字段中的 通过 Django ORM 当我读到这个时 我得到了类似的东西 gt gt gt p name u xce xb1 gt gt gt print p name 我本来希望
  • Perl Mongo 查找对象 ID

    你会认为这是一件简单的事情 我有一个集合中的对象 ID 列表 我想根据对象 ID 获取单个记录 谷歌搜索过 但没有任何帮助 所以我有对象 ID 5106c7703abc120a04070b34 my client MongoDB Mongo
  • 与 Ruby 1.9.X 中的 Iconv.conv("UTF-8//IGNORE",...) 等效吗?

    我正在从远程源读取数据 偶尔会得到另一种编码的一些字符 它们并不重要 我想得到一个 最佳猜测 utf 8 字符串 并忽略无效数据 主要目标是获得一个我可以使用的字符串 并且不会遇到以下错误 编码 UndefinedConversionErr
  • 如何拆分一行并重新排列其元素?

    我在一行中有一些数据 如下所示 abc edf xyz rfg yeg udh 我想呈现如下数据 abc xyz yeg edf rfg udh 以便打印备用字段并用换行符分隔 有没有这样的衬里 下列awk脚本可以做到这一点 gt echo
  • 如何在ggplot2中使用希腊符号?

    我的类别需要用希腊字母命名 我在用ggplot2 并且它与数据配合得很好 不幸的是 我无法弄清楚如何将这些希腊符号放在 x 轴上 在刻度线处 并使它们出现在图例中 有什么办法可以做到吗 更新 我看了一下link https github c
  • 如何设置 $!在 Perl 中

    我想在 perl 中编写一些设置 的函数 与内置 perl 函数类似 当我尝试执行此操作时 它抱怨 参数 无法创建管理员用户 在标量分配中不是数字 我试过用谷歌搜索这个 但不幸的是谷歌不会在 所以结果很难得到 if createUser a
  • 如何在 Perl 中以函数式风格进行编码?

    你如何 have a sub返回一个sub or 将文本作为代码执行 in Perl 另外 如何拥有匿名函数存储状态 子返回子作为coderef example 1 return a sub that is defined inline s
  • 是否有用于 AES 的纯 Perl 模块?

    是否有用于 AES 的纯 Perl 模块 地穴 Rijndael PP http search cpan org dist Crypt Rijndael PP Rijndael 是底层算法AES https secure wikimedia
  • 使用 Perl 分割大文本文件

    我必须将一个 1 8Tb 的大文本文件分成两部分 我只需要文件的后半部分 该文件有 n作为记录分隔符 I tried perl ne print if gt line to start from test txt gt result txt
  • 从日志尾部提取匹配行后退出

    我使用范围运算符来提取日志文件的一部分 例如 tail F logfile perl ne print if b d 现在 一旦提取的部分匹配 我就尝试退出该过程 我尝试过 tail F logfile perl ne print if b
  • 如何在 perl 程序中查找打开的全局文件句柄

    我刚刚发现一个问题 我必须关闭所有打开的文件句柄才能让我的 Apache cgi 脚本继续 我将问题追溯到 Parse RecDescent usr bin env perl use strict use warnings use feat
  • Perl 中的全局变量、子程序变量问题

    如何将子程序变量值转移到另一个子程序变量中 我可以使用全局变量吗 sub foo my myvar Hello sub foo1 my myvar1 myvar how can I get the Hello from myvar 我尝试使
  • OpenSSL DH 密钥太小错误

    我正在尝试使用简单的 PERL 脚本连接到封闭的服务器 空调 usr bin perl use 5 10 1 use warnings use strict use IO Socket SSL use IO Socket SSL qw de
  • #1115 - 未知字符集:'utf8mb4'

    我的电脑上运行着一个本地网络服务器 用于本地开发 我现在正处于导出数据库并导入到我的托管 VPS 的阶段 导出然后导入时出现以下错误 1115 未知字符集 utf8mb4 有人能指出我正确的方向吗 该错误明确表明您没有utf8mb4您的阶段
  • Perl 的反引号、system 和 exec 有什么区别?

    有人可以帮帮我吗 在 Perl 中 以下之间有什么区别 exec command and system command and print command 还有其他方法可以运行 shell 命令吗 exec 执行命令并一去不复返 这就像一个
  • 你能挽救我的负面回顾示例来传达数字吗?

    在 高级正则表达式 一章中掌握 Perl http oreilly com catalog 9780596527242 我有一个损坏的示例 我无法找到一个很好的修复方法 这个例子可能为了自己的利益而试图变得太聪明 但也许有人可以帮我解决它
  • Perl Parallel::Forkmanager 不允许收集变量值

    也许因为子进程不知道我的散列 请参阅下面的代码 散列 输出没有收集任何内容 除了写入 tmp 文件之外 还有其他方法来收集该值吗 foreach Item AllItems pid pm gt start Item and next Tem

随机推荐