1Test::Mojibake(3) User Contributed Perl Documentation Test::Mojibake(3)
2
3
4
6 Test::Mojibake - check your source for encoding misbehavior.
7
9 version 0.3
10
12 # Test::Mojibake lets you check for inconsistencies in source/documentation encoding, and report its results in standard Test::Simple fashion.
13
14 use Test::Mojibake;
15 file_encoding_ok($file, 'Valid encoding');
16 done_testing($num_tests);
17
19 Many modern text editors automatically save files using UTF-8
20 codification, however, perl interpreter does not expects it by default.
21 Whereas this does not represent a big deal on (most) backend-oriented
22 programs, Web framework (Catalyst <http://www.catalystframework.org/>,
23 Mojolicious <http://mojolicio.us/>) based applications will suffer of
24 so-called Mojibake <http://en.wikipedia.org/wiki/Mojibake> (lit.
25 "unintelligible sequence of characters").
26
27 Even worse: if an editor saves BOM (Byte Order Mark, "U+FEFF" character
28 in Unicode) at the start of the script with executable bit set (on Unix
29 systems), it won't execute at all, due to shebang corruption.
30
31 Avoiding codification problems is quite simple:
32
33 · Always "use utf8"/"use common::sense" when saving source as UTF-8;
34
35 · Always specify "=encoding utf8" when saving POD as UTF-8;
36
37 · Do neither of above when saving as ISO-8859-1;
38
39 · Never save BOM (not that it's wrong; just avoid it as you'll barely
40 notice it's presence when in trouble).
41
42 However, if you find yourself upgrading old code to use UTF-8 or trying
43 to standardize a big project with many developers each one using a
44 different platform/editor, reviewing all files manually can be quite
45 painful. Specially in cases when some files have multiple encodings
46 (note: it all started when I realized that Gedit & derivatives are
47 unable to open files with character conversion tables).
48
49 Enter the Test::Mojibake ";)"
50
52 file_encoding_ok( FILENAME[, TESTNAME ] )
53 Validates the codification of "FILENAME".
54
55 When it fails, "file_encoding_ok()" will report the probable cause.
56
57 The optional second argument "TESTNAME" is the name of the test. If it
58 is omitted, "file_encoding_ok()" chooses a default test name "Mojibake
59 test for FILENAME".
60
61 all_files_encoding_ok( [@entries] )
62 Validates codification of all the files under @entries. It runs
63 "all_files()" on directories and assumes everything else to be a file
64 to be tested. It calls the "plan()" function for you (one test for each
65 file), so you can't have already called "plan".
66
67 If @entries is empty or not passed, the function finds all
68 source/documentation files in files in the blib directory if it exists,
69 or the lib directory if not. A source/documentation file is one that
70 ends with .pod, .pl and .pm, or any file where the first line looks
71 like a shebang line.
72
73 all_files( [@dirs] )
74 Returns a list of all the Perl files in @dirs and in directories below.
75 If no directories are passed, it defaults to blib if blib exists, or
76 else lib if not. Skips any files in CVS, .svn, .git and similar
77 directories. See %Test::Mojibake::ignore_dirs for a list of them.
78
79 A Perl file is:
80
81 · Any file that ends in .PL, .pl, .pm, .pod, or .t;
82
83 · Any file that has a first line with a shebang and "perl" on it;
84
85 · Any file that ends in .bat and has a first line with "--*-Perl-*--"
86 on it.
87
88 The order of the files returned is machine-dependent. If you want them
89 sorted, you'll have to sort them yourself.
90
91 _detect_utf8( \$string )
92 Detects presence of UTF-8 encoded characters in a referenced octet
93 stream.
94
95 Return codes:
96
97 · 0 - 8-bit characters detected, does not validate as UTF-8;
98
99 · 1 - only 7-bit characters;
100
101 · 2 - 8-bit characters detected, validates as UTF-8.
102
103 Unicode::CheckUTF8 is highly recommended, however, it is optional and
104 this function will fallback to the Pure Perl implementation of the
105 following PHP code:
106 http://www.php.net/manual/en/function.utf8-encode.php#85293
107 <http://www.php.net/manual/en/function.utf8-encode.php#85293>
108
110 Module authors can include the following in a t/mojibake.t file and
111 have Test::Mojibake automatically find and check all source files in a
112 module distribution:
113
114 #!perl -T
115 use strict;
116
117 BEGIN {
118 unless ($ENV{RELEASE_TESTING}) {
119 require Test::More;
120 Test::More::plan(skip_all => 'these tests are for release candidate testing');
121 }
122 }
123
124 use Test::More;
125
126 eval 'use Test::Mojibake';
127 plan skip_all => 'Test::Mojibake required for source encoding testing' if $@;
128
129 all_files_encoding_ok();
130
132 Test::Mojibake validates codification of both source (Perl code) and
133 documentation (POD). Both are assumed to be encoded in ISO-8859-1 (aka
134 latin1). Perl switches to UTF-8 through the statement:
135
136 use utf8;
137
138 or:
139
140 use common::sense;
141
142 Similarly, POD encoding can be changed via:
143
144 =encoding utf8
145
146 Correspondingly, "no utf8"/"=encoding latin1" put Perl back into
147 ISO-8859-1 mode.
148
149 Actually, Test::Mojibake only cares about UTF-8, as it is roughly safe
150 to be detected. So, when UTF-8 characters are detected without
151 preceding declaration, an error is reported. On the other way,
152 non-UTF-8 characters in UTF-8 mode are wrong, either.
153
154 If present, Unicode::CheckUTF8 module (XS wrapper) will be used to
155 validate UTF-8 strings, note that it is 30 times faster and a lot more
156 Unicode Consortium compliant than the built-in Pure Perl
157 implementation!
158
159 UTF-8 BOM (Byte Order Mark) is also detected as an error. While Perl is
160 OK handling BOM, your OS probably isn't. Check out:
161
162 ./bom.pl: line 1: $'\357\273\277#!/usr/bin/perl': command not found
163
164 Caveats
165 Whole-line source comments, like:
166
167 # this is a whole-line comment...
168 print "### hello world ###\n"; # ...and this os not
169
170 are not checked at all. This is mainly because many scripts/modules do
171 contain authors' names in headers, before the proper encoding
172 specification. So, if you happen to have some acutes/umlauts in your
173 name and your editor sign your code in the similar way, you probably
174 won't be happy with Test::Mojibake flooding you with (false) error
175 messages.
176
177 If you are wondering why only whole-line comments are stripped, check
178 the second line of the above example.
179
181 · common::sense
182
183 · Dist::Zilla::Plugin::MojibakeTests
184
185 · Test::Perl::Critic
186
187 · Test::Pod
188
189 · Test::Pod::Coverage
190
191 · Test::Kwalitee
192
194 This module is based on Test::Pod.
195
196 Thanks to Andy Lester, David Wheeler, Paul Miller and Peter Edwards for
197 contributions and to "brian d foy" for the original code.
198
200 Stanislaw Pusep <stas@sysd.org>
201
203 This software is copyright (c) 2011 by Stanislaw Pusep.
204
205 This is free software; you can redistribute it and/or modify it under
206 the same terms as the Perl 5 programming language system itself.
207
208
209
210perl v5.12.4 2011-05-23 Test::Mojibake(3)