Makefile.PL 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. use strict;
  3. use inc::Module::Install;
  4. my $DefaultVersion = 'v5.0.0';
  5. my $DefaultDate = '2006-02-15';
  6. _build_pm();
  7. name 'Unicode-EastAsianWidth';
  8. all_from 'lib/Unicode/EastAsianWidth.pm';
  9. auto_provides;
  10. sign; WriteAll;
  11. sub _build_pm {
  12. my $file;
  13. foreach (@INC) {
  14. $file = "$_/unicore/EastAsianWidth.txt";
  15. last if -e $file;
  16. }
  17. my $use_bundled = 1;
  18. TRY: {
  19. unless (-e $file) {
  20. print "*** Cannot find unicore/EastAsianWidth.txt\n";
  21. last TRY;
  22. }
  23. unless (open EAW, $file) {
  24. print "*** Cannot open $file for reading: $!\n";
  25. last TRY;
  26. }
  27. unless (<EAW> =~ /EastAsianWidth/) {
  28. print "*** Cannot parse $file.\n";
  29. last TRY;
  30. }
  31. unless (<EAW> =~ /Date: (\d+-\d+-\d+)/ and $1 gt $DefaultDate) {
  32. print "*** Installed table not newer than the bundled version.\n";
  33. last TRY;
  34. }
  35. $use_bundled = 0;
  36. }
  37. if ($use_bundled) {
  38. print "*** Using bundled EastAsianWidth table ($DefaultVersion).\n";
  39. return;
  40. }
  41. my %ToFullName = (
  42. N => 'InEastAsianNeutral',
  43. A => 'InEastAsianAmbiguous',
  44. H => 'InEastAsianHalfwidth',
  45. W => 'InEastAsianWide',
  46. F => 'InEastAsianFullwidth',
  47. Na => 'InEastAsianNarrow',
  48. );
  49. my ($prev_code, $prev_categ) = '';
  50. my $prev_code_end = '';
  51. my %categ;
  52. while (<EAW>) {
  53. if (/^(\w+);(\w+)/) {
  54. my ($code, $categ) = ($1, $2);
  55. if ($prev_categ ne $categ) {
  56. $categ{$ToFullName{$prev_categ}} .= "$prev_code\\t$prev_code_end\n" if $prev_categ;
  57. $prev_code = $code;
  58. $prev_categ = $categ;
  59. }
  60. $prev_code_end = $code;
  61. }
  62. elsif (/^(\w+)\.\.(\w+);(\w+)/) {
  63. $categ{$ToFullName{$prev_categ}} .= "$prev_code\\t$prev_code_end\n" if $prev_categ;
  64. $categ{$ToFullName{$3}} .= "$1\\t$2\n";
  65. $prev_categ = '';
  66. }
  67. }
  68. my $out;
  69. unless (open PM, 'lib/Unicode/EastAsianWidth.pm') {
  70. print "*** Cannot read module ($!), falling back to default ($DefaultVersion)\n";
  71. return;
  72. }
  73. while (<PM>) { $out .= $_; last if /^### BEGIN ###$/ }
  74. $out .= "our \@EXPORT = qw(\n" . join(
  75. "\n", sort(values %ToFullName), qw(InFullwidth InHalfwidth)
  76. ) . "\n);\n\n";
  77. for my $name (sort values %ToFullName) {
  78. $out .= << ".";
  79. sub $name {
  80. return <<"END";
  81. $categ{$name}END
  82. }
  83. .
  84. }
  85. while (<PM>) { $out .= $_ and last if /^### END ###$/ }
  86. while (<PM>) { $out .= $_ }
  87. close PM;
  88. chmod 0644, 'lib/Unicode/EastAsianWidth.pm';
  89. unless (open PM, '>', 'lib/Unicode/EastAsianWidth.pm') {
  90. print "*** Cannot write to module ($!), falling back to default ($DefaultVersion)\n";
  91. return;
  92. }
  93. print PM $out;
  94. close PM;
  95. }