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

前端之家收集整理的这篇文章主要介绍了如何使用现代perl&utf8默认设置“使用My :: defaults”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为自己的“默认使用”制作一个模块,例如:
  1. use My::perldefs;

具有以下内容(主要基于tchrist’s帖子)

  1. use 5.014;
  2. use strict;
  3. use features qw(switch say state);
  4.  
  5. no warnings;
  6. use warnings qw(FATAL closed threads internal debugging pack substr malloc
  7. unopened portable prototype inplace io pipe unpack regexp
  8. deprecated exiting glob digit printf utf8 layer
  9. reserved parenthesis taint closure semicolon);
  10. no warnings qw(exec newline);
  11.  
  12. use utf8;
  13. use open qw(:std :utf8);
  14. use charnames qw(:full);
  15. use feature qw(unicode_strings);
  16. use Encode qw(encode decode);
  17. use Unicode::Normalize qw(NFD NFC);
  18. use Carp qw(carp croak confess cluck);
  19. use autodie;

只需要实现一个使用My :: perldefs来实现

>完整和正确的utf8支持,并与
>所有现代的perl功能开启.

基于recent question,良好的起点是uni :: perl.几乎所有的东西都是我想要的,只需要添加

  1. use feature qw(unicode_strings);
  2. use charnames qw(:full);
  3. use Encode qw(encode decode);
  4. use Unicode::Normalize qw(NFD NFC);
  5. use autodie;

我将奖励有利于上述5条线的uni :: perl(inseretd bellow)的使用有效和正确的方法的人.

请帮助为utf8和现代perl使用做一个很好的样板.谢谢.

Bellow是uni :: perl的副本.

  1. package My::perldefs;
  2.  
  3. use 5.014;
  4. BEGIN {
  5. ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
  6. $^H |= 0x00000602;
  7. }
  8. m{
  9. use strict;
  10. use warnings;
  11. }x;
  12. use mro ();
  13.  
  14. BEGIN {
  15. for my $sub (qw(carp croak confess)) {
  16. no strict 'refs';
  17. *$sub = sub {
  18. my $caller = caller;
  19. local *__ANON__ = $caller .'::'. $sub;
  20. require Carp;
  21. *{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
  22. goto &{ 'Carp::'.$sub };
  23. };
  24. }
  25. }
  26.  
  27. sub import {
  28. my $me = shift;
  29. my $caller = caller;
  30. ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
  31.  
  32. $^H |=
  33. 0x00000602 # strict
  34. | 0x00800000 # utf8
  35. ;
  36.  
  37. # use feature
  38. $^H{feature_switch} =
  39. $^H{feature_say} =
  40. $^H{feature_state} = 1;
  41.  
  42. # use mro 'c3';
  43. mro::set_mro($caller,'c3');
  44.  
  45. #use open (:utf8 :std);
  46. ${^OPEN} = ":utf8\0:utf8";
  47. binmode(STDIN,":utf8");
  48. binmode(STDOUT,":utf8");
  49. binmode(STDERR,":utf8");
  50.  
  51. for my $sub (qw(carp croak confess)) {
  52. no strict 'refs';
  53. *{ $caller .'::'. $sub } = \&$sub;
  54. }
  55. while (@_) {
  56. my $feature = shift;
  57. if ($feature =~ s/^://) {
  58. my $package = $me. '::'. $feature;
  59. eval "require $package; 1" or croak( "$@" );
  60. $package->load( $caller );
  61. }
  62. }
  63. }
  64.  
  65. 1;

PS:

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

解决方法

使用功能qw(unicode_strings)很简单,只需要设置$^ H {feature_unicode}.其他模块也不是太难,只需要明确地使用require并调用必要的模块功能(例如,Encode和Unicode :: Normalize通过导出器定义导出方法,该方法调用包作为参数).棘手的一个是autodie,它真的严格按照调用者的价值,通常会将其功能注入到My :: perldefs包中.我认为这里唯一很好的解决方案(在My :: perldefs中没有重新实现模块)正在使用goto – 这允许调用所需的方法而不改变调用者,因此这些方法被注入到正确的命名空间中.这是我得到的结局:
  1. package My::perldefs;
  2.  
  3. use 5.014;
  4. BEGIN {
  5. ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
  6. $^H |= 0x00000602;
  7. }
  8. m{
  9. use strict;
  10. use warnings;
  11. }x;
  12. use mro ();
  13.  
  14. BEGIN {
  15. for my $sub (qw(carp croak confess)) {
  16. no strict 'refs';
  17. *$sub = sub {
  18. my $caller = caller;
  19. local *__ANON__ = $caller .'::'. $sub;
  20. require Carp;
  21. *{ $caller.'::'.$sub } = \&{ 'Carp::'.$sub };
  22. goto &{ 'Carp::'.$sub };
  23. };
  24. }
  25. }
  26.  
  27. sub import {
  28. my $me = shift;
  29. my $caller = caller;
  30. ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
  31.  
  32. $^H |=
  33. 0x00000602 # strict
  34. | 0x00800000 # utf8
  35. ;
  36.  
  37. # use feature
  38. $^H{feature_switch} =
  39. $^H{feature_say} =
  40. $^H{feature_state} =
  41. $^H{feature_unicode}= 1;
  42.  
  43. # use mro 'c3';
  44. mro::set_mro($caller,":utf8");
  45.  
  46. #use charnames qw(:full)
  47. require charnames;
  48. charnames->import(":full");
  49.  
  50. #use Encode qw(encode decode)
  51. require Encode;
  52. Encode->export($caller,"encode","decode");
  53.  
  54. #use Unicode::Normalize qw(NFC NFD)
  55. require Unicode::Normalize;
  56. Unicode::Normalize->export($caller,"NFC","NFD");
  57.  
  58. for my $sub (qw(carp croak confess)) {
  59. no strict 'refs';
  60. *{ $caller .'::'. $sub } = \&$sub;
  61. }
  62. while (@_) {
  63. my $feature = shift;
  64. if ($feature =~ s/^://) {
  65. my $package = $me. '::'. $feature;
  66. eval "require $package; 1" or croak( "$@" );
  67. $package->load( $caller );
  68. }
  69. }
  70.  
  71. #use autodie qw(:default)
  72. #goto needs to be used here to make sure that caller doesn't change
  73. require autodie;
  74. @_ = ("autodie",":default");
  75. goto &autodie::import;
  76. }
  77.  
  78. 1;

猜你在找的Perl相关文章