Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:31:10 +08:00
commit debf87ace9
4 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
{
"name": "cpan-syntax-keyword-assert",
"description": "Assert keyword for Perl with zero runtime cost",
"version": "1.0.0",
"author": {
"name": "kfly8",
"email": "kentafly88@gmail.com"
},
"skills": [
"./skills/"
]
}

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# cpan-syntax-keyword-assert
Assert keyword for Perl with zero runtime cost

45
plugin.lock.json Normal file
View File

@@ -0,0 +1,45 @@
{
"$schema": "internal://schemas/plugin.lock.v1.json",
"pluginId": "gh:kfly8/claude-cpan-plugins:plugins/Syntax-Keyword-Assert",
"normalized": {
"repo": null,
"ref": "refs/tags/v20251128.0",
"commit": "209fc83ffd6a390f8992fc978084e7f80d78d34c",
"treeHash": "9fe6938088e8e965a810783bd438e8d58f6657afc912ee93078389720ac0573b",
"generatedAt": "2025-11-28T10:19:27.906760Z",
"toolVersion": "publish_plugins.py@0.2.0"
},
"origin": {
"remote": "git@github.com:zhongweili/42plugin-data.git",
"branch": "master",
"commit": "aa1497ed0949fd50e99e70d6324a29c5b34f9390",
"repoRoot": "/Users/zhongweili/projects/openmind/42plugin-data"
},
"manifest": {
"name": "cpan-syntax-keyword-assert",
"description": "Assert keyword for Perl with zero runtime cost",
"version": "1.0.0"
},
"content": {
"files": [
{
"path": "README.md",
"sha256": "4e758b7810ec91087f4247e56ef2106b8e504f860d7138a1b5be56d4e0ed4cc9"
},
{
"path": ".claude-plugin/plugin.json",
"sha256": "0fd8bd37258919d21fd26c4f3bac65a5e3271a780ab81ae674fd4cf149bef49f"
},
{
"path": "skills/syntax-keyword-assert/SKILL.md",
"sha256": "df86eb48e1289b9b0ed60812a5e7ea95b4b3ecffbe0e2f8fe310798416672af5"
}
],
"dirSha256": "9fe6938088e8e965a810783bd438e8d58f6657afc912ee93078389720ac0573b"
},
"security": {
"scannedAt": null,
"scannerVersion": null,
"flags": []
}
}

View File

@@ -0,0 +1,139 @@
---
name: syntax-keyword-assert-perl
description: Use Syntax::Keyword::Assert for zero-cost assertions in Perl
version: 1.0.0
author: kfly8
tags: [perl, cpan, assert, debugging, syntax-extension]
---
# Syntax::Keyword::Assert - Zero-Cost Assertions
Syntax::Keyword::Assert provides an `assert` keyword for Perl with zero runtime cost when disabled.
## Core Function
### `assert EXPR`
```perl
use Syntax::Keyword::Assert;
assert $x > 0;
assert $obj isa "MyClass";
assert defined $value;
# Dies with "Assertion failed" if expression is false
assert "apple" eq "banana"; # Dies: Assertion failed
```
## Key Features
### Zero Runtime Cost
When assertions are disabled, they are completely removed at compile time:
```perl
# In production with assertions disabled:
assert expensive_check(); # This code is completely removed
```
### Environment Control
```perl
# Enable/disable assertions before module loading
$ENV{PERL_ASSERT_ENABLED} = 0; # Disable assertions
use Syntax::Keyword::Assert;
# Or keep default (enabled)
use Syntax::Keyword::Assert;
```
## Practical Examples
### Input Validation
```perl
sub divide {
my ($a, $b) = @_;
assert defined $a && defined $b;
assert $b != 0;
return $a / $b;
}
```
### Object Type Checking
```perl
sub process_user {
my $user = shift;
assert $user isa "User";
assert $user->can("get_name");
return $user->get_name();
}
```
### Development Debugging
```perl
sub complex_algorithm {
my @data = @_;
assert @data > 0;
my $result = process_data(@data);
assert defined $result;
assert ref($result) eq 'HASH';
return $result;
}
```
### Contract Programming
```perl
sub fibonacci {
my $n = shift;
# Precondition
assert $n >= 0;
my $result = $n <= 1 ? $n : fibonacci($n-1) + fibonacci($n-2);
# Postcondition
assert $result >= 0;
return $result;
}
```
## Production Deployment
### Disable in Production
```perl
# In deployment script or environment setup
BEGIN {
$ENV{PERL_ASSERT_ENABLED} = 0 if $ENV{PRODUCTION};
}
use Syntax::Keyword::Assert;
# All assertions are now compile-time no-ops
```
### Conditional Assertions
```perl
# Development-only assertions
use Syntax::Keyword::Assert;
sub critical_function {
my $data = shift;
# This assertion disappears in production
assert validate_complex_invariant($data);
return process($data);
}
```
## Best Practices
1. **Use for debugging**: Add assertions during development, disable in production
2. **Check invariants**: Verify assumptions about data state and object types
3. **Validate inputs**: Assert preconditions for function parameters
4. **Document assumptions**: Assertions serve as executable documentation
5. **Zero-cost safety**: Enable aggressive checking without production performance penalty