2011-06-20 9 views
15

私は例えば、私自身の "デフォルトの使用" のためのモジュールを作りたい:、単に現代のperlとutf8のデフォルトで "My :: defaults"を使うには?

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; 

(主にtchrist'sポストに基づく。)次の内容

use My::perldefs; 

1を達成したいです

  • 完全かつ正確なUTF8のサポートを実現するためのuse My::perldefs、および
  • 付き
  • すべての最新のperl機能が有効になっています。

recent questionに基づいて、良い開始点はuni :: perlです。

私はユニを拡張しますバウンティ人と賞::上記の5行とのperl(怒鳴るinseretd)、使用します
use feature qw(unicode_strings); 
use charnames qw(:full); 
use Encode qw(encode decode); 
use Unicode::Normalize qw(NFD NFC); 
use autodie; 

を:それは私が欲しいものほぼすべてのことを行うで、唯一の追加必要効果的で正しい方法。

utf8と現代的なperlの使用のために良い定型を作成してください。ありがとう。


ベローは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("[email protected]"); 
      $package->load($caller); 
     } 
    } 
} 

1; 

Psが:

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

関連:[perl5i](http://p3rl.org/perl5i)、[perl5の](http://p3rl.org/perl5)、[ツールキット] (http://p3rl.org/Toolkit)。 – daxim

+2

[ToolSet](http://search.cpan.org/perldoc?ToolSet)も関心があります。 – bvr

答えて

9

use feature qw(unicode_strings)$^H{feature_unicode}は単純に設定する必要があり、簡単です。他のモジュールもあまり難しくないので、単にrequireを使用し、必要なモジュール関数を明示的に呼び出す必要があります(とUnicode::Normalizeは、Exporterexportメソッドを定義し、呼び出し元のパッケージをパラメータとして使用します)。厄介なのはautodieです。本当に厳密にはcallerの値になり、通常はその機能を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("[email protected]"); 
      $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; 
+0

autodie部分を使用すると、$ {^ OPEN} = ":utf8 \ 0:utf8"; - 動作を停止します。それで、オートディー部分なしでそれを使用します。とにかく、ありがとう。 つまり、 "autodieを使用する"は、$ {^ OPEN}と互換性がありません。 – jm666

+0

heh - ちょうどこれを見つけましたhttp://stackoverflow.com/questions/4959384/does-the-autodie-pragma-have-influence-on-the-encoding/4959646#4959646これは既知のperlバグです。 – jm666

関連する問題