commit 9454937855ed4fdbe684985636f645e980db1103 Author: Zhongwei Li Date: Sun Nov 30 08:31:08 2025 +0800 Initial commit diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json new file mode 100644 index 0000000..f3b71ad --- /dev/null +++ b/.claude-plugin/plugin.json @@ -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/" + ] +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc29f27 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# cpan-result-simple + +Simple result type implementation for Perl, inspired by Rust's Result type diff --git a/plugin.lock.json b/plugin.lock.json new file mode 100644 index 0000000..f544bb5 --- /dev/null +++ b/plugin.lock.json @@ -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": [] + } +} \ No newline at end of file diff --git a/skills/result-simple/SKILL.md b/skills/result-simple/SKILL.md new file mode 100644 index 0000000..13e022c --- /dev/null +++ b/skills/result-simple/SKILL.md @@ -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 +