Initial commit
This commit is contained in:
12
.claude-plugin/plugin.json
Normal file
12
.claude-plugin/plugin.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"name": "cpan-result-simple",
|
||||||
|
"description": "Simple result type implementation for Perl, inspired by Rust's Result type",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"author": {
|
||||||
|
"name": "kfly8",
|
||||||
|
"email": "kentafly88@gmail.com"
|
||||||
|
},
|
||||||
|
"skills": [
|
||||||
|
"./skills/"
|
||||||
|
]
|
||||||
|
}
|
||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# cpan-result-simple
|
||||||
|
|
||||||
|
Simple result type implementation for Perl, inspired by Rust's Result type
|
||||||
45
plugin.lock.json
Normal file
45
plugin.lock.json
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"$schema": "internal://schemas/plugin.lock.v1.json",
|
||||||
|
"pluginId": "gh:kfly8/claude-cpan-plugins:plugins/Result-Simple",
|
||||||
|
"normalized": {
|
||||||
|
"repo": null,
|
||||||
|
"ref": "refs/tags/v20251128.0",
|
||||||
|
"commit": "154cf4e67c424fd58bb178e0013786c0a441fb0a",
|
||||||
|
"treeHash": "08cd45d99aea826d8383c16316dc0f1fe0483921e9c28908e04b64dc5bd579f6",
|
||||||
|
"generatedAt": "2025-11-28T10:19:27.699821Z",
|
||||||
|
"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-result-simple",
|
||||||
|
"description": "Simple result type implementation for Perl, inspired by Rust's Result type",
|
||||||
|
"version": "1.0.0"
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"files": [
|
||||||
|
{
|
||||||
|
"path": "README.md",
|
||||||
|
"sha256": "876675c4775618f344d9a544da8ced71fddcb0e84b8ef175869556991205792a"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": ".claude-plugin/plugin.json",
|
||||||
|
"sha256": "82d40b26b15df7d23509aad72a7233e27d24ad18725a730501b3426dfc4da977"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "skills/result-simple/SKILL.md",
|
||||||
|
"sha256": "40ac6f5cf0a2b415a5630d417511b6a548a29f41a7f49d586d78f61bcbbf6ef5"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dirSha256": "08cd45d99aea826d8383c16316dc0f1fe0483921e9c28908e04b64dc5bd579f6"
|
||||||
|
},
|
||||||
|
"security": {
|
||||||
|
"scannedAt": null,
|
||||||
|
"scannerVersion": null,
|
||||||
|
"flags": []
|
||||||
|
}
|
||||||
|
}
|
||||||
85
skills/result-simple/SKILL.md
Normal file
85
skills/result-simple/SKILL.md
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
---
|
||||||
|
name: result-simple-perl
|
||||||
|
description: Use Result::Simple for tuple-based error handling in Perl
|
||||||
|
version: 1.0.0
|
||||||
|
author: kfly8
|
||||||
|
tags: [perl, cpan, error-handling, result-type]
|
||||||
|
---
|
||||||
|
|
||||||
|
# Result::Simple - Tuple-based Error Handling
|
||||||
|
|
||||||
|
Result::Simple provides lightweight error handling using tuples `($value, $error)` instead of objects.
|
||||||
|
|
||||||
|
## Core Functions
|
||||||
|
|
||||||
|
### `ok($value)` / `err($error)`
|
||||||
|
```perl
|
||||||
|
use Result::Simple qw(ok err);
|
||||||
|
|
||||||
|
sub divide {
|
||||||
|
my ($a, $b) = @_;
|
||||||
|
return err('Division by zero') if $b == 0;
|
||||||
|
return ok($a / $b);
|
||||||
|
}
|
||||||
|
|
||||||
|
my ($result, $error) = divide(10, 2);
|
||||||
|
if ($error) {
|
||||||
|
warn "Error: $error";
|
||||||
|
} else {
|
||||||
|
print "Result: $result";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Practical Examples
|
||||||
|
|
||||||
|
### Error Chaining Pattern
|
||||||
|
```perl
|
||||||
|
sub process_config {
|
||||||
|
my $filename = shift;
|
||||||
|
|
||||||
|
my ($content, $read_error) = read_file($filename);
|
||||||
|
return (undef, $read_error) if $read_error;
|
||||||
|
|
||||||
|
my $data = eval { decode_json($content) };
|
||||||
|
return err("JSON error: $@") if $@;
|
||||||
|
|
||||||
|
return ok($data);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Type Checking with result_for
|
||||||
|
```perl
|
||||||
|
use Result::Simple qw(ok err result_for);
|
||||||
|
use Types::Standard qw(Dict Str);
|
||||||
|
use Types::Common::Numeric qw(PositiveInt);
|
||||||
|
use kura Error => Dict[message => Str];
|
||||||
|
|
||||||
|
# result_for requires structured error type (not plain string)
|
||||||
|
result_for calculate => PositiveInt, Error;
|
||||||
|
|
||||||
|
sub calculate {
|
||||||
|
my ($a, $b) = @_;
|
||||||
|
# Error must be structured (HashRef)
|
||||||
|
return err({ message => 'Invalid input' }) if $a < 0;
|
||||||
|
return ok($a * $b);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Set RESULT_SIMPLE_CHECK_ENABLED=1 to activate type checking
|
||||||
|
# $ENV{RESULT_SIMPLE_CHECK_ENABLED} = 1;
|
||||||
|
|
||||||
|
my ($result, $error) = calculate(5, 3);
|
||||||
|
if ($error) {
|
||||||
|
warn "Error: $error->{message}";
|
||||||
|
} else {
|
||||||
|
print "Result: $result"; # 15
|
||||||
|
}
|
||||||
|
|
||||||
|
# Type checking works when RESULT_SIMPLE_CHECK_ENABLED=1
|
||||||
|
# calculate(5.5, 3); # Dies: Invalid success result (Float instead of PositiveInt)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important**: `result_for` requires:
|
||||||
|
- Type::Tiny objects (not string type names)
|
||||||
|
- Structured error type (use `Dict[...]` with kura)
|
||||||
|
- Environment variable `RESULT_SIMPLE_CHECK_ENABLED=1` for runtime checking
|
||||||
|
|
||||||
Reference in New Issue
Block a user