2011-06-20 11 views
15

chcę zrobić moduł do mojego własnego "default użytku", np: (. Głównie w oparciu o tchrist's postu)Jak ustawić "use My :: defaults" z domyślnymi wartościami domyślnymi perl i utf8?

use My::perldefs; 

o następującej treści

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; 

Po prostu chcemy osiągnąć jeden use My::perldefs osiągnięcia

  • pełną i prawidłową obsługę utf8 iz
  • wszystkie nowoczesne funkcje Perla włączone.

Na podstawie recent question dobrym punktem początkowym jest uni :: perl. Jest to zrobić prawie wszystko, co chcę, tylko trzeba dodać:

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

I nagroda będzie z kimś, kto nagród przedłuży uni :: Perl (inseretd mieszkowe) z powyższych 5 linii, stosując skuteczny i poprawny sposób.

Proszę, HELP zrobić dobry zestaw do utf8 i nowoczesny styl użytkowania. Dzięki.


Bellow jest kopią 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

pokrewne: [perl5i] (http://p3rl.org/perl5i) [perl5] (http://p3rl.org/perl5) [Toolkit] (http://p3rl.org/Toolkit). – daxim

+2

Również [ToolSet] (http://search.cpan.org/perldoc?ToolSet) może być interesujący. – bvr

Odpowiedz

9

use feature qw(unicode_strings) jest łatwe, $^H{feature_unicode} po prostu musi być ustawiony. Inne moduły również nie są zbyt trudne, wystarczy użyć funkcji require i jawnie wywołać niezbędne funkcje modułu (np. Encode i Unicode::Normalize definiują metodę export przez , która przyjmuje pakiet wywołujący jako parametr). Podchwytliwe jest autodie, to naprawdę idzie ściśle według wartości caller i zwykle wstrzykuje swoje funkcje do pakietu My::perldefs. Myślę, że jedynym dobrym rozwiązaniem tutaj (brak ponownego wdrożenia modułu w My::perldefs) jest użycie goto - pozwala to na wywołanie wymaganej metody bez zmiany caller, więc metody są wstrzykiwane do właściwej przestrzeni nazw. Oto co mam w końcu:

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

Gdy używana jest część autodie, $ {^ OPEN} = ": utf8 \ 0: utf8"; - przestaje działać. Więc użyje go bez części z autodie. W każdym razie, thanx. Innymi słowy, "użyj autodie" nieco nie jest kompatybilny z $ {^ OPEN}. – jm666

+0

heh - właśnie znalazłem ten http://stackoverflow.com/questions/4959384/does-the-autodie-pragma-have-influence-on-the-encoding/4959646#4959646 jest to znany błąd perlowy. – jm666

Powiązane problemy