gpt4 book ai didi

Perl:解释如何工作 "uni::perl"模块 - 加载编译指示和其他模块

转载 作者:行者123 更新时间:2023-12-04 15:26:33 26 4
gpt4 key购买 nike

在我的 pervious question我问如何一次使用多个模块。得到一个 perfect answer , 和 another one什么指向我 Modern::Perl模块什么的真的很简单。

经过一番 CPAN 搜索后,我发现了另一个名为 uni::perl 的模块。 , 什么是真正复杂的 - 它等同于:

use strict;
use feature qw(say state switch);
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 (:utf8 :std);
use mro 'c3';

有人可以解释/评论它的工作原理吗?

我将整个代码粘贴到这里分成几个部分,并将我的问题添加到(使用 ### )。

我明白这个问题真的很长。但是,把它分成更小的一个也无济于事,因为整个都是关于“uni::perl”模块的。

请帮助我理解有问题的部分。
package uni::perl;
use 5.010;
BEGIN {
### OK - these are bitmask of different warnings, they're coming from:
# paste this into perl to find bitmask
# 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);
# BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 };

${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
$^H |= 0x00000602; ### this mean "use strict;"
}

直接设置 ${^WARNING_BITS}一个 $^H ,比常见的“使用严格”等更快吗?

这是做什么的 m{ }x .
m{
use strict;
use warnings;
}x;
use mro ();

我知道“匹配”运算符和“x”标志,但不明白在这种情况下正在做什么.. use mro是一些“黑暗魔法”,普通的 perl 用户可能不需要知道... ;)
local *__ANON__ 是做什么的线?有什么好处 goto在这种情况下?
整个下一个 BEGIN block 对我来说是一个黑魔法。 ;(
BEGIN {
for my $sub (qw(carp croak confess)) {
no strict 'refs';
*$sub = sub { ### for what need replace the global *croak (etc) with this sub?
my $caller = caller;
local *__ANON__ = $caller .'::'. $sub; ### what's mean this?
require Carp;

### This set the Carp code-refs to the global namespace?
### But who is the "caller" in the BEGIN block? (compile time)

*{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };

goto &{ 'Carp::'.$sub }; ### Why need goto here?
};
}
}

最后 - 一些更清晰的事情。重写 import所以,这将在 use uni::perl; 时调用
sub import {
my $me = shift;
my $caller = caller;

### OK - again the bitmasks
${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";


### where are these documented?
$^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");


### again coderef magic. As I understand it - it will replace the
### "carp, etc" in the callers namespace with the coderef's defined
### in the above BEGIN block. But why with this complicated way?

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

### and finally - I have abosolutely no idea - what do the next code
### will take arguments of "use uni::perl qw(arg)"
### but have no idea how to use it - or what is doing ;(
while (@_) {
my $feature = shift;
if ($feature =~ s/^://) {
my $package = $me. '::'. $feature;
eval "require $package; 1" or croak( "$@" );
$package->load( $caller );
}
}
}

最近在做什么 while ?

补充问题:
  • 为什么做同样的事情两次?一次在 BEGIN block 中,一次在导入中? (导入是为了“使用”——但是为什么在“开始” block 中也做几乎相同的事情呢?

  • 因为这个问题有更多的部分,请在回答时引用相关部分。

    谢谢大家。

    最佳答案

  • 直接设置警告位会更快一些,并且具有更可预测的行为(您可以看到应该发生的一切),但显然更难使用和维护。可能是 uni::perl 的警告集通过位掩码尝试加载更容易。
  • m{ use strict; use warnings;}x;只是 void 上下文中的正则表达式。它会抛出关于上下文或 $_ 的错误。如果启用了警告,则不会设置。我不确定这样做的确切原因,可能是为了安抚一些代码度量系统,该系统查找“使用警告;使用严格”行。我可能会写它q{...} if 0;这至少更清楚一点。
  • 这个 BEGIN block 正在创建 Carp 中函数的自定义版本。它正在使用 local *__ANON__ = ...行设置任何匿名子例程的名称,以便更容易跟踪 Carp 堆栈跟踪。 BEGIN block 创建包装好的 Carp 例程。然后导入子例程将这些新包装的例程加载到调用者的命名空间中。
  • 最后一段时间似乎正在为 uni::perl 加载额外的插件模块。 .
  • 没有做同样的事情,请参阅#3的答案。 (BEGIN 创建包装的例程,import 将它们安装到调用者的空间)
  • 关于Perl:解释如何工作 "uni::perl"模块 - 加载编译指示和其他模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6494138/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com