Test::Mojibake(3pm)

πŸ“¦
version 1.3 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Test::Mojibake πŸ’–

okay bestie, welcome to Test::Mojibake β€” a lil module (manual page, section 3pm, in the User Contributed Perl Documentation corner of the world) whose whole entire job is checking your source for encoding misbehavior. no cap. it's version 1.3 and it's about to save your whole vibe.


✨ NAME ✨

Test::Mojibake β€” check your source for encoding misbehavior. πŸ¦‹


🌸 VERSION 🌸

version 1.3 (yes queen, she's got a version and she's proud of it)


πŸŽ€ 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);

like… that's it. that's the whole thing. so simple it's giving main character energy πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

okay so here's the tea. Many modern text editors automatically save files using UTF-8 codification, right? iconic of them. BUT β€” and this is the part that ruins everyone's day β€” the perl interpreter does not expect it by default. 😭

is that a big deal? for (most) backend-oriented programs, nope, lowkey fine. but for Web framework based applications β€” think Catalyst (<http://www.catalystframework.org/>) and Mojolicious (<http://mojolicio.us/>) β€” you get hit with the so-called Mojibake (<http://en.wikipedia.org/wiki/Mojibake>), which literally means "unintelligible sequence of characters". πŸ’€

and it gets WORSE. if an editor saves a BOM (Byte Order Mark, the U+FEFF character in Unicode) at the start of a script that has the executable bit set (on Unix systems), it won't execute at all, because the shebang gets corrupted. not me crying over a shebang. πŸ™ˆ

avoiding codification problems is actually quite simple, bestie:

  • 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).

but like… say you're upgrading old code to use UTF-8, or you're trying to standardize a big project where every single developer is on a different platform/editor. reviewing all those files manually? quite painful. specially when some files have multiple encodings. (note: it all started when the author realized that Gedit & derivatives are unable to open files with character conversion tables. iconic mess honestly.)

so 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. (so helpful of her.)

the optional second argument TESTNAME is the name of the test. if it's omitted, file_encoding_ok() chooses a default test name β€” and that default is 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. heads up bestie, that's a hard no. 🚫

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. it 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. sorry babe, she doesn't do that for you. πŸŽ€


🌈 _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();

literally copy paste and you're done. ate. πŸ’…


πŸ”₯ 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. either way someone's getting yelled at. 😀

If present, the 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

😭 devastating.


🌸 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 signs your code in a 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

go read them, they're all besties. πŸ’–


🫢 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. absolute legends, every single one. πŸ‘‘


πŸ‘‘ AUTHOR πŸ‘‘

Stanislaw Pusep <stas@sysd.org>


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. slay. ✨


🌟 CONTRIBUTORS 🌟

  • Dave Rolsky <autarch@urth.org>
  • Hunter McMillen <mcmillhj@gmail.com>
  • John SJ Anderson <john@genehack.org>
  • Karen Etheridge <ether@cpan.org>

and that's the whole page bestie!! this beauty came out under perl v5.36.0, dated 2022-11-20, and yes β€” that's the Test::Mojibake(3pm) footer, folded up all cute at the end like a little bow. πŸŽ€πŸ’– go forth and encode responsibly. ✨

πŸ“¦ where this page comes from