Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kfly8/syntax-keyword-assert
assert keyword for Perl with zero runtime cost in production
https://github.com/kfly8/syntax-keyword-assert
assert perl
Last synced: about 2 months ago
JSON representation
assert keyword for Perl with zero runtime cost in production
- Host: GitHub
- URL: https://github.com/kfly8/syntax-keyword-assert
- Owner: kfly8
- License: other
- Created: 2024-08-11T14:30:00.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-11-09T10:55:24.000Z (about 2 months ago)
- Last Synced: 2024-11-09T11:31:39.912Z (about 2 months ago)
- Topics: assert, perl
- Language: Perl
- Homepage:
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
[![Actions Status](https://github.com/kfly8/Syntax-Keyword-Assert/actions/workflows/test.yml/badge.svg)](https://github.com/kfly8/Syntax-Keyword-Assert/actions) [![Coverage Status](https://img.shields.io/coveralls/kfly8/Syntax-Keyword-Assert/main.svg?style=flat)](https://coveralls.io/r/kfly8/Syntax-Keyword-Assert?branch=main) [![MetaCPAN Release](https://badge.fury.io/pl/Syntax-Keyword-Assert.svg)](https://metacpan.org/release/Syntax-Keyword-Assert)
# NAMESyntax::Keyword::Assert - assert keyword for Perl with zero runtime cost in production
# SYNOPSIS
```perl
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.
```perl
BEGIN { $ENV{PERL_ASSERT_ENABLED} = 0 } # Disable STRICT modeuse Syntax::Keyword::Assert;
assert { 1 == 1 }; # Always passes
assert { 0 == 1 }; # Block is ignored, no runtime cost
```SEE ALSO:
[Bench ](https://metacpan.org/pod/%20https%3A#github.com-kfly8-Syntax-Keyword-Assert-blob-main-bench-compare-no-assertion.pl)# TIPS
## Verbose error messages
If you set `$Carp::Verbose = 1`, you can see stack traces when an assertion fails.
```perl
use Syntax::Keyword::Assert;
use Carp;assert {
local $Carp::Verbose = 1;
0;
}
```# SEE ALSO
- [PerlX::Assert](https://metacpan.org/pod/PerlX%3A%3AAssert)
This module also uses keyword plugin, but it depends on [Keyword::Simple](https://metacpan.org/pod/Keyword%3A%3ASimple).
- [Devel::Assert](https://metacpan.org/pod/Devel%3A%3AAssert)
This module provides a similar functionality, but it dose not use a keyword plugin.
# 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