Skip to content

assert keyword for Perl with zero runtime cost in production

License

Notifications You must be signed in to change notification settings

kfly8/Syntax-Keyword-Assert

Repository files navigation

Actions Status Coverage Status MetaCPAN Release

NAME

Syntax::Keyword::Assert - assert keyword for Perl with zero runtime cost in production

SYNOPSIS

use Syntax::Keyword::Assert;

sub hello($name) {
    assert { defined $name };
    say "Hello, $name!";
}

hello("Alice"); # => Hello, Alice!
hello();        # => Dies when STRICT mode is enabled

DESCRIPTION

Syntax::Keyword::Assert introduces a lightweight assert keyword to Perl, designed to provide runtime assertions with minimal overhead.

  • STRICT Mode

    When STRICT mode is enabled, assert statements are checked at runtime. Default is enabled. If the assertion fails (i.e., the block returns false), the program dies with an error. This is particularly useful for catching errors during development or testing.

  • Zero Runtime Cost

    When STRICT mode is disabled, the assert blocks are completely ignored at compile phase, resulting in zero runtime cost. This makes Syntax::Keyword::Assert ideal for use in production environments, as it does not introduce any performance penalties when assertions are not needed.

  • Simple Syntax

    The syntax is straightforward—assert BLOCK—making it easy to integrate into existing code.

STRICT Mode Control

If $ENV{PERL_ASSERT_ENABLED} is trusy, STRICT mode is enabled. Otherwise, it is disabled. Default is enabled.

BEGIN { $ENV{PERL_ASSERT_ENABLED} = 0 }  # Disable STRICT mode

use Syntax::Keyword::Assert;

assert { 1 == 1 };  # Always passes
assert { 0 == 1 };  # Block is ignored, no runtime cost

SEE ALSO: Bench

TIPS

Verbose error messages

If you set $Carp::Verbose = 1, you can see stack traces when an assertion fails.

use Syntax::Keyword::Assert;
use Carp;

assert {
    local $Carp::Verbose = 1;
    0;
}

SEE ALSO

LICENSE

Copyright (C) kobaken.

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

AUTHOR

kobaken [email protected]

About

assert keyword for Perl with zero runtime cost in production

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published