我想为我自己的“默认使用”制作一个模块,例如:
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> >>