.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Test::Mojibake 3pm" .TH Test::Mojibake 3pm "2017-08-04" "perl v5.26.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::Mojibake \- check your source for encoding misbehavior. .SH "VERSION" .IX Header "VERSION" version 1.3 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& # Test::Mojibake lets you check for inconsistencies in source/documentation encoding, and report its results in standard Test::Simple fashion. \& no strict \*(Aqvars\*(Aq; \& \& use Test::Mojibake; \& file_encoding_ok($file, \*(AqValid encoding\*(Aq); \& done_testing($num_tests); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Many modern text editors automatically save files using \s-1UTF\-8\s0 codification, however, perl interpreter does not expects it \fIby default\fR. 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. \*(L"unintelligible sequence of characters\*(R"). .PP Even worse: if an editor saves \s-1BOM\s0 (Byte Order Mark, \f(CW\*(C`U+FEFF\*(C'\fR 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. .PP Avoiding codification problems is quite simple: .IP "\(bu" 4 Always \f(CW\*(C`use utf8\*(C'\fR/\f(CW\*(C`use common::sense\*(C'\fR when saving source as \s-1UTF\-8\s0; .IP "\(bu" 4 Always specify \f(CW\*(C`=encoding UTF\-8\*(C'\fR when saving \s-1POD\s0 as \s-1UTF\-8\s0; .IP "\(bu" 4 Do neither of above when saving as \s-1ISO\-8859\-1\s0; .IP "\(bu" 4 \&\fBNever\fR save \s-1BOM\s0 (not that it's wrong; just avoid it as you'll barely notice it's presence when in trouble). .PP However, if you find yourself upgrading old code to use \s-1UTF\-8\s0 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 \fIGedit\fR & derivatives are unable to open files with character conversion tables). .PP Enter the Test::Mojibake \f(CW\*(C`;)\*(C'\fR .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "file_encoding_ok( FILENAME[, \s-1TESTNAME\s0 ] )" .IX Subsection "file_encoding_ok( FILENAME[, TESTNAME ] )" Validates the codification of \f(CW\*(C`FILENAME\*(C'\fR. .PP When it fails, \f(CW\*(C`file_encoding_ok()\*(C'\fR will report the probable cause. .PP The optional second argument \f(CW\*(C`TESTNAME\*(C'\fR is the name of the test. If it is omitted, \f(CW\*(C`file_encoding_ok()\*(C'\fR chooses a default test name \*(L"Mojibake test for \s-1FILENAME\*(R".\s0 .SS "all_files_encoding_ok( [@entries] )" .IX Subsection "all_files_encoding_ok( [@entries] )" Validates codification of all the files under \f(CW@entries\fR. It runs \f(CW\*(C`all_files()\*(C'\fR on directories and assumes everything else to be a file to be tested. It calls the \f(CW\*(C`plan()\*(C'\fR function for you (one test for each file), so you can't have already called \f(CW\*(C`plan\*(C'\fR. .PP If \f(CW@entries\fR is empty or not passed, the function finds all source/documentation files in files in the \fIblib\fR directory if it exists, or the \fIlib\fR directory if not. A source/documentation file is one that ends with \fI.pod\fR, \fI.pl\fR and \fI.pm\fR, or any file where the first line looks like a shebang line. .SS "all_files( [@dirs] )" .IX Subsection "all_files( [@dirs] )" Returns a list of all the Perl files in \fI\f(CI@dirs\fI\fR and in directories below. If no directories are passed, it defaults to \fIblib\fR if \fIblib\fR exists, or else \fIlib\fR if not. Skips any files in \s-1CVS,\s0 .svn, .git and similar directories. See \f(CW%Test::Mojibake::ignore_dirs\fR for a list of them. .PP A Perl file is: .IP "\(bu" 4 Any file that ends in \fI.PL\fR, \fI.pl\fR, \fI.pm\fR, \fI.pod\fR, or \fI.t\fR; .IP "\(bu" 4 Any file that has a first line with a shebang and \f(CW"perl"\fR on it; .IP "\(bu" 4 Any file that ends in \fI.bat\fR and has a first line with \f(CW"\-\-*\-Perl\-*\-\-"\fR on it. .PP The order of the files returned is machine-dependent. If you want them sorted, you'll have to sort them yourself. .SS "_detect_utf8( \e$string )" .IX Subsection "_detect_utf8( $string )" Detects presence of \s-1UTF\-8\s0 encoded characters in a referenced octet stream. .PP Return codes: .IP "\(bu" 4 0 \- 8\-bit characters detected, does not validate as \s-1UTF\-8\s0; .IP "\(bu" 4 1 \- only 7\-bit characters; .IP "\(bu" 4 2 \- 8\-bit characters detected, validates as \s-1UTF\-8.\s0 .PP Unicode::CheckUTF8 is highly recommended, however, it is optional and this function will fallback to the Pure Perl implementation of the following \s-1PHP\s0 code: .SH "SAMPLE TEST SCRIPT" .IX Header "SAMPLE TEST SCRIPT" Module authors can include the following in a \fIt/mojibake.t\fR file and have Test::Mojibake automatically find and check all source files in a module distribution: .PP .Vb 2 \& #!perl \-T \& use strict; \& \& BEGIN { \& unless ($ENV{RELEASE_TESTING}) { \& require Test::More; \& Test::More::plan(skip_all => \*(Aqthese tests are for release candidate testing\*(Aq); \& } \& } \& \& use Test::More; \& \& eval \*(Aquse Test::Mojibake\*(Aq; \& plan skip_all => \*(AqTest::Mojibake required for source encoding testing\*(Aq if $@; \& \& all_files_encoding_ok(); .Ve .SH "OPERATION" .IX Header "OPERATION" Test::Mojibake validates codification of both source (Perl code) and documentation (\s-1POD\s0). Both are assumed to be encoded in \s-1ISO\-8859\-1\s0 (aka latin1). Perl switches to \s-1UTF\-8\s0 through the statement: .PP .Vb 1 \& use utf8; .Ve .PP or: .PP .Vb 1 \& use utf8::all; .Ve .PP or even: .PP .Vb 1 \& use common::sense; .Ve .PP Similarly, \s-1POD\s0 encoding can be changed via: .PP .Vb 1 \& =encoding UTF\-8 .Ve .PP Correspondingly, \f(CW\*(C`no utf8\*(C'\fR/\f(CW\*(C`=encoding latin1\*(C'\fR put Perl back into \s-1ISO\-8859\-1\s0 mode. .PP Actually, Test::Mojibake only cares about \s-1UTF\-8,\s0 as it is roughly safe to be detected. So, when \s-1UTF\-8\s0 characters are detected without preceding declaration, an error is reported. On the other way, non\-UTF\-8 characters in \s-1UTF\-8\s0 mode are wrong, either. .PP If present, Unicode::CheckUTF8 module (\s-1XS\s0 wrapper) will be used to validate \s-1UTF\-8\s0 strings, note that it is \fB30 times faster\fR and a lot more Unicode Consortium compliant than the built-in Pure Perl implementation! .PP \&\s-1UTF\-8 BOM\s0 (Byte Order Mark) is also detected as an error. While Perl is \s-1OK\s0 handling \s-1BOM,\s0 your \s-1OS\s0 probably isn't. Check out: .PP .Vb 1 \& ./bom.pl: line 1: $\*(Aq\e357\e273\e277#!/usr/bin/perl\*(Aq: command not found .Ve .SS "Caveats" .IX Subsection "Caveats" Whole-line source comments, like: .PP .Vb 2 \& # this is a whole\-line comment... \& print "### hello world ###\en"; # ...and this os not .Ve .PP are not checked at all. This is mainly because many scripts/modules do contain authors' names in headers, \fBbefore\fR 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. .PP If you are wondering why only whole-line comments are stripped, check the second line of the above example. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 scan_mojibake .IP "\(bu" 4 common::sense .IP "\(bu" 4 utf8::all .IP "\(bu" 4 Dist::Zilla::Plugin::MojibakeTests .IP "\(bu" 4 Test::Perl::Critic .IP "\(bu" 4 Test::Pod .IP "\(bu" 4 Test::Pod::Coverage .IP "\(bu" 4 Test::Kwalitee .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" This module is based on Test::Pod. .PP Thanks to Andy Lester, David Wheeler, Paul Miller and Peter Edwards for contributions and to \f(CW\*(C`brian d foy\*(C'\fR for the original code. .SH "AUTHOR" .IX Header "AUTHOR" Stanislaw Pusep .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2017 by Stanislaw Pusep. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Dave Rolsky .IP "\(bu" 4 Hunter McMillen .IP "\(bu" 4 John \s-1SJ\s0 Anderson .IP "\(bu" 4 Karen Etheridge