PerlX::Assert(3) yet another assertion keyword

SYNOPSIS


use PerlX::Assert;

assert { 1 >= 10 };

DESCRIPTION

PerlX::Assert is a framework for embedding assertions in Perl code. Under normal circumstances, assertions are not checked; they are optimized away at compile time.

However if, at compile time, any of the following environment variables is true, assertions are checked, and if they fail, throw an exception.

  • "PERL_STRICT"
  • "AUTHOR_TESTING"
  • "EXTENDED_TESTING"
  • "RELEASE_TESTING"

That is, assertions will only typically be checked when the test suite is being run on the authors' machine, or otherwise opted into.

The exact decision logic can be found in Devel::StrictMode.

You can also force assertions to be checked using:

   use PerlX::Assert -check;

There are four syntaxes for expressing assertions:

   assert EXPR;
   assert { BLOCK };
   assert "name", EXPR;
   assert "name" { BLOCK };

Assertions can be named, which is probably a good idea because this module (and the rest of Moops) screws up Perl's reporting of line numbers. Names must be a quoted string (single or double quotes, or the "q" or "qq" quote-like operators); general expressions are not supported because Text::Balanced is used to parse the assertion name. An assertion is a statement, so must be followed by a semicolon unless it's the last statement in a block.

PerlX::Assert was originally distributed as part of Moops, but was fairly independent of the rest of it, and has been spun off as a separate release, and backported to Perl 5.8.1.

Assertions that span multiple lines are very likely to cause problems on versions of Perl prior to 5.12. If the "assert" keyword, the entire name, and the start of the expression or block are all on the same line, this should be sufficient.

AUTHOR

Toby Inkster <[email protected]>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.