1Test::Mojibake(3)     User Contributed Perl Documentation    Test::Mojibake(3)
2
3
4

NAME

6       Test::Mojibake - check your source for encoding misbehavior.
7

VERSION

9       version 1.3
10

SYNOPSIS

12           # Test::Mojibake lets you check for inconsistencies in source/documentation encoding, and report its results in standard Test::Simple fashion.
13           no strict 'vars';
14
15           use Test::Mojibake;
16           file_encoding_ok($file, 'Valid encoding');
17           done_testing($num_tests);
18

DESCRIPTION

20       Many modern text editors automatically save files using UTF-8
21       codification, however, perl interpreter does not expects it by default.
22       Whereas this does not represent a big deal on (most) backend-oriented
23       programs, Web framework (Catalyst <http://www.catalystframework.org/>,
24       Mojolicious <http://mojolicio.us/>) based applications will suffer of
25       so-called Mojibake <http://en.wikipedia.org/wiki/Mojibake> (lit.
26       "unintelligible sequence of characters").
27
28       Even worse: if an editor saves BOM (Byte Order Mark, "U+FEFF" character
29       in Unicode) at the start of the script with executable bit set (on Unix
30       systems), it won't execute at all, due to shebang corruption.
31
32       Avoiding codification problems is quite simple:
33
34       ·   Always "use utf8"/"use common::sense" when saving source as UTF-8;
35
36       ·   Always specify "=encoding UTF-8" when saving POD as UTF-8;
37
38       ·   Do neither of above when saving as ISO-8859-1;
39
40       ·   Never save BOM (not that it's wrong; just avoid it as you'll barely
41           notice it's presence when in trouble).
42
43       However, if you find yourself upgrading old code to use UTF-8 or trying
44       to standardize a big project with many developers each one using a
45       different platform/editor, reviewing all files manually can be quite
46       painful. Specially in cases when some files have multiple encodings
47       (note: it all started when I realized that Gedit & derivatives are
48       unable to open files with character conversion tables).
49
50       Enter the Test::Mojibake ";)"
51

FUNCTIONS

53   file_encoding_ok( FILENAME[, TESTNAME ] )
54       Validates the codification of "FILENAME".
55
56       When it fails, "file_encoding_ok()" will report the probable cause.
57
58       The optional second argument "TESTNAME" is the name of the test.  If it
59       is omitted, "file_encoding_ok()" chooses a default test name "Mojibake
60       test for FILENAME".
61
62   all_files_encoding_ok( [@entries] )
63       Validates codification of all the files under @entries. It runs
64       "all_files()" on directories and assumes everything else to be a file
65       to be tested. It calls the "plan()" function for you (one test for each
66       file), so you can't have already called "plan".
67
68       If @entries is empty or not passed, the function finds all
69       source/documentation files in files in the blib directory if it exists,
70       or the lib directory if not. A source/documentation file is one that
71       ends with .pod, .pl and .pm, or any file where the first line looks
72       like a shebang line.
73
74   all_files( [@dirs] )
75       Returns a list of all the Perl files in @dirs and in directories below.
76       If no directories are passed, it defaults to blib if blib exists, or
77       else lib if not. Skips any files in CVS, .svn, .git and similar
78       directories. See %Test::Mojibake::ignore_dirs for a list of them.
79
80       A Perl file is:
81
82       ·   Any file that ends in .PL, .pl, .pm, .pod, or .t;
83
84       ·   Any file that has a first line with a shebang and "perl" on it;
85
86       ·   Any file that ends in .bat and has a first line with "--*-Perl-*--"
87           on it.
88
89       The order of the files returned is machine-dependent.  If you want them
90       sorted, you'll have to sort them yourself.
91
92   _detect_utf8( \$string )
93       Detects presence of UTF-8 encoded characters in a referenced octet
94       stream.
95
96       Return codes:
97
98       ·   0 - 8-bit characters detected, does not validate as UTF-8;
99
100       ·   1 - only 7-bit characters;
101
102       ·   2 - 8-bit characters detected, validates as UTF-8.
103
104       Unicode::CheckUTF8 is highly recommended, however, it is optional and
105       this function will fallback to the Pure Perl implementation of the
106       following PHP code:
107       <http://www.php.net/manual/en/function.utf8-encode.php#85293>
108

SAMPLE TEST SCRIPT

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

OPERATION

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 utf8::all;
141
142       or even:
143
144        use common::sense;
145
146       Similarly, POD encoding can be changed via:
147
148        =encoding UTF-8
149
150       Correspondingly, "no utf8"/"=encoding latin1" put Perl back into
151       ISO-8859-1 mode.
152
153       Actually, Test::Mojibake only cares about UTF-8, as it is roughly safe
154       to be detected. So, when UTF-8 characters are detected without
155       preceding declaration, an error is reported. On the other way,
156       non-UTF-8 characters in UTF-8 mode are wrong, either.
157
158       If present, Unicode::CheckUTF8 module (XS wrapper) will be used to
159       validate UTF-8 strings, note that it is 30 times faster and a lot more
160       Unicode Consortium compliant than the built-in Pure Perl
161       implementation!
162
163       UTF-8 BOM (Byte Order Mark) is also detected as an error. While Perl is
164       OK handling BOM, your OS probably isn't. Check out:
165
166        ./bom.pl: line 1: $'\357\273\277#!/usr/bin/perl': command not found
167
168   Caveats
169       Whole-line source comments, like:
170
171        # this is a whole-line comment...
172        print "### hello world ###\n"; # ...and this os not
173
174       are not checked at all. This is mainly because many scripts/modules do
175       contain authors' names in headers, before the proper encoding
176       specification. So, if you happen to have some acutes/umlauts in your
177       name and your editor sign your code in the similar way, you probably
178       won't be happy with Test::Mojibake flooding you with (false) error
179       messages.
180
181       If you are wondering why only whole-line comments are stripped, check
182       the second line of the above example.
183

SEE ALSO

185       ·   scan_mojibake
186
187       ·   common::sense
188
189       ·   utf8::all
190
191       ·   Dist::Zilla::Plugin::MojibakeTests
192
193       ·   Test::Perl::Critic
194
195       ·   Test::Pod
196
197       ·   Test::Pod::Coverage
198
199       ·   Test::Kwalitee
200

ACKNOWLEDGEMENTS

202       This module is based on Test::Pod.
203
204       Thanks to Andy Lester, David Wheeler, Paul Miller and Peter Edwards for
205       contributions and to "brian d foy" for the original code.
206

AUTHOR

208       Stanislaw Pusep <stas@sysd.org>
209
211       This software is copyright (c) 2017 by Stanislaw Pusep.
212
213       This is free software; you can redistribute it and/or modify it under
214       the same terms as the Perl 5 programming language system itself.
215

CONTRIBUTORS

217       ·   Dave Rolsky <autarch@urth.org>
218
219       ·   Hunter McMillen <mcmillhj@gmail.com>
220
221       ·   John SJ Anderson <john@genehack.org>
222
223       ·   Karen Etheridge <ether@cpan.org>
224
225
226
227perl v5.28.0                      2017-02-06                 Test::Mojibake(3)
Impressum