NAME Test::Mojibake - check your source for encoding misbehavior. VERSION version 1.3 SYNOPSIS # Test::Mojibake lets you check for inconsistencies in source/documentation encoding, and report its results in standard Test::Simple fashion. no strict 'vars'; use Test::Mojibake; file_encoding_ok($file, 'Valid encoding'); done_testing($num_tests); DESCRIPTION Many modern text editors automatically save files using UTF-8 codification, however, perl interpreter does not expects it by default. Whereas this does not represent a big deal on (most) backend-oriented programs, Web framework (Catalyst , Mojolicious ) based applications will suffer of so-called Mojibake (lit. "unintelligible sequence of characters"). Even worse: if an editor saves BOM (Byte Order Mark, U+FEFF character in Unicode) at the start of the script with executable bit set (on Unix systems), it won't execute at all, due to shebang corruption. Avoiding codification problems is quite simple: * Always use utf8/use common::sense when saving source as UTF-8; * Always specify =encoding UTF-8 when saving POD as UTF-8; * Do neither of above when saving as ISO-8859-1; * Never save BOM (not that it's wrong; just avoid it as you'll barely notice it's presence when in trouble). However, if you find yourself upgrading old code to use UTF-8 or trying to standardize a big project with many developers each one using a different platform/editor, reviewing all files manually can be quite painful. Specially in cases when some files have multiple encodings (note: it all started when I realized that Gedit & derivatives are unable to open files with character conversion tables). Enter the Test::Mojibake ;) FUNCTIONS file_encoding_ok( FILENAME[, TESTNAME ] ) Validates the codification of FILENAME. When it fails, file_encoding_ok() will report the probable cause. The optional second argument TESTNAME is the name of the test. If it is omitted, file_encoding_ok() chooses a default test name "Mojibake test for FILENAME". all_files_encoding_ok( [@entries] ) Validates codification of all the files under @entries. It runs all_files() on directories and assumes everything else to be a file to be tested. It calls the plan() function for you (one test for each file), so you can't have already called plan. If @entries is empty or not passed, the function finds all source/documentation files in files in the blib directory if it exists, or the lib directory if not. A source/documentation file is one that ends with .pod, .pl and .pm, or any file where the first line looks like a shebang line. all_files( [@dirs] ) Returns a list of all the Perl files in @dirs and in directories below. If no directories are passed, it defaults to blib if blib exists, or else lib if not. Skips any files in CVS, .svn, .git and similar directories. See %Test::Mojibake::ignore_dirs for a list of them. A Perl file is: * Any file that ends in .PL, .pl, .pm, .pod, or .t; * Any file that has a first line with a shebang and "perl" on it; * Any file that ends in .bat and has a first line with "--*-Perl-*--" on it. The order of the files returned is machine-dependent. If you want them sorted, you'll have to sort them yourself. _detect_utf8( \$string ) Detects presence of UTF-8 encoded characters in a referenced octet stream. Return codes: * 0 - 8-bit characters detected, does not validate as UTF-8; * 1 - only 7-bit characters; * 2 - 8-bit characters detected, validates as UTF-8. Unicode::CheckUTF8 is highly recommended, however, it is optional and this function will fallback to the Pure Perl implementation of the following PHP code: http://www.php.net/manual/en/function.utf8-encode.php#85293 SAMPLE TEST SCRIPT Module authors can include the following in a t/mojibake.t file and have Test::Mojibake automatically find and check all source files in a module distribution: #!perl -T use strict; BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval 'use Test::Mojibake'; plan skip_all => 'Test::Mojibake required for source encoding testing' if $@; all_files_encoding_ok(); OPERATION Test::Mojibake validates codification of both source (Perl code) and documentation (POD). Both are assumed to be encoded in ISO-8859-1 (aka latin1). Perl switches to UTF-8 through the statement: use utf8; or: use utf8::all; or even: use common::sense; Similarly, POD encoding can be changed via: =encoding UTF-8 Correspondingly, no utf8/=encoding latin1 put Perl back into ISO-8859-1 mode. Actually, Test::Mojibake only cares about UTF-8, as it is roughly safe to be detected. So, when UTF-8 characters are detected without preceding declaration, an error is reported. On the other way, non-UTF-8 characters in UTF-8 mode are wrong, either. If present, Unicode::CheckUTF8 module (XS wrapper) will be used to validate UTF-8 strings, note that it is 30 times faster and a lot more Unicode Consortium compliant than the built-in Pure Perl implementation! UTF-8 BOM (Byte Order Mark) is also detected as an error. While Perl is OK handling BOM, your OS probably isn't. Check out: ./bom.pl: line 1: $'\357\273\277#!/usr/bin/perl': command not found Caveats Whole-line source comments, like: # this is a whole-line comment... print "### hello world ###\n"; # ...and this os not are not checked at all. This is mainly because many scripts/modules do contain authors' names in headers, before the proper encoding specification. So, if you happen to have some acutes/umlauts in your name and your editor sign your code in the similar way, you probably won't be happy with Test::Mojibake flooding you with (false) error messages. If you are wondering why only whole-line comments are stripped, check the second line of the above example. SEE ALSO * scan_mojibake * common::sense * utf8::all * Dist::Zilla::Plugin::MojibakeTests * Test::Perl::Critic * Test::Pod * Test::Pod::Coverage * Test::Kwalitee ACKNOWLEDGEMENTS This module is based on Test::Pod. Thanks to Andy Lester, David Wheeler, Paul Miller and Peter Edwards for contributions and to brian d foy for the original code. AUTHOR Stanislaw Pusep COPYRIGHT AND LICENSE This software is copyright (c) 2017 by Stanislaw Pusep. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. CONTRIBUTORS * Dave Rolsky * Hunter McMillen * John SJ Anderson * Karen Etheridge