How can I test error messages including $! without locale issues?

error-handling, internationalization, localization, perl, unit-testing

Solution

Is there a better solution than changing the test to only check for qr!^unable to parse file: !?

`$!` is a dual variable, i.e. it has string and numeric values. You could use the numeric value in the error message.

Problem

In my module, I've got this code: ``` croak("unable to parse file: $!"); ``` Then, in my tests, I want to check that I get the right error message when I attempt to parse a file that doesn't exist: ``` like( exception { HTML::Tree->new_from_file( "t/non_existent.html" ) }, qr!^unable to parse file: No such file !, "opening missing file failed" ); ``` This works fine, as long as the tests are running in an English locale. But if you run the tests in a German locale, the error message will come back `unable to parse file: Datei oder Verzeichnis nicht gefunden` and the test fails. Other locales have similar issues. I can't believe this is the first time this has come up, but I can't find any modules on CPAN that address this issue. Do people simply never test the `$!` part of the error message? Is there a better solution than changing the test to only check for `qr!^unable to parse file: !`? Note: this is RT#77823 in HTML-Tree.

Original source